如果我尝试直接使用 quit() 方法,它会完美编译,但是在运行时会出现错误消息“Object::connect: No such slot myClass::quit()”。所以要避免这种情况,有什么办法吗?通过使用方法 quitPicture()(defined as slot),应用程序运行良好。这是唯一的解决方案吗?
myClass::myClass(QWidget *parent)
: QWidget(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle(QObject::tr("Class"));
QPushButton *quitButton = new QPushButton("&Quit");
// QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quit())); //showing run time error
QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quitPicture())); //working perfectly
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(this);
layout->addWidget(quitButton);
window->setLayout(layout);
window->show();
}
void myClass::quitPicture()
{
std::cout << "calling quitPicture" << std::endl;
QApplication::quit();
}