Emiting signal does not work



  • Hello. I have got into some problem so I hope you can help me. I have made a simple class where I have set signal to be emited, and a if statement where is checked for condition. If condition is true signal is emited.

    Problem is that I run the program it always does the code inside the condition, but it doesn't emits the signal.

    I have tried with diffrent variations inside the signal and inside connect statment, but it simply doesn't work.

    Here is the code of if statment:
    @
    if (x>5){
    label->setText("this always run");
    emit signalForStatusBar("this doesnt run");
    }
    @

    and here is the code for signal:
    @
    signals:
    void signalForStatusBar(QString);
    @

    connect statment:
    @
    connect(this, SIGNAL(signalForStatusBar(QString)), this, SLOT(testSlot(QString)));
    @

    and the slot:
    @
    void mainWidget::testSlot(QString string)
    {
    label->setText("Slot is running"+string);
    }
    @


  • Moderators

    Did you see an error message on the output when executing the connect statement?

    You can also check the connect statements bool return value. It has to be 'true'.



  • There is no error message in the output, and I have now checked the output of connect statement and is true.

    The output of connect statement is true but the slot is not doing anything????


  • Moderators

    If you are running your program in the debugger, you can set a break point at the statement in the slot. Then you will that the slot is triggered.



  • Just to be clear, but is the signal inside the MainWindow class or somewhere else? That might fail if the 'this' is used in the connect statement. You then should you:
    @connect(variable pointing to the class of signalForStatusBar, SIGNAL(signalForStatusBar(QString)), this, SLOT(testSlot(QString)));@


  • Moderators

    Please post your header file. If it is too big, you may want to skip lines which have nothing to do with the signals and slots.



  • I have started a new program to see can I make emit to work but is the same principle, as the last program:
    main.cpp
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "mainwidget.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    mainWindow *w=new mainWindow;
    mainWidget *mw=new mainWidget;

    w->setCentralWidget(mw);
    w->show();
    
    QObject::connect(mw, SIGNAL(signalForShowMessageInStatusBar(QString)), w, SLOT(slotForStatusBarMessage(QString)));
    
    return a.exec&#40;&#41;;
    

    }
    @

    mainWindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class mainWindow;
    }

    class mainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit mainWindow(QWidget *parent = 0);
    ~mainWindow();

    private:
    Ui::mainWindow *ui;

    private slots:
    void slotForStatusBarMessage(QString);
    };

    #endif // MAINWINDOW_H
    @

    mainWindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    mainWindow::mainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainWindow)
    {
    ui->setupUi(this);
    }

    mainWindow::~mainWindow()
    {
    delete ui;
    }

    void mainWindow::slotForStatusBarMessage(QString string)
    {
    statusBar()->showMessage(string);
    }
    @

    mainWidget.h
    @
    #ifndef MAINWIDGET_H
    #define MAINWIDGET_H

    #include <QWidget>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QtSql>
    #include <QString>

    #include "mainwindow.h"

    class mainWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit mainWidget(QWidget *parent = 0);

    signals:
    void signalForShowMessageInStatusBar(QString);

    public slots:

    private:
    // varijable
    QPushButton *buttonAddNewRecord;
    QVBoxLayout *layoutMain;
    QHBoxLayout *layoutButton;

    // funkcije
    void functionDatabaseOpen();
    

    };

    #endif // MAINWIDGET_H
    @

    mainWidget.cpp
    @
    #include "mainwidget.h"

    mainWidget::mainWidget(QWidget *parent) :
    QWidget(parent)
    {
    buttonAddNewRecord=new QPushButton("Add new record", this);

    layoutButton=new QHBoxLayout();
    layoutButton->addWidget(buttonAddNewRecord);
    
    layoutMain=new QVBoxLayout();
    layoutMain->addLayout(layoutButton);
    
    functionDatabaseOpen();
    
    setLayout(layoutMain);
    

    }

    void mainWidget::functionDatabaseOpen()
    {
    QSqlDatabase sqlDatabase=QSqlDatabase::addDatabase("QPSQL");

    sqlDatabase.setHostName("localhost");
    sqlDatabase.setDatabaseName("postgres");
    sqlDatabase.setUserName("postgres");
    sqlDatabase.setPassword("xxxx");
    
    if (sqlDatabase.open())
    {
        emit signalForShowMessageInStatusBar("true");
    }
    else
    {
        emit signalForShowMessageInStatusBar("false");
    }
    

    }
    @

    Thank you for all your help



  • did you try to give a name to the signal argument? like:

    signals:
        void signalForStatusBar(QString abc);
    


  • @VRodin I have tried still nothing



  • The only weird thing I spot, is that you are connecting to a private slot from outside the class itself. Other than that, your code looks fine to me.



  • Andre thank you for your help. I have changed the private slot into a public slot but still nothing happnes.



  • I think I spotted the issue: The signal is getting emitted before the connection is made. You trigger emitting the signal from the constructor of mainWidget (via the functionDatabaseOpen method), but at that moment the signal is not connected to the slot yet, as you do that only a couple of lines later in your main method.



  • Andre thanks for your help. I have made some changes in program and it worked.

    @
    QApplication a(argc, argv);
    mainWindow *w=new mainWindow;
    mainWidget *mw=new mainWidget;

    w->setCentralWidget(mw);
    w->show();
    
    QObject::connect(mw, SIGNAL(signalForShowMessageInStatusBar(QString)), w, SLOT(slotForStatusBarMessage(QString)));
    
    mw->functionDatabaseOpen();
    
    return a.exec&#40;&#41;;
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.