Need help passing a variable to another Form (Signals/Slots)
-
@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