mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void newThread();
private:
Ui::MainWindow *ui;
public slots:
void a(int);
void b();
};
#endif // MAINWINDOW_H
myThread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QApplication>
class myThread:public QThread
{
public:
myThread();
protected:
void run();
signals:
void aaa(int);
void bbb();
};
void myThread::run()
{
while(true){
emit aaa(1);
}
}
myThread::myThread ()
{
}
#endif // MYTHREAD_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myThread.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myThread* tmp = new myThread;
QObject::connect(tmp,SIGNAL(aaa(int)),this,SLOT(a(int)));
tmp->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::a(int)
{
ui->textBrowser->append("q");
}
这样写有什么错吗?实在不解啊。。编译都过不去
------解决方案--------------------
myThread类也要有Q_OBJECT,才能使用信号槽
------解决方案--------------------
自定义的线程类加上 Q_OBJECT
------解决方案--------------------
http://blog.csdn.net/yuzhiyuxia/article/details/7844595