我是qt的新手,想要创建一个发送整数信号的线程,并且我有一个接收信号的主窗口,并且我确信信号按照我用cout看到它,但是当我想将接收到的号码发送到置于ui中的spinbox(或标签)时,主线程被卡住并且不显示任何内容。Qt信号已收到,但UI没有显示出来
这里是我的线程类:螺纹
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QtCore>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
bool Stop;
signals:
void NumberChanged(int);
public slots:
};
#endif // MYTHREAD_H
CPP文件:
#include "mythread.h"
#include <QtCore>
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}
void MyThread::run()
{
for(int i=0;i<2000000;i++){
QMutex m;
emit NumberChanged(i);
QThread::msleep(100);
}
}
mainwindow.h:
终于mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mthread = new MyThread(this);
connect(mthread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
mthread->property(Q)
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onNumberChanged(int num)
{
cout << num << endl;
ui->spinBox->setValue(num);
}
void MainWindow::on_pushButton_clicked()
{
mthread->run();
}
当我运行这个,整数值是由cout显示,但spinbox或标签不会改变,直到数量达到2000000.什么是错的?
预先感谢您!它解决了我的问题:) –