Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Emiting signal does not work
Forum Updated to NodeBB v4.3 + New Features

Emiting signal does not work

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    depecheSoul
    wrote on last edited by
    #1

    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);
    }
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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'.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        depecheSoul
        wrote on last edited by
        #3

        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????

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            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)));@

            Greetz, Jeroen

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                depecheSoul
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

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

                  signals:
                      void signalForStatusBar(QString abc);
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    depecheSoul
                    wrote on last edited by
                    #9

                    @VRodin I have tried still nothing

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        depecheSoul
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            depecheSoul
                            wrote on last edited by
                            #13

                            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;;
                            

                            @

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved