如何通过计算 QTWidget 中 C++ 端的倒计时来更新 QML 中的进度条?
How to update a progress Bar in QML by calculating the countdown on the C++ side in the QTWidget?
我基本上想从c++端发送进度来更新QML大小上ProgressBar的值。
我正在尝试将 QML 集成到小部件应用程序中。问题是我的 mainwindwo.cpp 文件是这样的:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QQmlContext *context = ui->quickWidget->rootContext();
QString title = "Progress Bar App";
int progress = 0;
context->setContextProperty("_myString",title); //Set Text in QML that says Hello World
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(myFunction()));
timer->start(1000);
progress = myFunction();
context->setContextProperty("_myProgress", progress);
if(progress == 100.0){
timer->stop();
}
ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
ui->quickWidget->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::myFunction(){
//Calculate progress
static uint16_t total_time = 50000;
static uint16_t progress = 0;
static uint16_t i = 0;
// for(double i=0; i<=total_time; i=i+1000){
// progress = ((i)*100)/total_time;
// qDebug()<<"Progress: "<<progress<<endl;
// }
if(i < total_time){
i = i + 1000;
progress = ((i)*100)/total_time;
qDebug()<<"Progress: "<<progress<<endl;
}else{
qDebug()<<"Finished: "<<progress<<endl;
}
return progress;
}
我想将此处计算的进度发送到 QML 端的 ProgressBar,但我似乎无法让它工作。
我的问题是,如果您通常创建带有头文件和 cpp 文件的 C++ class,您如何做同样的事情,而是使用 QTWidget 或 Mainwindow.cpp 文件中的内容并发送数据不断从它到QML文件更新一个ProgressBar?
您必须创建一个 class 继承自 QObject 并且是 C++ 和 QML 之间的桥梁:
#ifndef PROGRESSBRIDGE_H
#define PROGRESSBRIDGE_H
#include <QObject>
class ProgressBridge : public QObject
{
Q_OBJECT
Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
public:
explicit ProgressBridge(QObject *parent = nullptr)
: QObject{parent}, m_progress{0}
{}
int progress() const{
return m_progress;
}
void setProgress(int newProgress){
if (m_progress == newProgress)
return;
m_progress = newProgress;
emit progressChanged();
}
signals:
void progressChanged();
private:
int m_progress;
};
#endif // PROGRESSBRIDGE_H
mainwindow.h
ProgressBridge progress_bridge;
mainwindow.cpp
context->setContextProperty("progressBridge", &progress_bridge);
ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
int MainWindow::myFunction(){
// TODO
progress_bridge.setProgress(progress);
}
并且在 QML 中您确实使用了 Connections
ProgressBar{
id: progressbar
from: 0.0
to: 100.0
}
Connections{
target: progressBridge
function onProgressChanged(){
progressbar.value = progressBridge.progress
}
}
我基本上想从c++端发送进度来更新QML大小上ProgressBar的值。
我正在尝试将 QML 集成到小部件应用程序中。问题是我的 mainwindwo.cpp 文件是这样的:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QQmlContext *context = ui->quickWidget->rootContext();
QString title = "Progress Bar App";
int progress = 0;
context->setContextProperty("_myString",title); //Set Text in QML that says Hello World
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(myFunction()));
timer->start(1000);
progress = myFunction();
context->setContextProperty("_myProgress", progress);
if(progress == 100.0){
timer->stop();
}
ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
ui->quickWidget->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::myFunction(){
//Calculate progress
static uint16_t total_time = 50000;
static uint16_t progress = 0;
static uint16_t i = 0;
// for(double i=0; i<=total_time; i=i+1000){
// progress = ((i)*100)/total_time;
// qDebug()<<"Progress: "<<progress<<endl;
// }
if(i < total_time){
i = i + 1000;
progress = ((i)*100)/total_time;
qDebug()<<"Progress: "<<progress<<endl;
}else{
qDebug()<<"Finished: "<<progress<<endl;
}
return progress;
}
我想将此处计算的进度发送到 QML 端的 ProgressBar,但我似乎无法让它工作。
我的问题是,如果您通常创建带有头文件和 cpp 文件的 C++ class,您如何做同样的事情,而是使用 QTWidget 或 Mainwindow.cpp 文件中的内容并发送数据不断从它到QML文件更新一个ProgressBar?
您必须创建一个 class 继承自 QObject 并且是 C++ 和 QML 之间的桥梁:
#ifndef PROGRESSBRIDGE_H
#define PROGRESSBRIDGE_H
#include <QObject>
class ProgressBridge : public QObject
{
Q_OBJECT
Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
public:
explicit ProgressBridge(QObject *parent = nullptr)
: QObject{parent}, m_progress{0}
{}
int progress() const{
return m_progress;
}
void setProgress(int newProgress){
if (m_progress == newProgress)
return;
m_progress = newProgress;
emit progressChanged();
}
signals:
void progressChanged();
private:
int m_progress;
};
#endif // PROGRESSBRIDGE_H
mainwindow.h
ProgressBridge progress_bridge;
mainwindow.cpp
context->setContextProperty("progressBridge", &progress_bridge);
ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
int MainWindow::myFunction(){
// TODO
progress_bridge.setProgress(progress);
}
并且在 QML 中您确实使用了 Connections
ProgressBar{
id: progressbar
from: 0.0
to: 100.0
}
Connections{
target: progressBridge
function onProgressChanged(){
progressbar.value = progressBridge.progress
}
}