Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting values from dynamically created user interfaces

Getting values from dynamically created user interfaces

Scheduled Pinned Locked Moved Solved General and Desktop
dynamicinterfacevaluesstore
9 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on 9 Oct 2017, 16:25 last edited by Lasith 10 Sept 2017, 16:26
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Oct 2017, 16:56 last edited by
      #2

      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.

      L 1 Reply Last reply 9 Oct 2017, 17:03
      1
      • M mrjj
        9 Oct 2017, 16:56

        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.

        L Offline
        L Offline
        Lasith
        wrote on 9 Oct 2017, 17:03 last edited by Lasith 10 Sept 2017, 17:03
        #3

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

        M 1 Reply Last reply 9 Oct 2017, 17:11
        0
        • L Lasith
          9 Oct 2017, 17:03

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Oct 2017, 17:11 last edited by
          #4

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

          L 1 Reply Last reply 9 Oct 2017, 17:16
          1
          • M mrjj
            9 Oct 2017, 17:11

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

            L Offline
            L Offline
            Lasith
            wrote on 9 Oct 2017, 17:16 last edited by
            #5

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

            M 1 Reply Last reply 9 Oct 2017, 17:19
            0
            • L Lasith
              9 Oct 2017, 17:16

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Oct 2017, 17:19 last edited by mrjj 10 Sept 2017, 17:22
              #6

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

              L 1 Reply Last reply 9 Oct 2017, 17:23
              2
              • M mrjj
                9 Oct 2017, 17:19

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

                L Offline
                L Offline
                Lasith
                wrote on 9 Oct 2017, 17:23 last edited by
                #7

                @mrjj Thanx alot mate :)

                M 1 Reply Last reply 9 Oct 2017, 17:26
                0
                • L Lasith
                  9 Oct 2017, 17:23

                  @mrjj Thanx alot mate :)

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 9 Oct 2017, 17:26 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 9 Oct 2017, 20:15 last edited by
                    #9

                    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 ?

                    1 Reply Last reply
                    0

                    1/9

                    9 Oct 2017, 16:25

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved