我使用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连接,并执行slot 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
答案 0 :(得分:2)
你调用connect()是错误的。它应该是:
connect(ui->lineEdit_4, SIGNAL(textChanged(QString)),
ui->widget_diagram2, SLOT(upDateDatas(QString)));
注意内部连接你应该传递类型而不是变量名。常量引用const type &
没问题,但理想情况下应该省略type
- 它可以节省输入,而且connect
内部也可以。