网站seo优化要怎么做,广州做网站推广公司,互联网商城建设,电子商务营销论文背景#xff1a; 一开始是通过在主线程中写一个for循环#xff0c;每次加1后睡眠1s进行进度条更新。但这样写的结果是 -- 无法动态显示进度条进度。后通过上一篇文章 [ QT5|C|通过信号槽机制实现进度条更新 ] 中的写信号槽机制实现。实现后 考虑了下有没有其他方式实现 一开始是通过在主线程中写一个for循环每次加1后睡眠1s进行进度条更新。但这样写的结果是 -- 无法动态显示进度条进度。后通过上一篇文章 [ QT5|C|通过信号槽机制实现进度条更新 ] 中的写信号槽机制实现。实现后 考虑了下有没有其他方式实现后想到了通过子线程方式。以下是通过子线程实现的具体事例 功能
1点击【显示进度条进度】按钮每隔1s动态加载进度条进度直到加载到100%
2点击【退出】按钮关闭当前对话框。1、dialog.cpp
#include dialog.h
#include ui_dialog.h
#includeQPushButton
//#includeQThread
#includethread
#includechrono
#includeiostreamDialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui-setupUi(this);connect(ui-quit_pushButton,QPushButton::clicked,this,QDialog::accept);ui-progressBar-setRange(1,100);std::thread myThread([]{ThreadFunction();});myThread.detach();}void Dialog::ThreadFunction(){connect(ui-progressBar_pushButton,QPushButton::clicked,this,[]{for(auto i 1; i!101 ;i){ui-progressBar-setValue(i);std::this_thread::sleep_for(std::chrono::milliseconds(1000));}});
}Dialog::~Dialog()
{delete ui;
}2、dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include QDialogQT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACEclass Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent nullptr);~Dialog();void ThreadFunction();private:Ui::Dialog *ui;
};
#endif // DIALOG_H3、main.cpp
#include dialog.h#include QApplicationint main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}4、Dialog.ui
?xml version1.0 encodingUTF-8?
ui version4.0classDialog/classwidget classQDialog nameDialogproperty namegeometryrectx0/xy0/ywidth800/widthheight600/height/rect/propertyproperty namewindowTitlestringDialog/string/propertywidget classQWidget nameverticalLayoutWidgetproperty namegeometryrectx150/xy370/ywidth351/widthheight161/height/rect/propertylayout classQVBoxLayout nameverticalLayoutitemlayout classQHBoxLayout namehorizontalLayoutitemwidget classQPushButton nameprogressBar_pushButtonproperty nametextstring显示进度条进度/string/property/widget/itemitemwidget classQPushButton namequit_pushButtonproperty nametextstring退出/string/property/widget/item/layout/itemitemlayout classQHBoxLayout namehorizontalLayout_2itemwidget classQProgressBar nameprogressBarproperty namevaluenumber0/number/property/widget/item/layout/item/layout/widget/widgetresources/connections/
/ui注意事项
1、在主线程的for循环中睡眠1s后更新会造成阻塞 不能直接写
2、关于主线程中的控件不要在子线程中进行设置会阻塞主线程。
关于这一点猜测的原因qt内部机制 刷新不及时虽然会阻塞最终还是会更新完进度。如果有大神了解具体原因请详细介绍下虚心学习显示效果 子线程进度条