我一直在尝试在我的Qt应用程序中整理自定义信号,而我似乎无法将其编译。基本上我有一个窗口,有一个槽来处理按钮按下(slotCalibrate()),另一个槽接收消息并通过QTextEdit(slotMessage())将它们输出到屏幕。这两个函数都可以正常工作。
这是Window.cpp,slotCalibrate()当前正在向Output类发送预制数据。在最终设计中,slotCalibrate()将在某些硬件上激活校准过程,其结果将是发送到Output.cpp的数据
#include "window.h"
#include "output.h"
#include <QPushButton>
#include <QTextEdit>
#include <QApplication>
#include <string>
using namespace std;
Window::Window(QWidget *parent) :
QWidget(parent)
{
setFixedSize(640, 480);
calButton = new QPushButton("Calibrate", this);
calButton->setGeometry(280, 20, 80, 30);
calButton->setToolTip("Press to zero connected transducers.");
connect(calButton, SIGNAL(clicked()), this, SLOT(slotCalibrate()));
quitButton = new QPushButton("Quit", this);
quitButton->setGeometry(550, 440, 80, 30);
connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
readout = new QTextEdit(this);
readout->setReadOnly(1);
readout->setPlainText("Testing, testing, 1, 2, 3.\n");
readout->setGeometry(10, 70, 620, 360);
}
void Window::slotCalibrate()
{
string sn[5] = {"1234", "2463", "7821", "5027", "5981"};
Output *op = new Output();
op->writeMessage(1, 5, sn);
//I also tried the following with exactly the same result
//Output op;
//op.writeMessage(1, 5, sn);
}
void Window::slotMessage(string message)
{
QString qstr = QString::fromStdString(message); //Convert from string to QString
readout->append(qstr); //Print message to screen
}
我正试图按下按钮来调用output.cpp的构造函数,然后调用函数writeMessage来构造并向window.cpp中的slotMessage发出一条消息
#include "output.h"
#include <string>
#include <sstream>
using namespace std;
Output::Output(QWidget *parent) :
QWidget(parent)
{
connect (this, SIGNAL(sendMessage(string)), parentWidget(), SLOT(slotMessage(string)));
}
//void sendMessage(string)
//{
//}
void writeMessage(int cat, int count, string sn[])
{
int i;
stringstream ss;
switch (cat)
{
case 1 : //Case where calibration of one or more nodes was successful
ss << count << " transducers were successfully calibrated.\n Their serial numbers are:";
for (i=0; i<cat; i++)
{
ss << "\n" << sn[i];
}
break;
case 2: //Case where calibration of one or more nodes failed
ss << "One or more transducers failed to calibrate.";
break;
case 3: //Case where no config file is found
ss << "WARNING! Could not find 'params.config'. The default values will be used for calibration.";
break;
}
emit sendMessage(ss.str());
}
不幸的是,由于代码完全像这样,编译器对我大喊大叫。它说:
'sendMessage' was not declared in this scope
我已在头文件中声明sendMessage
作为信号,而我的印象是信号不需要在代码中实现。
尽管如此,我决定尝试实现一个名为sendMessage
的空函数。这摆脱了编译器错误,但引入了另一个错误。在window.cpp中调用op.writeMessage()
时,我收到错误:
“未定义的引用`Output :: writeMessage(int,int,std :: string *)'”
我也试过在Output构造函数中调用writeMessage
,我得到了同样的错误。
我彻底迷失了,并且已经在这个问题上工作了几天,所以任何帮助都会受到极大的赞赏。
为了完整性,这里分别是头文件window.h和output.h:
window.h中
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <string>
class QPushButton;
class QTextEdit;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QTextEdit *readout;
private:
QPushButton *calButton;
QPushButton *quitButton;
signals:
public slots:
void slotCalibrate();
private slots:
void slotMessage(std::string);
};
#endif // WINDOW_H
output.h
#ifndef OUTPUT_H
#define OUTPUT_H
#include <QWidget>
#include <QObject>
#include <string>
class Output : public QWidget
{
Q_OBJECT
public:
explicit Output(QWidget *parent = 0);
void writeMessage(int, int, std::string[]);
signals:
void sendMessage(std::string);
public slots:
};
#endif // OUTPUT_H
答案 0 :(得分:2)
当然这不起作用。您定义了writeMessage()函数,但您将其定义为全局函数。你必须添加&#34;输出::&#34;定义。