QT: no such slot

The problem is that I always get a "No such slot" error in Qt Creator every time I launch the "Settings" window from my main window. I found that Qt is still pretty intuitive, and this concept of signals in the "n" slots seems a bit stretched from just passing through vars or function calls. Basically, I have a menu with the settings parameter, which, when clicked, opens the settings window, which you need to capture double from the user and update var in the main window.

SettingsWindow.h

class SettingsWindow : public QWidget { Q_OBJECT public: SettingsWindow(QWidget *parent = 0); signals: void ValChanged(double newVal); public slots: void Accept(); private: QLineEdit *le1; }; 

There is an accept button in the settings window that calls Accept (), which emits a ValChanged signal with newVal set as user input in le1 as double.

SettingsWindow.cpp

 void SettingsWindow::Accept(){ emit ValChanged(le1->text().toDouble()); this->close(); } 

This settings window is called up by the main application window: MainWindow

mainwindow.cpp

 class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); public slots: void SetVal(double x); private slots: void NewWindow(); private: double theVal; }; 

This main window has a menu from which you can select settings. This will create a new window with a field for entering the number.

mainwindow.cpp

 void MainWindow::NewWindow() { SettingsWindow *MySettings=new SettingsWindow(this); QObject::connect(MySettings, SIGNAL(ValChanged(double)), this, SLOT(SetVal(double))); MySettings->show(); MySettings->raise(); } void MainWindow::SetVal(double x){ theVal = x; } 

I hope that when the settings window opens, the user can enter val in the field, which then emits a ValChanged Signal, which sets the Val value to the value specified by the user. Most of the time I saw a problem with people, not including the Q_OBJECT macro, but I turned it on both times. Any suggestions on why this is not working?

+4
source share
4 answers

The problem you are facing is almost certainly due to the fact that the moc file is not recreated, a typo in your call to connect, or a typo in the declaration of the corresponding slot.

Perhaps you should think that this is much more than necessary to get input from a dialog box. A simpler method would be to connect the signal with the Accept button pressed to the slot in the main window, and then get the desired value directly from the settings window instance using the getXXX() method.

If in the end you have a settings dialog with a large number of values, instead of receiving each value through getters, the signal of the "Accept" button returns a structure with all the values ​​as fields of this structure.

I should mention that it looks like NewWindow() every time it is called, it creates a new instance of SettingsWindow . All these instances will remain until MainWindow is destroyed.

+3
source

For me, adding public Q_SLOTS: above function of my slot was what I was missing. (I already had Q_OBJECT, etc.)

+7
source

I solved my problem when I manually updated the moc file on the command line.

I used the qt command line option, so all paths were set:

 cd /path/to/my/project moc -o moc_myheaderfile.cpp myheaderfile.h 

There was nothing wrong with my code, and my makefile did not have any moc command that I could see. This works for all my examples that I have tried. Hope someone tries to do it too. I experimented for nearly 85 hours before I could find the reason.

For code block users, try re-creating your moc files to be precise. the -o myheaderfile.cpp parameter is intended to save the moc output to a file. Running moc in a file actually displays everything on the console window.

+1
source

I was trying incorrectly to pass a parameter to my slot without QSignalMapper , which I learned from this SO post.

Removing all parameters of the slot function (.h and .cpp) allowed us to find and call a callback.

Yes, I am Qt n00b. Time to refactor with QSignalMapper :)

NTN

0
source

All Articles