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:
connect statment:
and the slot:
- {
- label->setText("Slot is running"+string);
- }
12 replies
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[])
- {
- mainWindow *w=new mainWindow;
- mainWidget *mw=new mainWidget;
- w->setCentralWidget(mw);
- w->show();
- return a.exec();
- }
mainWindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- namespace Ui {
- class mainWindow;
- }
- {
- Q_OBJECT
- public:
- ~mainWindow();
- private:
- Ui::mainWindow *ui;
- private slots:
- };
- #endif // MAINWINDOW_H
mainWindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- ui(new Ui::mainWindow)
- {
- ui->setupUi(this);
- }
- mainWindow::~mainWindow()
- {
- delete ui;
- }
- {
- 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"
- {
- Q_OBJECT
- public:
- signals:
- public slots:
- private:
- // varijable
- // funkcije
- void functionDatabaseOpen();
- };
- #endif // MAINWIDGET_H
mainWidget.cpp
- #include "mainwidget.h"
- {
- layoutButton->addWidget(buttonAddNewRecord);
- layoutMain->addLayout(layoutButton);
- functionDatabaseOpen();
- setLayout(layoutMain);
- }
- void mainWidget::functionDatabaseOpen()
- {
- 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
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.
You must log in to post a reply. Not a member yet? Register here!



