• 3805阅读
  • 6回复

[讨论]程序运行报错No such slot MainWindow::recvTime [复制链接]

上一主题 下一主题
离线phyond
 

只看楼主 倒序阅读 楼主  发表于: 2017-05-31
一个主窗口,另外启动了一个线程,并在这个线程里发送一个信号,主窗口的lineEdit设置一个槽接收这个信号里的字符串并显示
运行时报错:


QObject::connect: No such slot MainWindow::recvTime(const QString time) in ../processTest1/mainwindow.cpp:11
QObject::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;
}


离线phyond

只看该作者 1楼 发表于: 2017-05-31
哪位老师指点一下?
离线chunhuixihui

只看该作者 2楼 发表于: 2017-05-31
connect(tThread,SIGNAL(sendtime(QString)), this,SLOT(recvTime(const QString time)), Qt::QueuedConnection);写错了啊
1条评分金钱+1
phyond 金钱 +1 - 2017-06-01
离线never_forget

只看该作者 3楼 发表于: 2017-05-31
猜测跟 const 有关系,孤陋寡闻的我,看不出来其他的毛病~~~
离线phyond

只看该作者 4楼 发表于: 2017-06-01
回 chunhuixihui 的帖子
chunhuixihui:connect(tThread,SIGNAL(sendtime(QString)), this,SLOT(recvTime(const QString time)), Qt::QueuedConnection);写错了啊 (2017-05-31 14:26) 

昨天最后确实发现是这个地方错了,不应该有 time
但是不明白原因,这是为什么呢,我定义的函数也是有形参time的
离线michico2000

只看该作者 5楼 发表于: 2017-06-01
这有啥不明白的  connectl连接时不需要带参数名字 只要写参数类型就可以  你把参数名写上它会以为你这个类型未定义呢
离线phyond

只看该作者 6楼 发表于: 2017-06-01
回 michico2000 的帖子
michico2000:这有啥不明白的  connectl连接时不需要带参数名字 只要写参数类型就可以  你把参数名写上它会以为你这个类型未定义呢 (2017-06-01 14:38) 

好吧
快速回复
限100 字节
 
上一个 下一个