Emiting signal does not work
-
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????
-
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)));@ -
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();
}
@mainWindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class mainWindow;
}class mainWindow : public QMainWindow
{
Q_OBJECTpublic:
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();
@