问题描述
- QProgressBar 不动,多线程实现进度条
-
class ProcDlg : public QRunnable
{
public:
ProcDlg(QProgressBar* progressBar);void run(); void setVal(int val);
private:
QProgressBar* m_ProgressBar;
int m_val;
};ProcDlg::ProcDlg( QProgressBar* progressBar)
{
m_ProgressBar = progressBar;
m_val = 0;
}void ProcDlg::setVal(int val)
{
m_val = val;
}void ProcDlg::run()
{while (m_val <= 100) { QMetaObject::invokeMethod(m_ProgressBar, "setValue", Qt::QueuedConnection, Q_ARG(int, m_val)); QThread::sleep(1); if(m_val == 100) { break; } }
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_pushButton_clicked();private:
Ui::MainWindow ui;
QProgressBar procBar;
ProcDlg task;
};void MainWindow::on_pushButton_clicked()
{
task = new ProcDlg(&procBar);
procBar.show();
QThreadPool::globalInstance()->start(task);int i = 1; for(i = 1; i <= 10; i++) { task->setVal(i*10); QThread::sleep(2); }
}
QProgressBar一直都不动,这是什么原因啊?
解决方案
http://www.cnblogs.com/guoyiqi/archive/2011/07/30/2139177.html
解决方案二:
using System.ComponentModel;
?
文件开头
?
??? public partial class Form1 : Form
??? {
??????? private BackgroundWorker worker = new BackgroundWorker();
?
??????? public Form1()
??????? {
??????????? Initi......
答案就在这里:进度条实现(多线程)