-1

I have a MainWindow class which is declared in mainwindow.h and defined in mainwindow.cpp respectively like this:

In mainwindow.h:

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ...
    void addNewTab(QString fullFilePath, QString textString="");

public slots:
    void disableMenuItem();
    ...

private:
    ...
};

In mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ...
    connect(this, &MainWindow::addNewTab, this, &MainWindow::disableMenuItem); 
    ...
}

void MainWindow::addNewTab(QString fullFilePath, QString textString)
{
    ...
}

void MainWindow::disableMenuItem()
{
   ...
}

Everything compiles and run fine except for the following message on the console:

QObject::connect: signal not found in MainWindow

The message come from the connect call in the constructor above. What does that message mean in my case, and where am I doing wrong?

Amani
  • 16,245
  • 29
  • 103
  • 153

1 Answers1

3

As drescherjm and Learner mentioned, you forgot to add a signals: section to your header file, and declare your signal within it.

Qt connects signals to slots at runtime, not at compile time, so mis-connected signals and slots cannot be detected until the program is actually run; that's why this problem is reported when it is.

Qt uses the moc preprocessor to turn signals and slots into standard c++, so that's why the signals: and slots: sections of your header will not cause problems when compiling.

Signals are fully defined by moc, so you do not need to define them in your .cpp file, but they still need to be in the header so moc knows to create them.

EDIT: It appears that you are trying to use a signal with the name of one of your class functions. I don't think that's going to work. the documentation for the new signal/slot syntax indicates that you can connect TO anything, it doesn't have to be a slot, but I believe you still need to define your signal as a signal.

stonecrusher
  • 1,246
  • 10
  • 12
  • I tried to declare "signals:" above the addNewTab() function and got the following error message during compilation: multiple definition of `MainWindow::addNewTab(QString, QString)' – Amani Jun 03 '16 at 17:14
  • @Amani see my edit: you'll need to use a signal name that does not clash with an already defined function name. The signal name has to be unique - think of it like a totally separate function, one that is called by `emit functionName()`, and ends up calling whatever you've connected to it. – stonecrusher Jun 03 '16 at 17:20
  • also note that [the emit is not necessary](http://stackoverflow.com/questions/10160476/using-emit-vs-calling-a-signal-as-if-its-a-regular-function-in-qt), but I would encourage its use for clarity. – stonecrusher Jun 03 '16 at 17:21
  • @Amani since you say "everything runs fine", I suspect that you don't really need to use connect, or signals/slots at all in this instance. It sounds like, inside your *addNewTab* function, you are directly calling *disableMenuItem*. If that's true, you do not need to have the `connect` line at all, nor the signal, nor define *disableMenuItem* as a slot. – stonecrusher Jun 03 '16 at 17:27
  • If you wish to use signals/slots, you would instead call `emit functionName()` after you have connected *functionName* to *disableMenuItem*. I find this most useful within a single class if I only want the receiving slot to be called in certain situations, and then I can connect and disconnect the signal from the slot as needed. – stonecrusher Jun 03 '16 at 17:30