我想通过下面的代码 实现线程的线程中调用窗体 但是发送的信号好像不执行,有兴趣的可以执行下。谢谢了 帮帮忙?
main.cpp
#include <QtGui/QApplication>
#include <QTextCodec>
#include <string.h>
#include "widget.h"
#include "thread.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Thread t;
t.start();
return a.exec();
}
thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include <QTimer>
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
virtual void run();
protected slots:
void Display();
private:
QTimer *m_pTimer;
};
#endif // THREAD_H
thread.cpp
#include "thread.h"
#include "widget.h"
Thread::Thread()
{
}
void Thread::run()
{
qDebug("if");
m_pTimer = new QTimer();
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(Display()));
m_pTimer->start(15);
}
void Thread::Display()
{
qDebug("begin");
Widget w;
w.show();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QtGui/QLabel>
#include <QApplication>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget();
};
#endif // WIDGET_H
widget.cpp
#include <QtGui/QHBoxLayout>
#include <QDesktopWidget>
#include <QMoveEvent>
#include "widget.h"
Widget::Widget()
{
//初始化窗体
setWindowFlags(Qt::Dialog); //窗体没有最大化最小化按钮
setFixedSize(250, 100); //设置窗体的大小
setWindowTitle(tr("信息提示")); //设置QWidget抬头信息
//最前端显示
Qt::WindowFlags flags = Qt::Widget;
flags |= Qt::WindowStaysOnTopHint;
setWindowFlags(flags);
}
[ 此帖被wanglei2258在2009-08-30 14:41重新编辑 ]