[英]QT C++ - Signals and Slots: “No such slot QLabel…” even the SLOT function is existing in my class
So I am trying to learn Qt Framework with C++. 因此,我尝试使用C ++学习Qt Framework。 I am in the middle of understanding signals and slots but I am having a hard time creating a customized slot. 我正在理解信号和插槽,但是很难创建自定义的插槽。 So as I follow some tutorials, my program ended up with the following error: 因此,当我按照一些教程进行学习时,我的程序最终出现以下错误:
QObject::connect: No such slot QLabel::updateMessage() in main.cpp:28 QObject :: connect:main.cpp:28中没有这样的插槽QLabel :: updateMessage()
Where updateMessage() is declared as a public slot inside my Test class 其中updateMessage()在我的Test类中声明为公共插槽
The following are some snippets from my code: 以下是我的代码中的一些片段:
Test.h 测试
class Test : public QObject
{
Q_OBJECT
public:
Test(void);
~Test(void);
void setMessage(char *tMsg);
char* getMessage();
QWidget *window;
QGridLayout *layout;
QLabel *lblMsg;
QPushButton *btnShow;
public slots:
void updateMessage();
private:
char msg[80];
QString str;
};
main.cpp main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Test t;
t.window->setWindowTitle("Testing Qt");
t.window->setLayout(t.layout);
t.window->show();
return app.exec();
}
Test::Test(void)
{
window = new QWidget;
lblMsg = new QLabel;
btnShow = new QPushButton("Show message");
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
layout = new QGridLayout;
layout->addWidget(lblMsg);
layout->addWidget(btnShow);
char str1[] = "Hello, Qt World!";
setMessage(str1);
}
Test::~Test(void)
{
}
void Test::setMessage(char *tMsg)
{
memcpy(msg, tMsg, sizeof(msg));
}
char* Test::getMessage()
{
return msg;
}
void Test::updateMessage()
{
string strMsg(getMessage());
QString qstr = QString::fromStdString(strMsg);
lblMsg->setText(qstr);
delete msg;
}
HelloQtWorld.pro HelloQtWorld.pro
######################################################################
# Automatically generated by qmake (3.0) Tue Nov 15 00:30:22 2016
######################################################################
TEMPLATE = app
TARGET = HelloQtWorld
INCLUDEPATH += .
# Input
HEADERS += stdafx.h Test.h
SOURCES += anotherClass.cpp \
main.cpp \
stdafx.cpp \
GeneratedFiles/qrc_helloqtworld.cpp
RESOURCES += helloqtworld.qrc
Can also someone try to explain for me how Signals and Slots work? 也有人可以为我解释信号和插槽的工作原理吗? Thank you in advance. 先感谢您。 :) :)
The problem is in this line: 问题在这一行:
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
You are connecting with slot of QLabel
instead of slot of your class. 您正在使用QLabel
插槽而不是您的班级插槽进行连接。
This should be changed to 这应该更改为
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));
Pay attention at new Qt 5 syntax for signals and slots. 请注意信号和插槽的新Qt 5语法。
Your label
of type QLabel
does not have a slot named updateMessage()
. QLabel
类型的label
没有名为updateMessage()
的插槽。 Your class Test
has. 您的班级Test
有。
Change the connect to: 将连接更改为:
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));
This will connect the button's clicked
signal to your Test
object s
updateMessage` slot. 这会将按钮的clicked
信号连接到Test
对象s
updateMessage插槽。
Your slot is declared at Test
class, not QLabel
class, but you are trying to connect to the slot as if it was declared at the QLabel
: 您的插槽是在Test
类而不是QLabel
类中声明的,但是您试图连接到该插槽,就像在QLabel
中声明的那样:
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
You should connecto to this
, instead of lblMsg
: 您应该连接到this
,而不是lblMsg
:
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.