无法找到退出槽,并使用 Q_OBJECT 宏
我正在使用带有 QT 4.7.1 和插件的 VS2008。我是这个环境的新手。
我设法进行必要的设置并运行简单的“hello world”。但是当我尝试使用简单的 单击按钮时 quit()
槽,我失败了。尝试使用 Q_OBJECT
时也会导致构建失败。
注释 Q_OBJECT
代码后,将构建并调试代码。现在它显示
QObject::connect : no such slot QWidget::quit() in .\main.cpp find.
下面是我通过网络搜索的代码
#include <QtGui>
#include "QtGui\QApplication"
#include "QObject"
class Notepad : public QWidget
{
//Q_OBJECT
public:
Notepad();
private slots:
void quit();
private:
QTextEdit *textEdit;
QPushButton *quitButton;
};
Notepad::Notepad()
{
textEdit = new QTextEdit;
quitButton = new QPushButton(tr("Quit"));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit() ));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Notepad"));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad nt;// = new Notepad();
nt.show();
return a.exec();
}
,但未能获得合理的解决方案。大多数解决方案都是在命令行上使用 qmake。 我还可以找到该项目的 .pro 文件。
任何帮助表示赞赏。
I am using VS2008 with QT 4.7.1 and add-ins. I am new to this environment.
I managed to do necessary setting and run simple "hello world". But when I try to use simplequit()
slot on click of a button, I failed. Also it results in build failed when trying to use Q_OBJECT
.
After commenting Q_OBJECT
code is built and debugged. Now it shows
QObject::connect : no such slot QWidget::quit() in .\main.cpp found.
below is my code
#include <QtGui>
#include "QtGui\QApplication"
#include "QObject"
class Notepad : public QWidget
{
//Q_OBJECT
public:
Notepad();
private slots:
void quit();
private:
QTextEdit *textEdit;
QPushButton *quitButton;
};
Notepad::Notepad()
{
textEdit = new QTextEdit;
quitButton = new QPushButton(tr("Quit"));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit() ));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Notepad"));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad nt;// = new Notepad();
nt.show();
return a.exec();
}
I have searched through net but failed to get reasonable solution. Most of the solution are for working with qmake on command line.
Also I am able to find .pro file for the project.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Nitesh:您需要 Q_OBJECT 宏才能使插槽正常工作,MOC 将包含 Q_OBJECT 的每个标头编译为 moc_*.cpp 文件。将 moc*.cpp 添加到您的项目中,一切都会正常工作。未解析的外部意味着您缺少该函数的定义,您是否在任何地方定义了它?
Nitesh: You need Q_OBJECT macro for the slots to work properly, the MOC compiles every header that contains Q_OBJECT into moc_*.cpp file. Add that moc*.cpp to your project and everything should work fine. The unresolved external means that you are missing the definition of the function, did you define it anywhere?
将记事本的声明移至标头(例如 notepad.h),重新启用 Q_OBJECT,然后添加到您的 .pro 文件中:
重新运行 qmake,然后它应该可以工作。
Move the declaration of Notepad to a header (say, notepad.h), reenable the Q_OBJECT, then add to your .pro file:
Rerun qmake, then it should work.