我一直在尝试在 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”未在此范围内声明
我已在头文件中将 sendMessage
声明为信号,并且我认为信号不需要在代码中实现。
尽管如此,我还是决定尝试实现一个名为 sendMessage
的空函数。这消除了编译器错误,但引入了另一个错误。在 window.cpp 中调用 op.writeMessage()
时,出现错误:
“对‘Output::writeMessage(int, int, std::string*)’的 undefined reference ”
我还尝试在输出构造函数内调用writeMessage
,但出现了相同的错误。
我完全迷失了,并且已经在这个问题上工作了几天,所以任何帮助将不胜感激。
为了完整起见,这里分别是头文件 window.h 和 output.h:
窗口.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
输出.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
最佳答案
当然这是行不通的。您定义了 writeMessage() 函数,但将其定义为全局函数。您必须在定义前添加“Output::”。
关于c++ - 信号未在此范围内声明/未定义对 class::method 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571629/