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.
@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();
-
@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();
@SnuggleKat Can you show the content of dialog.set_up(...)?
-
@jsulm Do you mean this?
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
-
@jsulm Do you mean this?
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
@SnuggleKat set_up(
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; opt_dia.set_up(db, mode); }
-
I admit, I'm a little bit out of my field here, but can you simply reassign a
QSqlDatabase
in that way?I usually pass a QSqlDatabase-pointer between classes.
-
@SnuggleKat set_up(
public slots: void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; opt_dia.set_up(db, mode); }
@jsulm
Okay, it now works! Thank you.
I also hat to re-write a set_up() function inside the Dialog class.
I will clean up the way it's done by now and remember it for the next time