Need help passing a variable to another Form (Signals/Slots)
-
Hello,
I've been working on a tool to connect with a MySQL database. However, I'd like to pass an int and an QSqlDatabase object from my mainwindow to another form when a Push Button is clicked.
mainwindow.h
//... signals: void set_mode(int mode, QSqlDatabase db); //...
mainwindow.cpp
//... connect(ui->pushButton_open, SIGNAL(&QPushButton::clicked), this, SLOT(&get_info(1, db))); //... void MainWindow::on_pushButton_open_clicked() { dialog.setModal(true); dialog.exec(); } //...
dialog.h
//... public slots: void get_info(int x, QSqlDatabase db); //...
I don't know if I did anything the right way so far. Any idea?
-
Hello,
I've been working on a tool to connect with a MySQL database. However, I'd like to pass an int and an QSqlDatabase object from my mainwindow to another form when a Push Button is clicked.
mainwindow.h
//... signals: void set_mode(int mode, QSqlDatabase db); //...
mainwindow.cpp
//... connect(ui->pushButton_open, SIGNAL(&QPushButton::clicked), this, SLOT(&get_info(1, db))); //... void MainWindow::on_pushButton_open_clicked() { dialog.setModal(true); dialog.exec(); } //...
dialog.h
//... public slots: void get_info(int x, QSqlDatabase db); //...
I don't know if I did anything the right way so far. Any idea?
hi @SnuggleKat
at the very least you're mixing new and old connect syntax,did you read through this:
http://doc.qt.io/qt-5/signalsandslots.html
and this
https://wiki.qt.io/New_Signal_Slot_Syntax
should give you everything you need to
fix
your errors.
additionally to that, you're trying to connect to a slot that has incompatible arguments to your signal, that won't work either.
-
Hello,
I've been working on a tool to connect with a MySQL database. However, I'd like to pass an int and an QSqlDatabase object from my mainwindow to another form when a Push Button is clicked.
mainwindow.h
//... signals: void set_mode(int mode, QSqlDatabase db); //...
mainwindow.cpp
//... connect(ui->pushButton_open, SIGNAL(&QPushButton::clicked), this, SLOT(&get_info(1, db))); //... void MainWindow::on_pushButton_open_clicked() { dialog.setModal(true); dialog.exec(); } //...
dialog.h
//... public slots: void get_info(int x, QSqlDatabase db); //...
I don't know if I did anything the right way so far. Any idea?
@SnuggleKat try this:
mainwindow.h//...
//signals:
//void set_mode(int mode, QSqlDatabase db);
//...mainwindow.cpp
//...
//connect(ui->pushButton_open, SIGNAL(&QPushButton::clicked), this, SLOT(&get_info(1, db)));
//...
void MainWindow::on_pushButton_open_clicked()
{
dialog.setModal(true);
dialog.exec();
dialog.get_info(1, db);
}
//...dialog.h
//...
public slots:
void get_info(int x, QSqlDatabase db);
//...OR ***************************************
just change it
connect(ui->pushButton_open, SIGNAL(clicked), dialog, SLOT(get_info(1, db)));and read it SIGNAL_SLOT
-
Thank you for the answers. Gonna give it a try soon. When my MainWindow class inherites from the QMainWindow class, does it indirectly derive from the QObject class?
-
Thank you for the answers. Gonna give it a try soon. When my MainWindow class inherites from the QMainWindow class, does it indirectly derive from the QObject class?
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
When my MainWindow class inherites from the QMainWindow class, does it indirectly derive from the QObject class?
Yes.
You can see what a class is inherited from in the documentation: http://doc.qt.io/qt-5/qwidget.html (as QMainWindow inherits QWidget)
See "Inherits:" column at the top. -
Okay,
inspired by this: http://doc.qt.io/qt-5/signalsandslots.html#a-small-exampleI have done the following:
The functions.h defines a class of a QSqlDatabase and an int attribute.
//... #include <QSqlDatabase> class Option: QSqlDatabase { private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){ return this->db; } int get_mode(){ return this->mode; } signals: void send_option(QSqlDatabase db, int mode;{ emit set_up(db, mode); } public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; } };
Attempting to pass such an object over to another form by doing this:
mainwindow.h://... #include "functions.h" //... private: QSqlDatabase db; Option opt_main;
mainwindow.cpp
connect(&opt_main, SIGNAL(&Option::send_option), &opt_dia, SLOT(&Option::set_up));
opt_dia is an object of Option. What's the best way to instantiate this option inside the dialog.h? The dialog.h has the functions.h included.
-
Okay,
inspired by this: http://doc.qt.io/qt-5/signalsandslots.html#a-small-exampleI have done the following:
The functions.h defines a class of a QSqlDatabase and an int attribute.
//... #include <QSqlDatabase> class Option: QSqlDatabase { private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){ return this->db; } int get_mode(){ return this->mode; } signals: void send_option(QSqlDatabase db, int mode;{ emit set_up(db, mode); } public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; } };
Attempting to pass such an object over to another form by doing this:
mainwindow.h://... #include "functions.h" //... private: QSqlDatabase db; Option opt_main;
mainwindow.cpp
connect(&opt_main, SIGNAL(&Option::send_option), &opt_dia, SLOT(&Option::set_up));
opt_dia is an object of Option. What's the best way to instantiate this option inside the dialog.h? The dialog.h has the functions.h included.
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
connect(&opt_main, SIGNAL(&Option::send_option), &opt_dia, SLOT(&Option::set_up));
This is still wrong - you're mixing old and new connect syntax.
Please read the documentation (links were already provided).. -
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
connect(&opt_main, SIGNAL(&Option::send_option), &opt_dia, SLOT(&Option::set_up));
This is still wrong - you're mixing old and new connect syntax.
Please read the documentation (links were already provided)..@jsulm said in Need help passing a variable to another Form (Signals/Slots):
Ah, sorry, totally misseen the new syntacks lacks the SIGNAL and SLOT terms.
QObject::connect(&opt_main, &Option::send_option, &opt_dia, &Option::set_up);
Still something wrong?
What I need to know is what'd be the best way to instantiate the opt_dia object since it's needed in another Form class -
Hi
you also need
class Option: QSqlDatabase
{
Q_OBJECT
...Q_OBJECT is needed for signals & slots.
note, after adding it, select clean all, rebuild all to make sure its seen.
-
Hi
you also need
class Option: QSqlDatabase
{
Q_OBJECT
...Q_OBJECT is needed for signals & slots.
note, after adding it, select clean all, rebuild all to make sure its seen.
@mrjj said in Need help passing a variable to another Form (Signals/Slots):
Hi
you also need
class Option: QSqlDatabase
{
Q_OBJECT
...Q_OBJECT is needed for signals & slots.
note, after adding it, select clean all, rebuild all to make sure its seen.
Hello,
just did that.
After this I instantiated another object of the type Option in the dialog.h:#include <QDialog> #include "functions.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); Option opt_dia; //note #1 ~Dialog(); private: Ui::Dialog *ui; void load_meta(); int mode; private slots: void on_pushButton_ok_clicked(); };
#1 - This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:
no matching function for call to 'QObject::connectImpl(const Object*&, void**, const Object*&, void**, QtPrivate::QSlotObject<void (Option::*)(QSqlDatabase, int), QtPrivate::List<QSqlDatabase, int>, void>*, Qt::ConnectionType&, const int*&, const QMetaObject*)'
invalid static_cast from type 'QObject*' to type 'QtPrivate::FunctionPointer<void (Option::*)(QSqlDatabase, int)>::Object* {aka Option*}'
fyi, the Object class now looks like this:
#include <QtSql> #include <QSqlDatabase> #include <QTableView> class Option: QSqlDatabase { Q_OBJECT private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){return this->db;} Option() { this->db = QSqlDatabase::addDatabase("QMYSQL"); this->mode = 0; } virtual ~Option(); int get_mode(){return this->mode;} signals: void send_option(QSqlDatabase db, int mode){ emit set_up(db, mode); } public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; } };
-
@mrjj said in Need help passing a variable to another Form (Signals/Slots):
Hi
you also need
class Option: QSqlDatabase
{
Q_OBJECT
...Q_OBJECT is needed for signals & slots.
note, after adding it, select clean all, rebuild all to make sure its seen.
Hello,
just did that.
After this I instantiated another object of the type Option in the dialog.h:#include <QDialog> #include "functions.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); Option opt_dia; //note #1 ~Dialog(); private: Ui::Dialog *ui; void load_meta(); int mode; private slots: void on_pushButton_ok_clicked(); };
#1 - This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:
no matching function for call to 'QObject::connectImpl(const Object*&, void**, const Object*&, void**, QtPrivate::QSlotObject<void (Option::*)(QSqlDatabase, int), QtPrivate::List<QSqlDatabase, int>, void>*, Qt::ConnectionType&, const int*&, const QMetaObject*)'
invalid static_cast from type 'QObject*' to type 'QtPrivate::FunctionPointer<void (Option::*)(QSqlDatabase, int)>::Object* {aka Option*}'
fyi, the Object class now looks like this:
#include <QtSql> #include <QSqlDatabase> #include <QTableView> class Option: QSqlDatabase { Q_OBJECT private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){return this->db;} Option() { this->db = QSqlDatabase::addDatabase("QMYSQL"); this->mode = 0; } virtual ~Option(); int get_mode(){return this->mode;} signals: void send_option(QSqlDatabase db, int mode){ emit set_up(db, mode); } public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; } };
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:
Please show your exact connect call
-
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:
Please show your exact connect call
@jsulm I have found a little mistake. I accidentally assigned the emit call to the signal instead of the slot. After correcting this I tried connecting 2 instances of my class Option inside the MainWindow class which worked (I think). I still can't see an instance of Option outside the MainWindow class.
functions.h
#include <QtSql> #include <QSqlDatabase> #include <QTableView> class Option: public QObject { Q_OBJECT private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){return this->db;} int get_mode(){return this->mode;} signals: void send_option(QSqlDatabase db, int mode); public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit send_option(db, mode);} };
mainwindow.h
//... class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_connect_clicked(); void on_pushButton_create_pdf_clicked(); void on_pushButton_save_settings_clicked(); void on_pushButton_open_clicked(); private: Ui::MainWindow *ui; int create_pdf(); Dialog dialog; QSqlDatabase db; Option opt_main, testopt; //testopt was the test-instance i used };
mainwindow.cpp
//... QObject::connect(&opt_main, &Option::send_option, &opt_dia, &Option::set_up); //opt_dia not declared in this scope //... void MainWindow::on_pushButton_open_clicked() { opt_main.send_option(db, 123); dialog.setModal(true); dialog.exec(); }
dialog.h
//... #include "functions.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); //QObject* x; Option opt_dia; ~Dialog(); private: Ui::Dialog *ui; void load_meta(); int mode; private slots: void on_pushButton_ok_clicked(); };
dialog.cpp
//... void Dialog::on_pushButton_ok_clicked() { qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName(); //testing the result }
opt_dia cannot be found since it's not declared in this scope. If I use &Dialog::opt_dia it is found but the connection will fail to compile with the following error: Fehler: no matching member function for call to 'connect'
-
@jsulm I have found a little mistake. I accidentally assigned the emit call to the signal instead of the slot. After correcting this I tried connecting 2 instances of my class Option inside the MainWindow class which worked (I think). I still can't see an instance of Option outside the MainWindow class.
functions.h
#include <QtSql> #include <QSqlDatabase> #include <QTableView> class Option: public QObject { Q_OBJECT private: QSqlDatabase db; int mode; public: QSqlDatabase get_db(){return this->db;} int get_mode(){return this->mode;} signals: void send_option(QSqlDatabase db, int mode); public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit send_option(db, mode);} };
mainwindow.h
//... class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_connect_clicked(); void on_pushButton_create_pdf_clicked(); void on_pushButton_save_settings_clicked(); void on_pushButton_open_clicked(); private: Ui::MainWindow *ui; int create_pdf(); Dialog dialog; QSqlDatabase db; Option opt_main, testopt; //testopt was the test-instance i used };
mainwindow.cpp
//... QObject::connect(&opt_main, &Option::send_option, &opt_dia, &Option::set_up); //opt_dia not declared in this scope //... void MainWindow::on_pushButton_open_clicked() { opt_main.send_option(db, 123); dialog.setModal(true); dialog.exec(); }
dialog.h
//... #include "functions.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); //QObject* x; Option opt_dia; ~Dialog(); private: Ui::Dialog *ui; void load_meta(); int mode; private slots: void on_pushButton_ok_clicked(); };
dialog.cpp
//... void Dialog::on_pushButton_ok_clicked() { qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName(); //testing the result }
opt_dia cannot be found since it's not declared in this scope. If I use &Dialog::opt_dia it is found but the connection will fail to compile with the following error: Fehler: no matching member function for call to 'connect'
@SnuggleKat Why don't you simply define the slot inside Dialog class?
Exposing a member (opt_dia) as public to outside world is bad design. -
@SnuggleKat Why don't you simply define the slot inside Dialog class?
Exposing a member (opt_dia) as public to outside world is bad design.@jsulm said in Need help passing a variable to another Form (Signals/Slots):
@SnuggleKat Why don't you simply define the slot inside Dialog class?
Exposing a member (opt_dia) as public to outside world is bad design.I'm aware it's bad design. That's why I asked what'd be the best way to instantiate it.
Is it the right way to access the signal through opt_dia if I define the slot inside the Dialog class?
And what should the connect() look like now? -
@jsulm said in Need help passing a variable to another Form (Signals/Slots):
@SnuggleKat Why don't you simply define the slot inside Dialog class?
Exposing a member (opt_dia) as public to outside world is bad design.I'm aware it's bad design. That's why I asked what'd be the best way to instantiate it.
Is it the right way to access the signal through opt_dia if I define the slot inside the Dialog class?
And what should the connect() look like now?@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
When I define the slot inside the Dialog class is it the right way to access the signal through opt_dia?
I don't understand this - what signal do you want to access from opt_dia?
Connect would be:connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
Actually I don't see a need for signal/slot here as dialog is a member of MainWindow. set_up can be a normal method which you call like any other without emiting a signal:
dialog.set_up(...);
-
@SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):
When I define the slot inside the Dialog class is it the right way to access the signal through opt_dia?
I don't understand this - what signal do you want to access from opt_dia?
Connect would be:connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
Actually I don't see a need for signal/slot here as dialog is a member of MainWindow. set_up can be a normal method which you call like any other without emiting a signal:
dialog.set_up(...);
@jsulm Sorry, I meant to ask if ithis would be the right way of doing it:
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
Change the connect() method as suggestion but the values don't get passed over.
Same for adding dialog.set_up();
Should I make QSqlDatabase db and int mode static? -
@jsulm Sorry, I meant to ask if ithis would be the right way of doing it:
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
Change the connect() method as suggestion but the values don't get passed over.
Same for adding dialog.set_up();
Should I make QSqlDatabase db and int mode static?@SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
"Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases. -
@SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
"Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.@jsulm said in Need help passing a variable to another Form (Signals/Slots):
@SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
"Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.That's what I thought.
void MainWindow::on_pushButton_open_clicked() { opt_main.send_option(db, 123); QObject::connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up); dialog.set_up(db, 123); //also tried this which didn't help either dialog.setModal(true); dialog.exec(); }
-
@jsulm said in Need help passing a variable to another Form (Signals/Slots):
@SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
"Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.That's what I thought.
void MainWindow::on_pushButton_open_clicked() { opt_main.send_option(db, 123); QObject::connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up); dialog.set_up(db, 123); //also tried this which didn't help either dialog.setModal(true); dialog.exec(); }
@SnuggleKat I really don't get why you use signals slots here?
void MainWindow::on_pushButton_open_clicked() { dialog.set_up(db, 123); //also tried this which didn't help either dialog.setModal(true); dialog.exec(); }
"//also tried this which didn't help either" - please show what dialog.set_up(db, 123) is doing.
-
@SnuggleKat I really don't get why you use signals slots here?
void MainWindow::on_pushButton_open_clicked() { dialog.set_up(db, 123); //also tried this which didn't help either dialog.setModal(true); dialog.exec(); }
"//also tried this which didn't help either" - please show what dialog.set_up(db, 123) is doing.
@jsulm
qDebug() shows me that it effectively does nothing. dialog.set_up() does not touch the object declared as opt_dia."mode: 12141696 - "
12141696 seems to be a random value.
This is the qDebug() line:qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName();