.hpp
#ifndef PROGRESSCONNECTION_HPP
#define PROGRESSCONNECTION_HPP
#include <QObject>
class progressConnection
: public QObject {
Q_OBJECT
Q_PROPERTY(int progressMaximum READ progressMaximum WRITE setProgressMaximum NOTIFY progressMaximumChanged)
Q_PROPERTY(int progressValue READ progressValue WRITE setProgressValue NOTIFY progressValueChanged)
public:
explicit progressConnection
(QObject *parent
= 0);
Q_INVOKABLE void addProgressValue();
int progressMaximum() const;
int progressValue() const;
void setProgressMaximum(int value);
void setProgressValue(int value);
signals:
void progressMaximumChanged();
void progressValueChanged();
private:
void addProgressValueImpl();
private:
int progressMaximum_;
int progressValue_;
int size_;
};
#endif // PROGRESSCONNECTION_HPP
#ifndef PROGRESSCONNECTION_HPP
#define PROGRESSCONNECTION_HPP
#include <QObject>
class progressConnection : public QObject
{
Q_OBJECT
Q_PROPERTY(int progressMaximum READ progressMaximum WRITE setProgressMaximum NOTIFY progressMaximumChanged)
Q_PROPERTY(int progressValue READ progressValue WRITE setProgressValue NOTIFY progressValueChanged)
public:
explicit progressConnection(QObject *parent = 0);
Q_INVOKABLE void addProgressValue();
int progressMaximum() const;
int progressValue() const;
void setProgressMaximum(int value);
void setProgressValue(int value);
signals:
void progressMaximumChanged();
void progressValueChanged();
private:
void addProgressValueImpl();
private:
QMutex mutex_;
int progressMaximum_;
int progressValue_;
int size_;
};
#endif // PROGRESSCONNECTION_HPP
To copy to clipboard, switch view to plain text mode
.cpp
#include <QDebug>
#include "progressConnection.hpp"
progressConnection
::progressConnection(QObject *parent
) : progressMaximum_(1),
progressValue_(0),
size_(100000)
{
}
void progressConnection::addProgressValue()
{
setProgressMaximum(size_);
setProgressValue(0);
addProgressValueImpl();
}
int progressConnection::progressMaximum() const
{
return progressMaximum_;
}
int progressConnection::progressValue() const
{
return progressValue_;
}
void progressConnection::setProgressMaximum(int value)
{
if(value != progressMaximum_){
progressMaximum_ = value;
qDebug()<<"progress maximum = "<<progressMaximum_;
emit progressMaximumChanged();
}
}
void progressConnection::setProgressValue(int value)
{
if(value != progressValue_){
progressValue_ = value;
//qDebug()<<"progress value = "<<progressValue_;
emit progressValueChanged();
}
}
void progressConnection::addProgressValueImpl()
{
for(int i = 0; i != size_; ++i){
setProgressValue(i);
}
}
#include <QDebug>
#include "progressConnection.hpp"
progressConnection::progressConnection(QObject *parent) :
QObject(parent),
progressMaximum_(1),
progressValue_(0),
size_(100000)
{
}
void progressConnection::addProgressValue()
{
setProgressMaximum(size_);
setProgressValue(0);
addProgressValueImpl();
}
int progressConnection::progressMaximum() const
{
return progressMaximum_;
}
int progressConnection::progressValue() const
{
return progressValue_;
}
void progressConnection::setProgressMaximum(int value)
{
if(value != progressMaximum_){
progressMaximum_ = value;
qDebug()<<"progress maximum = "<<progressMaximum_;
emit progressMaximumChanged();
}
}
void progressConnection::setProgressValue(int value)
{
if(value != progressValue_){
progressValue_ = value;
//qDebug()<<"progress value = "<<progressValue_;
emit progressValueChanged();
}
}
void progressConnection::addProgressValueImpl()
{
for(int i = 0; i != size_; ++i){
setProgressValue(i);
}
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include <QQmlEngine>
#include <QQuickView>
#include <QtQml>
#include "progressConnection.hpp"
int main(int argc, char *argv[])
{
qmlRegisterType<progressConnection>("Test", 1, 0, "ProgressConnection");
QQmlApplicationEngine engine
(QUrl("qrc:/main.qml"));
QObject *topLevel
= engine.
rootObjects().
value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
if (!window) {
qWarning("Error: Your root item has to be a Window.");
return -1;
}
window->show();
return app.exec();
}
#include <QApplication>
#include <QQmlEngine>
#include <QQuickView>
#include <QtQml>
#include "progressConnection.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<progressConnection>("Test", 1, 0, "ProgressConnection");
QQmlApplicationEngine engine(QUrl("qrc:/main.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
if (!window) {
qWarning("Error: Your root item has to be a Window.");
return -1;
}
window->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
main.qml
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.0
import QtQuick.Layouts 1.0
import Test 1.0
ApplicationWindow{
id: root
title: "Color Correction"
color: syspal.window
width: 1450
height: 750
minimumHeight: 750
minimumWidth: 1400
SystemPalette {id: syspal}
Component{
id: compProgressTest
ProgressConnection{
}
}
Loader{
id: loader
sourceComponent: null
onStatusChanged: {
if(loader.status == Loader.Ready){
console.log("Ready")
}
}
}
Row{
anchors.fill: parent
Button{
text: "reload"
onClicked: {
loader.sourceComponent = null
loader.sourceComponent = compProgressTest
}
}
Button{
text: "run"
onClicked: {
if(loader.status == Loader.Ready){
loader.item.addProgressValue()
}
}
}
ProgressBar{
id: progressBar
maximumValue: loader.status == Loader.Ready ? loader.item.progressMaximum : 1
value: loader.status == Loader.Ready ? loader.item.progressValue : 0
onValueChanged: {
console.log("progress bar value = " + progressBar.value)
}
}
}
}
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.0
import QtQuick.Layouts 1.0
import Test 1.0
ApplicationWindow{
id: root
title: "Color Correction"
color: syspal.window
width: 1450
height: 750
minimumHeight: 750
minimumWidth: 1400
SystemPalette {id: syspal}
Component{
id: compProgressTest
ProgressConnection{
}
}
Loader{
id: loader
sourceComponent: null
onStatusChanged: {
if(loader.status == Loader.Ready){
console.log("Ready")
}
}
}
Row{
anchors.fill: parent
Button{
text: "reload"
onClicked: {
loader.sourceComponent = null
loader.sourceComponent = compProgressTest
}
}
Button{
text: "run"
onClicked: {
if(loader.status == Loader.Ready){
loader.item.addProgressValue()
}
}
}
ProgressBar{
id: progressBar
maximumValue: loader.status == Loader.Ready ? loader.item.progressMaximum : 1
value: loader.status == Loader.Ready ? loader.item.progressValue : 0
onValueChanged: {
console.log("progress bar value = " + progressBar.value)
}
}
}
}
To copy to clipboard, switch view to plain text mode
The codes would crash, have no idea what kind of mistakes I do
Bookmarks