【问题标题】:SIGNAL fail when debug started调试开始时信号失败
【发布时间】:2014-03-11 22:17:38
【问题描述】:

我在使用 Qt5 的连接时遇到问题。 我在这里发布了一些代码以快速查看:

void MainWindow::onMessage(const Message* message) {


        try {
            const TextMessage* textMessage = dynamic_cast<const TextMessage*> (message);
            std::string text = "";




        if (textMessage != NULL) {
                text = textMessage->getText();

            } else {
                text = "NOT A TEXTMESSAGE!"; 
            }

            int fieldIndex=message->getIntProperty("field");
            QString qstr = QString::fromStdString(text);



            switch(fieldIndex)
            {

            case 0:ui->lineEdit->setText(qstr);break;
            case 1:ui->lineEdit_2->setText(qstr);break;
            case 2:ui->lineEdit_3->setText(qstr);break;
            case 3:ui->lineEdit_4->setText(qstr);break;
            case 4:ui->lineEdit_5->setText(qstr);break;
            case 5:ui->lineEdit_6->setText(qstr);break;
            case 6:ui->lineEdit_7->setText(qstr);break;
            case 7:ui->lineEdit_8->setText(qstr);break;
            case 8:ui->lineEdit_9->setText(qstr);break;
            }




            connect(ui->lineEdit_4,SIGNAL(textChanged(qstr)),ui->widget_diagram2,SLOT(upDateDatas(qstr)));

            }catch (CMSException& e) {
            e.printStackTrace();
        }

}

如您所见,我正在尝试将 lineEdit_4 生成的信号 textChanged 与 ui 对象 widget_diagram2 连接并执行插槽 upDateDatas(qstr)。但是发生了一些不好的事情,因为我收到了这条消息:

QObject::connect: No such signal QLineEdit::textChanged(qstr) in mainwindow.cpp:97
QObject::connect:  (sender name:   'lineEdit_4')
QObject::connect:  (receiver name: 'widget_diagram2')

我个人不知道为什么....错误在哪里?

void MainWindow::upDateDatas(QString qstr){

 bool ok;
 double value0=qstr.toDouble(&ok); 
 double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
  static double lastPointKey = 0;
   if (key-lastPointKey > 0.01) // at most add point every 10 ms
   {

     ui->widget_diagram2->graph(0)->addData(key, value0);
     ui->widget_diagram2->graph(0)->removeDataBefore(key-8);
     ui->widget_diagram2->graph(0)->rescaleValueAxis();
     lastPointKey = key;
   }
  ui->widget_diagram2->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
  //ui->widget_diagram2->replot();
}

这是错误:

QObject::connect: No such slot QCustomPlot::upDateDatas(QString) in mainwindow.cpp:97
QObject::connect:  (sender name:   'lineEdit_4')
QObject::connect:  (receiver name: 'widget_diagram2')

.h 文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/System.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include "qcustomplot.h"
#include "IfacomAmqReceiver.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow, public MessageListener
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void connectionReceiver();
    void onMessage(const Message*);
    void setupDiagram();
    IfacomAmqReceiver* m_IfacomAmqListener;


private:
    Ui::MainWindow *ui;


private slots:

   void upDateDatas(QString);
};

#endif // MAINWINDOW_H

【问题讨论】:

  • 你为什么在每条消息中重复连接行编辑的textChanged信号?连接应该按照evilruff 的答案完成,但只在MainWindow 构造函数中完成一次。
  • Connect to a SLOT in Qt 的可能重复项
  • 为什么将格式编辑恢复为可怕的空白?
  • @Kuba Ober 为什么你不帮我解决我的问题而不是考虑格式?

标签: c++ qt qt5 signals-slots


【解决方案1】:

你调用 connect() 是错误的。应该是:

connect(ui->lineEdit_4, SIGNAL(textChanged(QString)),
        ui->widget_diagram2, SLOT(upDateDatas(QString)));

请注意,在 connect 内部,您应该传递类型而不是变量名称。常量引用 const type &amp; 是可以的,但理想情况下应该省略以产生 type - 它可以节省输入,并且无论如何 connect 内部都是这样做的。

【讨论】:

  • 规范化的信号和槽签名省略了 const 引用。已编辑。
  • @evilruff 按照您的建议,我现在遇到了新问题,但为了了解更多信息,我发布了在我的个人插槽中实现的功能......所以请查看已编辑的新版本。
  • 你能在 MainWindow.h 声明的地方发布标题你确定 ui->widget_diagram2 是 MainWindow 实例吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多