Solved How to get the values entered in the Main Window from a newly open Dialog ?
-
Hi,
Can u please ellaborate, u want values from QDialog is it? .
If u want values from QDialog,- Use connect statement in constructor.
QObject::connect(Dialog_object, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));
- Define the slot where u want to get values from QDialog .
void dialogIsFinished(int result)
{
if(result == QDialog::Accepted)
{
// Store the values from QDialog in variable.
}
else
{
// write the logic if u have canceled the QDialog operation
}
}Thanks,
-
@Lazy93
Or, if you are usingQDialog::exec()
, after it returns (check return value) simply access variables via theQDialog
instance, before it gets destroyed. -
@Lazy93 What dialog is that exactly?
And your question is really unclear: "How to get the values entered in the Main Window from a newly open Dialog".
What values? Where are those entered (in main window or dialog)? -
-
Hello there guys, what I'm looking for is something like for example,
At mainwindow, i will be asking the user to input a number of player, and then execute next form while carrying the input number from the mainwindow.
something like
1.Start,
2.Mainwindow appear,
3.Key in the number of player, // lets say i put an X
4.Click OK/Proceed,
5.A new dialog/form will be executed, // lets say its called Dialog1
6.The number of player that has been input in the mainwindow is carried to that new form // I want to carry the value of X from the mainwindow to Dialog1 -
@Lazy93 What is the problem?
Create your own dialog/widget, add a constructor where you can pass this parameter (number of player) from main window to this dialog/window.class MyDialog: public QDialog { public: MyDialog(int playerNumber, QWidget *parent = nullptr); }
-
Hi there, I tried that method but I get funny value on the new Dialog.
In the main window.h (header),
public:
explicit Home(QWidget *parent = 0);
~Home();
long long pn;
private slots:
void on_lineEdit_textEdited(const QString &arg1);at the mainwindow.cpp
void Home::on_lineEdit_textEdited(const QString &arg1){
pn = 0;pn = arg1.toLongong();
}
void Home::on_pushButton_clicked()
{
Dialog dialog;
dialog.setModal(true);
dialog.exec();
}at dialog.h
public:
explicit Dialog(QWidget *parent= 0);
~Dialog();
long long pn1;at dialog.cpp
Home *home = new Home;
pn1 = home->pn;
ui->label->setText(QString ("%1").arg(pn1));The problem is the number shown at label is not the same as the number i put in pn(at Home)
-
@Lazy93 I don't understand the code fragments you posted.
You create an instance of Home, but you don't show it to the user - so how can the user change the value?!Home *home = new Home; pn1 = home->pn; // You will get the default value here as the dialog isn't shown and the user cannot change the value...
-
Hi what i want to know is how to pass the value entered from mainwindow to dialog ? I tried connect signal and slot. But that didnt work. Connect Signal and Slot only works if you already open Form A and then you open Form B, key in the data and pass it back to form A. What i want to do is to key in some data in form A and when I open Form B, i can get the data from Form A.
-
@Lazy93 said in How to get the values entered in the Main Window from a newly open Dialog ?:
what i want to know is how to pass the value entered from mainwindow to dialog ?
I already explained how (no need for signals/slots). And I pointed out (in my last post) that you're doing something wrong. Did you fix the bug?
-
class MyDialog: public QDialog
{
public:
MyDialog(int playerNumber, QWidget *parent = nullptr);
}if i use this method, i cannot open my dialog with
Dialog *dialog = new Dialog;
dialog->exec();as it says no matching function for call to 'Dialog::Dialog();
-
@Lazy93 Then fix the problem. Please take a closer look at the constructor...
-
Hi
Just in case @jsulm's good hint do not ring any bells..MyDialog(int playerNumber, QWidget *parent = nullptr);
Asks for INT and optional parent
but you give it nothingDialog *dialog = new Dialog;
so you need at least
Dialog *dialog = new Dialog(playerNumberVariable); -
Hi mrjj , How do i insert value into playernumber then ?
-
@Lazy93 The user has to enter it, right? Then use this value.
Here your own description:
"
1.Start,
2.Mainwindow appear,
3.Key in the number of player, // lets say i put an X
4.Click OK/Proceed,
5.A new dialog/form will be executed, // lets say its called Dialog1
6.The number of player that has been input in the mainwindow is carried to that new form // I want to carry the value of X from the mainwindow to Dialog1
" -
Yes, but how to use the playernumber in
MyDialog(int playerNumber, QWidget *parent = nullptr); ?
i think.....
void Dialog::on_lineEdit_textEdited(const QString &arg1){
playernumber = arg1.toInt ();? / / <<<< cannot be done right ?
}
-
@Lazy93 said in How to get the values entered in the Main Window from a newly open Dialog ?:
playernumber = arg1.toInt ();? / / <<<< cannot be done right ?
it can be done, why not?
-
Hi, How do i call the value from Dialog ?
-
@Lazy93 Simply add a public getter method to your dialog:
void Dialog::on_lineEdit_textEdited(const QString &arg1){ _playernumber = arg1.toInt ();? / / <<<< cannot be done right ? } int Dialog::playerNumber() { return _playerNumber; }