天嵌 ARM开发社区

 找回密码
 注册
查看: 7633|回复: 19

QT下如何创建线程

[复制链接]
我爱linux 发表于 2010-8-26 16:19:31 | 显示全部楼层 |阅读模式
请问亚瑟王:

     我在你们的first例字中想创建一个线程,但编译时总是提示错误,不知什么原因?具体如下:
   
     /****************************************************************************
** Form interface generated from reading ui file 'first.ui'
**
** Created: Mon Aug 16 15:20:13 2010
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef FIRST_H
#define FIRST_H

#include "graphic.h"
#include <qvariant.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qpoint.h>
#include <qthread.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;

class first : public QWidget
{
    Q_OBJECT

protected:
    QPushButton* user_b;
    QLabel* user_t;
    QPushButton* close;
    QTimer*   timer;

public:
   first( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
    ~first();
     void paintEvent(QPaintEvent *);

public slots:
    virtual void user_button();
};

class MyThread : public QThread
{
     public:
        virtual void run();
};

#endif // FIRST_H

创建线程的部分如红色所示,但在编译时总是提示有错误,错误提示是:
parse error before "{" token

其中错误提示中的“{”是指的public QThread下面一行的“{”
 楼主| 我爱linux 发表于 2010-8-26 16:28:14 | 显示全部楼层
我用的是QT2,是不是缺少什么配置呀,如果是怎么配置呀
回复

使用道具 举报

ahnushe 发表于 2010-8-27 18:08:07 | 显示全部楼层
为什么,构造函数没有呢?
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-8-30 09:38:18 | 显示全部楼层
没有构造函数,就等于是采用默认构造函数
回复

使用道具 举报

天嵌_support1 发表于 2010-8-30 11:25:19 | 显示全部楼层
你看过 这个类  “QThread”   的 构造函数没有

QThread::QThread ( QObject * parent = 0 )
回复

使用道具 举报

天嵌_support1 发表于 2010-8-30 11:26:02 | 显示全部楼层
本帖最后由 embedsky_lxt 于 2010-8-30 11:27 编辑

:Q  是  : + Q
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-8-31 10:11:22 | 显示全部楼层
class Q_EXPORT QThread : public Qt
{
    friend class QThreadPrivate;
public:
    static HANDLE currentThread();
    static void postEvent( QObject *,QEvent * );

    static void exit();

    QThread();    virtual ~QThread();

    // default argument causes thread to block indefinately
    bool wait( unsigned long time = ULONG_MAX );

    void start();

    bool finished() const;
    bool running() const;


protected:
    virtual void run() = 0;

    static void sleep( unsigned long );
    static void msleep( unsigned long );
    static void usleep( unsigned long );

private:
    QThreadPrivate * d;

#if defined(Q_DISABLE_COPY)
    QThread( const QThread & );
    QThread &operator=( const QThread & );
#endif
};

这就是QThread的类定义,里面红色不是就是不带参数的构造函数吗
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-8-31 10:23:58 | 显示全部楼层
并且QT帮助文档里面给的例子也是如此,没有任何参数:具体如下详细描述
QThread类提供了与系统无关的线程。

QThread代表在程序中一个单独的线程控制,在多任务操作系统中,它和同一进程中的其它线程共享数据,但运行起来就像一个单独的程序一样。它不是在main()中开始,QThread是在run()中开始运行的。你继承run()并且在其中包含你的代码。例如:


    class MyThread : public QThread {

    public:

        virtual void run();

    };

    void MyThread::run()
    {
        for( int count = 0; count < 20; count++ ) {
            sleep( 1 );
            qDebug( "Ping!" );
        }
    }

    int main()
    {
        MyThread a;
        MyThread b;
        a.start();
        b.start();
        a.wait();
        b.wait();
    }
回复

使用道具 举报

天嵌_support1 发表于 2010-8-31 11:11:00 | 显示全部楼层
8# 我爱linux


Detailed Description

The QThread class provides platform-independent threads.

A QThread represents a separate thread of control within the program; it shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system. Instead of starting in main(), QThreads begin executing in run(). By default, run() starts the event loop by calling exec() (see below). To create your own threads, subclass QThread and reimplement run(). For example:

class MyThread : public QThread
{
public:
     void run();
};

void MyThread::run()
{
     QTcpSocket socket;
     // connect QTcpSocket's signals somewhere meaningful
     ...
     socket.connectToHost(hostName, portNumber);
     exec();
}
This will create a QTcpSocket in the thread and then execute the thread's event loop. Use the start() method to begin execution. Execution ends when you return from run(), just as an application does when it leaves main(). QThread will notifiy you via a signal when the thread is started(), finished(), and terminated(), or you can use isFinished() and isRunning() to query the state of the thread. Use wait() to block until the thread has finished execution.
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-9-10 09:28:06 | 显示全部楼层
我脑子有点愚顿,还是不明白,斑竹能不否详细一点,或者说说怎么改法?多谢了!
回复

使用道具 举报

天嵌_support1 发表于 2010-9-10 11:42:52 | 显示全部楼层
本帖最后由 embedsky_lxt 于 2010-9-10 11:45 编辑

10# 我爱linux


翻译阿,你找个作翻译的就可以理解拉。
这上面的一段和你之前说的一样,只是多了一句:
在缺省情况下,run()通过调用 exec() 来执行你的线程任务....

这个例子创建了QTcpSocket线程并执行线程的任务。它利用start()来开始执行,
在run()执行完返回时结束,就和一般的程序执行完main() 后返回一样。
线程在 started() , finished() 和 terminated()执行流程中会通过信号方式来通知你它的当前状态。
你可以通过调用 isFinished() 和 isRunning()来得知它的执行状态。
你可以利用wait()方法来等待直到线程执行完成,返回。


以上是我的理解。
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-9-10 16:05:52 | 显示全部楼层
你可能理解错了,我的意思是说我如何做才能消除我上面的错误,请您指点一下,最好给一个可以运行的例子,谢谢
回复

使用道具 举报

 楼主| 我爱linux 发表于 2010-9-13 10:56:18 | 显示全部楼层
embedsky_lxt兄台怎么不回我的贴了,是不是我这个问题问的太笨了,请embedsky_lxt兄台帮帮忙了,究竟我怎么样做,才能正确创建好线程,不出现以上错误,最好能给个简单的例子,拜托了!小弟将非常感激
回复

使用道具 举报

天嵌_support1 发表于 2010-9-13 11:55:37 | 显示全部楼层
13# 我爱linux


mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;
};


#include<QThread>
class mythread : public QThread
{
public:
     void run();
};


#endif // MAINWINDOW_H

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void mythread::run()
{
   for( int count = 0; count < 10; count++ ) {
        sleep( 1 );
        qDebug( "thread runing %d!" ,count);
    }

}


main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
//#include "mythread.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    mythread c;
    mythread b;
    c.start();
    b.start();
    c.wait();
    b.wait();
    return a.exec();
}
回复

使用道具 举报

天嵌_support1 发表于 2010-9-13 11:56:33 | 显示全部楼层
:L======== : + L
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2025-6-20 02:54 , Processed in 2.060392 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表