Solved SLOT connection problem
-
Hi
I d'like to run external *.exe files in my program.
There is one error:
QObject::connect: No such slot QWidget::runCleaner() in ..\Tests\arcMainWindow.cpp:11
But the slot is there.When I run the programm, the window opens, and of course, nothing happens when the button is pushed.
Here is the code:
QT += widgets SOURCES += \ main.cpp \ arcMainWindow.cpp HEADERS += \ arcMainWindow.h
arcMainWindow.h
#ifndef MYWINDOW_H #define MYWINDOW_H #include <QApplication> #include <QWidget> #include <QPushButton> #include <QProcess> #include <QObject> class arcMainWindow : public QWidget // On hérite de QWidget { public: arcMainWindow(); public slots: void runCleaner(); private: QPushButton *m_btn; }; #endif // MYWINDOW_H
main.cpp
#include "arcMainWindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); arcMainWindow mainWindow; mainWindow.show(); return app.exec(); }
arcMainWindow.cpp
#include "arcMainWindow.h" arcMainWindow::arcMainWindow() : QWidget() { setFixedSize(300, 150); m_btn = new QPushButton("CCleaner", this); m_btn->setFont(QFont("Comic Sans MS", 14)); m_btn->setCursor(Qt::PointingHandCursor); m_btn->move(60, 50); QObject::connect(m_btn, SIGNAL(clicked()), this, SLOT(runCleaner())); } void arcMainWindow::runCleaner() { QProcess *myProcess = new QProcess(); myProcess->start( "\"C:\\Program Files\\CCleaner\\CCleaner.exe\"" ); }
Any ideas??
Thanks in advance =)
-
Hi.
You are missing Q_OBJECT macro in the
arcMainWindow.h
.
The Q_OBJECT is required to use Signal & Slots.like this:
class arcMainWindow : public QWidget // On hérite de QWidget { Q_OBJECT public: arcMainWindow();
-
This is it!
At the begining I had the Q_OBJECT macro in the arcMainWindow.h file, but I had a lot of errors.
Certainly because I forgot to run qmake...?I spent hours on that... :-)
Merci beaucoup
-
Well, maybe it is a stupid question, but I try to understand...
My misstake was to forget to add Q_OBJECT macro in the arcMainWindow.h and to run qmake. OK
I cleared the Q_OBJECT macro to get back the "no such SLOT" error.
Then I added QT += core to the .pro file and Q_OBJECT to the .h file and it seems also to work .What difference does this make?
-
Even if you add
Q_OBJECT
to thearcMainWindow.h
,qmake
will not run automatically when you build it.
The qmake will run automatically upon build when the .pro file is modified. -
Okay. Thanks.