QObject :: connect a non-connecting signal to the slot

I use C ++ and Qt in my project, and my problem is that the QObject :: connect function does not connect the signal to the slot. I have the following classes:

class AddCommentDialog : public QDialog { Q_OBJECT public: ...some functions signals: void snippetAdded(); private slots: void on_buttonEkle_clicked(); private: Ui::AddCommentDialog *ui; QString snippet; }; 

Part of my main window:

 class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void commentAddedSlot(); void variableAddedSlot(); ... private: AddCommentDialog *addCommentDialog; ... }; 

Ant last dialogue;

 class AddDegiskenDialog : public QDialog { Q_OBJECT public: ... signals: void variableAdded(); private slots: void on_buttonEkle_clicked(); private: Ui::AddDegiskenDialog *ui; ... }; 

In the main window designer, I connect signals and slots:

 addCommentDialog=new AddCommentDialog(); addDegiskenDialog=new AddDegiskenDialog(); connect(addDegiskenDialog, SIGNAL(variableAdded()), this, SLOT(variableAddedSlot())); connect(addCommentDialog, SIGNAL(snippetAdded()), this, SLOT(commentAddedSlot())); 

Point - my commentAddedSlot connected to this signal successfully, but commentAddedSlot failed. There are macros Q_OBJECT, no warnings, for example, about slot x x. In addition to this, receivers (SIGNAL (snippetAdded ())) gives me 1, but receivers (SIGNAL (snippetAdded ())) gives me 0 and I used qmake -project; qmake and fully compile. What am I missing?

+4
source share
1 answer

A quick look at your code gives no idea what your problem is.

But here are a few points:

  • As Mike said here : for connection problems, always check the console for connection failure messages. Since Qt cannot determine if the connection makes sense before runtime, it notifies you of failures. You might think this would work, but it just speaks quietly about it in the console. With Qt, it makes sense to watch the console always. Qt prints all kinds of error messages that may help in the event of a problem.
  • You can control the result of the connect function, therefore (from the official documentation)

    The function returns true if it successfully connects the signal to the slot. It will return false if it cannot create a connection, for example, if QObject cannot check for the presence of any signal or method, or if their signatures are incompatible.

  • Check if your objects (dialogs) are well created and the pointers are not NULL .

  • Try to clear the project (the "Clear Project" command in QtCreator), even manually delete all ui_* , moc_* . And then recompile it.

Good luck And please write to us.

+3
source

All Articles