5
votes

I am using C++ and Qt in my project and my problem is QObject::connect function doesn't connect signal to a 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;
};

A 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 the last dialog;

class AddDegiskenDialog : public QDialog
{
    Q_OBJECT

public:
    ...

signals:
    void variableAdded();

private slots:
    void on_buttonEkle_clicked();

private:
    Ui::AddDegiskenDialog *ui;
    ...
};

In the main window constructor 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()));

The point is my commentAddedSlot is connected to it's signal successfully, but commentAddedSlot is failed. There is the Q_OBJECT macros, no warning such as about no x slot. In addition to this, receivers(SIGNAL(snippetAdded())) gives me 1 but receivers(SIGNAL(variableAdded())) gives me 0 and i used commands qmake -project; qmake and make to fully compile. What am i missing?

1
Have you tried clicking on Build -> Run qmake (in Qt Creator) and cleaning, rebuilding your project? Does your Output Window show something?Daniel Castro
yes, tried it, but didn't solve. I am printing with qDebug() in slots but they aren't called by signal.Barış Akkurt
I just used your code and the connections work fine. There must be something else going on. Are you later re-initializing addDegiskenDialog perhaps?Dave Mateer

1 Answers

3
votes

Quick look at your code gives no ideas what is your problem.

But, here are some moments:

  1. As Mike said here: With connection problems, always make sure that you check the console for messages about connect failures. Since Qt can't tell if a connection makes sense until runtime, it notifies you of failures there. You'd think it would crash, but it just quietly says these things in the console. With Qt, it makes sense to watch the console always. Qt prints out all sorts of error messages that can help when you've got a problem.
  2. You can control result of connect function, so (from official documentation)

    The function returns true if it successfully connects the signal to the slot. It will return false if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren't compatible.

  3. Check if your objects (dialogs) creates well and pointers is not equal to NULL.

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

Good luck! And, please, give us feedback.