Envoyer des informations de C++ vers QML
Bonjour,
Je m' entraine sur quelques exemples qml sous gnu/linux
Je n ai pas rencontré trop de problemes dans mes exercices precedents, par contre je bute sur celui de "dialcontrol"
http://www.youtube.com/watch?v=Fr5FuGhTqm8
Je voulais remplacer le slider par le monitoring du cpu load mais impossible d' y parvenir, le dialcontrol reste inactif.
" ReferenceError: Can't find variable: loadcpu"
cpuload.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#ifndef CPULOAD_H
#define CPULOAD_H
#include <QList>
#include <QByteArray>
#include <QObject>
class CPULoad : public QObject
{
Q_OBJECT
public:
explicit CPULoad(QObject *parent = 0);
void init();
void update();
Q_INVOKABLE int cpuLoad() const;
private:
typedef QList<QByteArray> TimeList;
CPULoad(const CPULoad &);
CPULoad & operator= (const CPULoad &);
TimeList readTimeList();
private:
TimeList m_timeList;
int m_load;
static int m_forcedLoad;
};
#endif // CPULOAD_H |
cpuload.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include "cpuload.h"
#include <QFile>
#include <QDebug>
/*!
* \class CPULoad
* \brief CPULoad calculates the current CPU load.
*/
int CPULoad::m_forcedLoad = -1;
//! Constructor
CPULoad::CPULoad(QObject *parent) :
m_load(-1)
{}
/*!
* Reads /proc/stat file
* ToDo: use sscanf if it faster
*/
CPULoad::TimeList CPULoad::readTimeList()
{
#ifdef Q_OS_LINUX
QFile fin(QString("/proc/stat"));
if (fin.open(QIODevice::ReadOnly | QIODevice::Text))
return fin.readLine().split(' ');
#endif
return TimeList();
}
/*!
* Initialize. Currently only sets the value to -1.
*/
void CPULoad::init()
{
m_load = -1;
}
/*!
* Update the value.
* This must be called periodically (e.g. 1 - 5 seconds) in order to update the measurement.
* The data is obtained from first line of /proc/stat file
*/
void CPULoad::update()
{
// Read timing from the file
TimeList v(readTimeList());
if (m_timeList.length()) {
// Take delta with the previous timing
int sum = 0;
int idle = 0;
int iowait = 0;
int cpu = 0;
for (int i = 2; i < 7 && i < v.length(); i++)
sum += v.at(i).toInt() - m_timeList.at(i).toInt();
idle = v.at(5).toInt() - m_timeList.at(5).toInt();
iowait = v.at(6).toInt() - m_timeList.at(6).toInt();
cpu = sum - idle - iowait;
// Calculate load
m_load = 100.0 - (100.0 * idle / sum);
} else {
m_load = -1;
}
// Store the current timing
m_timeList = v;
}
/*!
* Returns The current value of the load in the range of 0 to 100
* or -1 if nothing measured.
*/
int CPULoad::cpuLoad() const
{
return m_forcedLoad > -1 ? m_forcedLoad : m_load;
} |
main.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include "cpuload.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/conkycloneqml/main.qml"));
viewer.showExpanded();
CPULoad execProcess;
QDeclarativeView view;
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("loadcpu", &execProcess);
return app->exec();
} |
main.qml (ligne 117-120)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
//! [imports]
import QtQuick 1.0
import Qt 4.7
import "content"
//! [imports]
//! [0]
Rectangle {
color: "#545454"
width: 300; height: 300
// Dial with a slider to adjust it
Dial {
id: dial
x: -42
y: -42
width: 210
height: 210
anchors.verticalCenterOffset: -87
anchors.horizontalCenterOffset: -87
scale: 0.600
anchors.centerIn: parent
value: slider.x * 100 / (container.width - 34)
}
Rectangle {
id: container
anchors { bottom: parent.bottom; left: parent.left
right: parent.right; leftMargin: 20; rightMargin: 20
bottomMargin: 10
}
height: 16
radius: 8
opacity: 0.7
smooth: true
gradient: Gradient {
GradientStop { position: 0.0; color: "gray" }
GradientStop { position: 1.0; color: "white" }
}
Rectangle {
id: slider
x: 1; y: 1; width: 30; height: 14
radius: 6
smooth: true
gradient: Gradient {
GradientStop { position: 0.0; color: "#424242" }
GradientStop { position: 1.0; color: "black" }
}
MouseArea {
anchors.fill: parent
anchors.margins: -16 // Increase mouse area a lot outside the slider
drag.target: parent; drag.axis: Drag.XAxis
drag.minimumX: 2; drag.maximumX: container.width - 32
}
}
}
QuitButton {
anchors.right: parent.right
anchors.top: parent.top
anchors.margins: 10
}
Dial {
id: dial1
x: 84
y: -42
width: 210
height: 210
anchors.verticalCenterOffset: -87
anchors.horizontalCenterOffset: 39
anchors.centerIn: parent
//value: dial1.value = loadcpu.cpuLoad()
scale: 0.600
onValueChanged: {
loadcpu.cpuLoad();
}
}
}
//! [0] |
http://qt.gitorious.org/qtplayground...b01fe/src/core
Si vous aviez l' amabilitée de m' orienter sur la facon de proceder?
Merci d' avance.
Je debute et cela n' est pas evident pour moi.