一个主
窗口,另外启动了一个
线程,并在这个线程里发送一个信号,主窗口的lineEdit设置一个槽接收这个信号里的字符串并
显示运行时报错:
QObject::connect: No such slot MainWindow::recvTime(const QString time) in ../processTest1/mainwindow.cpp:11QObject::connect: (receiver name: 'MainWindow')
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "stimethread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
timeThread *tThread;
public slots:
void recvTime(const QString time);
};
#endif // MAINWINDOW_H
线程头文件stimethread.h
#ifndef TIMETHREAD_H
#define TIMETHREAD_H
#include <QThread>
#include "datalib.h"
class timeThread:public QThread
{
Q_OBJECT
public:
explicit timeThread(QObject *parent=0);
~timeThread();
void stop();
protected:
void run();
private:
volatile bool stopped;
signals:
void sendtime(const QString);
};
#endif // TIMETHREAD_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->dateTimeEdit->setDisplayFormat("yyyy年MM月dd日 HH:mm:ss");
tThread=new timeThread;
connect(tThread,SIGNAL(sendtime(QString)), this,SLOT(recvTime(const QString time)), Qt::QueuedConnection);
tThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::recvTime(const QString time)
{
//QDateTime data;
//data.toString("2017 05");
//ui->dateTimeEdit->setDateTime((QDateTime)time);
ui->lineEdit->setText(time);
}
线程源
文件stimethread.cpp
#include "stimethread.h"
#include <QThread>
#include <QDateTime>
timeThread::timeThread(QObject *parent):
QThread(parent)
{
stopped=false;
}
timeThread::~timeThread()
{
wait();
}
void timeThread::run()
{
qint32 i;
QDateTime nowTime;
QString timestr;
while(!stopped)
{
msleep(1000);
nowTime=QDateTime::currentDateTime();
timestr=nowTime.toString("yyyy年MM月dd日 hh:mm:ss");
emit sendtime(timestr);
//str = csv.section(',', -3, -2);
}
}
void timeThread::stop()
{
stopped=true;
}