测试多线程编程:一个
对话框(dialog)上有两个pushButton、一个lable:一个start,一个stop,点击start,将一个int变量status自增(sleep(1)),同时在lable显示status的 值,点击stop按钮停止status自增,然后在lable显示自增后的status的值;对于start按钮,如果status的值大于5就弹出警告对话框。
可是每次都不弹出对话框,到应该弹出的时候,会出现:“Xlib: unexpected async reply (sequence 0x788)!”
清高手帮忙解释、解决一下下,谢谢~~~(我感觉QThread的使用和进程具体干什么有关系并非一个套路就完了)
一共6个文件:
thread.h:
#ifndef THREAD_H
#define THREAD_H
//#include <QMutex>
#include <QMessageBox>
#include <QThread>
class TestThread : public QThread
{
Q_OBJECT
public:
TestThread(int *flag, QObject *parent);
void stop();
protected:
void run();
private:
void warning(
QString s);
int *status;
volatile bool stopped;
};
#endif // THREAD_H
thread.cpp:
#include "thread.h"
TestThread::TestThread(int *flag, QObject *parent)
:status(flag),QThread(parent)
{
stopped = false;
}
void TestThread::warning(QString s)
{
QMessageBox *messagebox = new QMessageBox;
int ret = QMessageBox::critical(messagebox,
tr("Warning!!"),
s,
QMessageBox::Ok,
QMessageBox::Ok);
}
void TestThread::run()
{
while(!stopped){
if((*status)>5) {
warning("status>5");
}
(*status)++;
sleep(1);
}
stopped = false;
}
void TestThread::stop()
{
stopped = true;
warning("stopped");
}
dialog.h:
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
#include "ui_dialog.h"
#include "thread.h"
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0, Qt::WFlags flags = 0);
~Dialog();
private slots:
void tstart();
void tstop();
private:
Ui::DialogClass ui;
TestThread *testthread;
int a;
};
#endif // DIALOG_H
dialog.h:
#include "dialog.h"
Dialog::Dialog(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
a = 1;
testthread = new TestThread(&a,this);
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(tstart()));
connect(ui.pushButton_2,SIGNAL(clicked()),this,SLOT(tstop()));
ui.Textlabel->setText(tr("Not Beginning!"));
}
Dialog::~Dialog()
{
}
void Dialog::tstart()
{
testthread->start();
ui.Textlabel->setText(tr("Start!"));
ui.pushButton->setDisabled(true);
ui.pushButton_2->setEnabled(true);
}
void Dialog::tstop()
{
testthread->stop();
QString string = QString().setNum(a);
ui.Textlabel->setText("a = "+ string);
ui.pushButton_2->setDisabled(true);
ui.pushButton->setEnabled(true);
}
dialog.ui(主要是一个dialog,两个pushButton,一个lable):
<ui version="4.0" >
<class>DialogClass</class>
<widget class="QDialog" name="DialogClass" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>332</width>
<height>197</height>
</rect>
</property>
<property name="windowTitle" >
<string>Dialog</string>
</property>
<widget class="QPushButton" name="pushButton" >
<property name="geometry" >
<rect>
<x>50</x>
<y>140</y>
<width>75</width>
<height>26</height>
</rect>
</property>
<property name="text" >
<string>Start</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2" >
<property name="geometry" >
<rect>
<x>210</x>
<y>140</y>
<width>75</width>
<height>26</height>
</rect>
</property>
<property name="text" >
<string>Stop</string>
</property>
</widget>
<widget class="QLabel" name="Textlabel" >
<property name="geometry" >
<rect>
<x>120</x>
<y>60</y>
<width>101</width>
<height>20</height>
</rect>
</property>
<property name="text" >
<string/>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11" />
<resources/>
<connections/>
</ui>
main.cpp:
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
[ 此贴被ruger在2008-12-18 16:55重新编辑 ]