Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get the values entered in the Main Window from a newly open Dialog ?



  • #signals #slots #QDialog



  • Hi,

    Can u please ellaborate, u want values from QDialog is it? .
    If u want values from QDialog,

    1. Use connect statement in constructor.

    QObject::connect(Dialog_object, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));

    1. 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 using QDialog::exec(), after it returns (check return value) simply access variables via the QDialog instance, before it gets destroyed.


  • Qt Champions 2019

    @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)?



  • @Lazy93 this is a recurring question. Please do a search in the forum, take a look at this post for instance.



  • 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


  • Qt Champions 2019

    @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)


  • Qt Champions 2019

    @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.


  • Qt Champions 2019

    @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();


  • Qt Champions 2019

    @Lazy93 Then fix the problem. Please take a closer look at the constructor...


  • Lifetime Qt Champion

    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 nothing

    Dialog *dialog = new Dialog;

    so you need at least
    Dialog *dialog = new Dialog(playerNumberVariable);



  • Hi mrjj , How do i insert value into playernumber then ?


  • Qt Champions 2019

    @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 ?

    }


  • Qt Champions 2019

    @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 ?


  • Qt Champions 2019

    @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;
    }
    

Log in to reply