Emiting signal does not work
-
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)));@ -
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
-
@VRodin I have tried still nothing
-
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();@