• 13002阅读
  • 18回复

请问:Qt4多线程编程问题(已解决) [复制链接]

上一主题 下一主题
离线ruger
 
只看楼主 倒序阅读 楼主  发表于: 2008-12-18
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
测试多线程编程:一个对话框(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重新编辑 ]
描述:多线程编程
附件: mutithreadtest.tar.gz (176 K) 下载次数:71
离线ruger
只看该作者 1楼 发表于: 2008-12-18
顶顶,ms无人关注
离线lj_0212

只看该作者 2楼 发表于: 2008-12-18
可以把代码打个包放上来么。
离线ruger
只看该作者 3楼 发表于: 2008-12-18
引用第2楼lj_0212于2008-12-18 14:09发表的  :
可以把代码打个包放上来么。

已上传,但是修改过,依然效果不佳
离线ruger
只看该作者 4楼 发表于: 2008-12-18
ms有用的几句话:
You don't need to look into above URL (yet): the point was that you
_probably_ update the GUI from within your callback thread - you mustn't
do that!

No thread other than the main thread (aka "GUI-thread") may update any
widget (that is, no call to QWidget::update(), QWidget::setFoo() etc.
from another thread).

Instead, in your worker thread put events into the Qt event queue and
inform the GUI thread that it needs to update its GUI state according to
the "model" (that is, your sound data).

See QApplication::postEvent.

This is a common mistake, see also Qt archive for more details. Maybe
qtforum.org knows more as well (haven't checked).
离线ruger
只看该作者 5楼 发表于: 2008-12-18
实际上,我就是想点开始按钮从一个新进程做循环,然后出错就弹出一个对话框,描述错误,点击停止按钮,就停止这个进程。
http://lists.trolltech.com/qt-interest/2005-05/msg00702.html
上面的链接可能有点帮助
离线evoleci
只看该作者 6楼 发表于: 2008-12-18
把你的显示出错对话框放在UI主线程上处理
我们只是在经历着一些事情。
离线lj_0212

只看该作者 7楼 发表于: 2008-12-18
只能在GUI-THREAD中创建widget。
离线lj_0212

只看该作者 8楼 发表于: 2008-12-18
给线程加个signal,主界面加个slot,连接起来,在slot里处理弹出框。
离线ruger
只看该作者 9楼 发表于: 2008-12-18
引用第6楼evoleci于2008-12-18 15:46发表的  :
把你的显示出错对话框放在UI主线程上处理

这样也不好,不是实时的,会处理上一次stop后的结果,而我想的是在start后,如果出现了临界条件,就弹出一个对话框,并停止该进程,在UI主线程上处理(用while(1)或者while(thread.isRunning)有时会出现死循环的情况,不知道为什么?貌似work thread并没有执行,a老是上次stop后的结果)
离线ruger
只看该作者 10楼 发表于: 2008-12-18
引用第8楼lj_0212于2008-12-18 15:54发表的  :
给线程加个signal,主界面加个slot,连接起来,在slot里处理弹出框。

i will try
离线lj_0212

只看该作者 11楼 发表于: 2008-12-18
引用第10楼ruger于2008-12-18 15:56发表的  :
i will try


我已经测过了,没有问题。
不过你需要加个控制变量,否则会不断的弹出对话框。
离线ruger
只看该作者 12楼 发表于: 2008-12-18
引用第11楼lj_0212于2008-12-18 16:35发表的  :
我已经测过了,没有问题。
不过你需要加个控制变量,否则会不断的弹出对话框。

对,我也成功了,多谢~~~不断的弹出对话框,无所谓了,成功就好,多谢~~~
复代码
描述:成功代码
附件: mutithreadtest.tar.gz (176 K) 下载次数:88
离线toby_0814
只看该作者 13楼 发表于: 2009-02-24
期待上传更多的关于qt多线程编程的知识,和实例,以及学习笔记,共享!
离线tgvlcw
只看该作者 14楼 发表于: 2009-04-02
楼主好强啊!这几天也在弄这个问题,现在看到你的代码,当运行时出现n个警告时,心情激动啊!多谢楼主啦!看来还要多加学习啊!
离线wywwh
只看该作者 15楼 发表于: 2009-12-15
mark
离线embeddedgood

只看该作者 16楼 发表于: 2010-03-23
GOOD
离线luoyes

只看该作者 17楼 发表于: 2010-03-24
mark
离线pannian

只看该作者 18楼 发表于: 2011-04-30
不错。。。
追梦永不放弃
快速回复
限100 字节
 
上一个 下一个