Qt文档阅读笔记-继承QProgressDialog使得Dialog更加灵活
生活随笔
收集整理的這篇文章主要介紹了
Qt文档阅读笔记-继承QProgressDialog使得Dialog更加灵活
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
理論
例子
?
理論
在讀一篇官方文檔的時候,發(fā)現可以子類化QProgressDialog,使得這個QProgressDialog更加的靈活,下面是官方對于QProgressDialog中最簡單的例子:
QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);progress.setWindowModality(Qt::WindowModal);for (int i = 0; i < numFiles; i++) {progress.setValue(i);if (progress.wasCanceled())break;//... copy one file}progress.setValue(numFiles);重新繼承QProgressDialog后可以使得Dialog用起來更加的靈活方便:
class MyProgressDialog : public QProgressDialog{Q_OBJECTpublic:MyProgressDialog(const QString &titleName, QWidget *parent = Q_NULLPTR);public slots:void replyProgress(qint64 bytesRead, qint64 totalBytes); };函數的定義如下:
MyProgressDialog::MyProgressDialog(const QString &titleName, QWidget *parent): QProgressDialog(parent) {setWindowTitle(titleName); }void MyProgressDialog::replyProgress(qint64 bytesRead, qint64 totalBytes) {//qDebug() << "bytesRead : " << bytesRead << " totalBytes :" << totalBytes;setMaximum(totalBytes);setValue(bytesRead); }發(fā)現這和繼承QWidget等有著異曲同工之妙!!!!
下面舉一個例子將說明這一點!
?
例子
程序結構如下圖所示:
程序運行截圖如下:
源碼如下:
testtask.h
#ifndef TESTTASK_H #define TESTTASK_H#include <QThread>class TestTask : public QThread {Q_OBJECT public:TestTask(QObject *parent = 0);~TestTask();int create(const int &total);void destroy();int getStatus();int getProgress();signals:void threadValue(int currentValue, int totalValue);private:void run();private:int m_totalByte;bool m_isRun;int m_currentByte;};#endif // TESTTASK_Hwidet.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QProgressDialog>class TestTask;class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();protected slots:void btnClicked();private:TestTask *m_testTask; };class MyProgressDialog : public QProgressDialog{Q_OBJECTpublic:MyProgressDialog(const QString &titleName, QWidget *parent = Q_NULLPTR);public slots:void replyProgress(qint64 bytesRead, qint64 totalBytes); };#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }testtask.cpp
#include "testtask.h" #include <QDebug> #include <QMessageBox>TestTask::TestTask(QObject *parent) : QThread(parent) {m_totalByte = 0;m_currentByte = 0;m_isRun = false; }TestTask::~TestTask() {}int TestTask::create(const int &total) {m_totalByte = total;m_isRun = true;start(); }void TestTask::destroy() {m_isRun = false; }int TestTask::getStatus() {return m_isRun; }int TestTask::getProgress() {return m_currentByte; }void TestTask::run() {while(m_isRun && (m_currentByte <= m_totalByte)){//qDebug() << "m_currentByte : " << m_currentByte << " m_totalByte : " << m_totalByte;m_currentByte++;QThread::sleep(1);threadValue(m_currentByte, m_totalByte);} }widget.cpp
#include "widget.h" #include "testtask.h" #include <QPushButton> #include <QVBoxLayout> #include <QDebug> #include <QMessageBox>Widget::Widget(QWidget *parent): QWidget(parent) {QPushButton *button = new QPushButton("button");QVBoxLayout *layout = new QVBoxLayout;connect(button, &QPushButton::clicked, this, &Widget::btnClicked);layout->addWidget(button);setLayout(layout);m_testTask = new TestTask; }Widget::~Widget() {}void Widget::btnClicked() {//QMessageBox::information(this, "tip", "button clicked");MyProgressDialog *dialog = new MyProgressDialog("MyProgressDialog");connect(m_testTask, &TestTask::threadValue, dialog, &MyProgressDialog::replyProgress);m_testTask->create(20);dialog->exec();delete dialog; }MyProgressDialog::MyProgressDialog(const QString &titleName, QWidget *parent): QProgressDialog(parent) {setWindowTitle(titleName); }void MyProgressDialog::replyProgress(qint64 bytesRead, qint64 totalBytes) {//qDebug() << "bytesRead : " << bytesRead << " totalBytes :" << totalBytes;setMaximum(totalBytes);setValue(bytesRead); }?
總結
以上是生活随笔為你收集整理的Qt文档阅读笔记-继承QProgressDialog使得Dialog更加灵活的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++设计模式-建造者模式
- 下一篇: Spring Boot注册Servlet