日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt文档阅读笔记-继承QProgressDialog使得Dialog更加灵活

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt文档阅读笔记-继承QProgressDialog使得Dialog更加灵活 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

理論

例子


?

理論

在讀一篇官方文檔的時候,發現可以子類化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); }

發現這和繼承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_H

widet.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_H

main.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更加灵活的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。