(C++) Object::connect: No such slot QDialog::my\_slot()
February 24, 2017 · View on GitHub
(C++) Object::connect: No such slot QDialog::my_slot()
Full warning message
Object::connect: No such slot QDialog::buttonClicked() in ../MyFolder/main.cpp:40
Cause
Operating system: Ubuntu
IDE: Qt Creator 2.0.0
Project type: Qt4 GUI Application
Libraries used:
Qt project file
#------------------------------------------------- # # Project created by QtCreator 2010-07-22T12:56:24 # #------------------------------------------------- QT += core gui TARGET = CppQtExample11 TEMPLATE = app SOURCES += main.cpp HEADERS += \ MyDialog.h
Source code
main.cpp
#include <boost/scoped_ptr.hpp> #include <QtGui/QApplication> #include <QDialog> #include <QPushButton> #include <QVBoxLayout> #include "MyDialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); boost::scoped_ptr<MyDialog> dialog(new MyDialog); boost::scoped_ptr<QVBoxLayout> layout(new QVBoxLayout); boost::scoped_ptr<QPushButton> button1(new QPushButton); button1->connect( button1.get(),SIGNAL(clicked()), dialog.get(),SLOT(buttonClicked())); button2->setText("Quit"); layout->addWidget(button1.get()); dialog->setLayout(layout.get()); dialog->show(); return a.exec(); }
MyDialog.h
#ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class MyDialog : public QDialog { public slots: void buttonClicked() { } }; #endif // MYDIALOG_H
Solution
Add the Q_OBJECT at the beginning of the MyDialog class declaration.
#ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class MyDialog : public QDialog { Q_OBJECT public slots: void buttonClicked() { } }; #endif // MYDIALOG_H
Note that a complete example of the corrected version of this compile warning can be found at Qt example 12: creating a custom QDialog with slot.