Getting values from dynamically created user interfaces



  • In my Qt c++ application there are mainly 2 interfaces(MainWindow.ui and Dialog.ui). The MainWindow has a line edit and a push button! The line edit is used to input a number and when button is clicked that number of Dialog interfaces appear as follows!

    void MainWindow::on_pushButton_clicked()
    {
    for(int i=0;i<ui->values->text().toInt();i++){
    Dialog *a = new Dialog();
    a->show();
    }

    }

    Dialog interface has 2 line edits mainly to get the name and age and a button to get these 2 values ! How can I store the name and age values of each dynamically created "Dialog "interface?


  • Qt Champions 2016

    Hi
    Store where?
    You could create a new signal with name and age and emit from dialog when user press ok
    and connect that new signal to a slot in mainwindow
    or you can add some function to the Dialog to simply return it like
    QString getName()
    QString getAge();

    You will need to keep all the pointers ( all of a's ) to be able to do that.

    Also, as a note, you will leak memory since all the Dialogs will not be deleted.
    You can use
    a->setAttribute(Qt::WA_DeleteOnClose);
    to have it clean up when closed.
    But then you must use a signal as you cant ask it about
    values when closed.



  • @mrjj thanx but to use Signals and slots we have to instantiate MainWindow class inside Dialog again right? Then it will open a new MainWindow and values won't go to the intially created window right?


  • Qt Champions 2016

    @Lasith
    no, you already have an instance of main window.
    You will connect the dialog and the mainwindow
    in the same place as where you create the
    dialogs.
    Like in MainWindow::on_pushButton_clicked()

    Shall i make small sample ?
    Takes shorter than to discuss without code.



  • @mrjj Thanx :) Give me a sample then its easy to understand


  • Qt Champions 2016

    @Lasith
    here u go
    https://www.dropbox.com/s/2afopz3tuojmivv/funkydialogs.zip?dl=0

    Keypoints is:

    class MyDialog : public QDialog {
      Q_OBJECT
    public:
      explicit MyDialog(QWidget* parent = 0);
      ~MyDialog();
    private:
      Ui::MyDialog* ui;
    signals:
      void DataSignal(QString Name, QString Age ); // our new signal
    public slots:
      virtual void accept() override; // we override this so we can send signal when user press ok
    };
    
    void MyDialog::accept() {
      emit DataSignal( ui->edName->text(), ui->edAge->text()); // here we emit the signal
      QDialog::accept();// call base
    }
    
    and in mainwindow we do
    
    void MainWindow::on_pbGO_released() {
      int Num = ui->NumDialogs->text().toInt();
      for(int i = 0; i < Num; i++) {
        MyDialog* a = new MyDialog();
        qDebug() << "connect ok: " << connect( a, SIGNAL (DataSignal(QString, QString)), this, SLOT(DataSignal(QString, QString)) ) ;
        a->show();
      }
    }
    
    

    btw, you should maybe add
    a->setAttribute(Qt::WA_DeleteOnClose);
    so it cleans up.



  • @mrjj Thanx alot mate :)


  • Qt Champions 2016

    @Lasith
    Np :)
    If you do not want the dialogs to be on top of each other, you can use move to offset them a bit.
    Also, maybe a check for number. like a max number of dialogs.
    I didnt dare try with 13286754568723456734275632476575 ;)


  • Qt Champions 2016

    Hi
    While walking my imaginary dog, i was wondering if the
    number you input "ui->values->text().toInt()"
    means how many names/ages to get and you mean for it
    to ask that many times, and not really create that many
    dialogs ?
    So like ask me 10 times
    and one and the same dialog would show up 10 times ?
    And each time, user press ok, it would save the data, then
    show again ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.