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. [Solved] get object from dialog when accepted

[Solved] get object from dialog when accepted

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.9k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rfringuello89
    wrote on last edited by
    #1

    Hi, this is my first post here.

    I am trying to implement a simple project in order to learn some qt programming.

    The idea is to create a little rubric.
    The list of contacts is shown in a main window. The main window provides also some buttons like create a new contact, delete a contact, edit a contact, and so on. All the contacts should be inserted in a QList created in the main window code, I defined a custom class "Contact" for the contacts so the list is a Qlist<Contact>.

    I'm using a QDialog with 3 labels and 3 line edit so the user can insert name, phone number and email address of the new contact. The dialog is opened as expected through signal/slot mechanism.

    The problem is simple, when I accept the dialog pressing OK i need to give back the 3 informations to my main window in order to create a QList entry.

    I saw other posts but they did not help me.

    Thanks guys ;)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Just add getters for the three informations of your dialog and retrieve them in your main window class or only one getter that builds a Contact from the content of the dialog.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rfringuello89
        wrote on last edited by
        #3

        How can I call the getter/s from the main?
        Should I use a signal/slot connection triggered when the dialog finishes?

        Thank you SGaist

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you show the code where you use your dialog ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rfringuello89
            wrote on last edited by
            #5

            sure:

            main window cpp code:
            @Contacts::Contacts(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::Contacts)
            {
            ui->setupUi(this);

            QList<contact> contatti;
            

            }

            Contacts::~Contacts()
            {
            delete new_c_dialog;
            delete ui;
            }

            void Contacts::on_actionNew_triggered()
            {
            //NEW button has been clicked

            //a "new_contact_dialog" dialog window is shown
            new_c_dialog = new AddContact_Dialog(this);
            new_c_dialog->show();
            

            }
            @

            dialog window cpp code:
            @AddContact_Dialog::AddContact_Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::AddContact_Dialog)
            {
            ui->setupUi(this);
            }

            AddContact_Dialog::~AddContact_Dialog()
            {
            delete ui;
            }

            void AddContact_Dialog::on_buttonBox_accepted()
            {
            //executed when OK is clicked in the dialog box
            QString name = ui->name_edit->text();
            QString mobile = ui->mobile_edit->text();
            QString email = ui->email_edit->text();

            contact C(name, mobile, email);
            
            qDebug() << name << "  " << mobile << "  " << email;
            

            }@

            contact class code:
            @#include <QString>

            class contact
            {
            private:
            QString name, mobile, email;

            public:
            contact(QString name_, QString mobile_, QString email_);
            };
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              First thing, you should have a look at Qt's examples/demos and documentation.

              You'll find in there everything you need to get a good start.

              Since you require an input from the user, you should call exec() not show(). Or if you don't want your dialog to block the main window. User open and connect it to a slot in your Contacts widget so you know when the user as accepted.

              @
              Header:
              class AddContact_Dialog : public QDialog {

              AddContact_Dialog(QWidget *parent = 0);

              QString name() const; // << getter
              };

              Implementation:
              QString AddContact_Dialog::name() const
              {
              return ui->name_edit->text();
              }

              void Contacts::on_actionNew_triggered()
              {
              //NEW button has been clicked

              //a "new_contact_dialog" dialog window is shown
              if (!new_c_dialog) // don't forget to initialize it to 0
                  new_c_dialog = new AddContact_Dialog(this);
              new_c_dialog->clear(); // this method is up to you to write, it just to empty the fields of your dialog
              if (new_c_dialog->exec&#40;&#41; == QDialog::Accepted&#41; {
                  contact myContact(new_c_dialog->name(), new_c_dialog->mobile(), new_c_dialog->email());
              doSomethingWithNewContact(myContact);
              }
              

              }

              @

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Just some comments on your code

                @Contacts::Contacts(QWidget *parent) : // not a good name for a widget, I would think of it as a container for Contact objects
                QMainWindow(parent),
                ui(new Ui::Contacts)
                {
                ui->setupUi(this);

                QList<contact> contatti; // you know that contatti will disappear at the end of the constructor ?
                

                }

                Contacts::~Contacts()
                {
                delete new_c_dialog;
                delete ui;
                }

                void Contacts::on_actionNew_triggered()
                {
                //NEW button has been clicked

                //a "new_contact_dialog" dialog window is shown
                new_c_dialog = new AddContact_Dialog(this); // sort of leak, you are creating a new dialog each time you click on the button and not deleting the old one
                new_c_dialog->show();
                

                }
                @

                dialog window cpp code:
                @AddContact_Dialog::AddContact_Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::AddContact_Dialog)
                {
                ui->setupUi(this);
                }

                AddContact_Dialog::~AddContact_Dialog()
                {
                delete ui;
                }

                void AddContact_Dialog::on_buttonBox_accepted()
                {
                //executed when OK is clicked in the dialog box
                QString name = ui->name_edit->text();
                QString mobile = ui->mobile_edit->text();
                QString email = ui->email_edit->text();

                contact C(name, mobile, email);
                
                qDebug() << name << "  " << mobile << "  " << email;
                

                }@

                contact class code:

                @#include <QString>

                class contact // you should rather use camel cased class name otherwise it will make your code hard to read
                {
                private:
                QString name, mobile, email;

                public:
                contact(QString name_, QString mobile_, QString email_);
                };
                @

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rfringuello89
                  wrote on last edited by
                  #8

                  Well, I solved my problem using some QString member and some get/set method. I don't know which is the best solution.

                  Thank for your help and for your comments also.

                  I think you will see me again here ;)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You're welcome !

                    Now that you have it working, please also update the thread title prepending [solved] so other forum users may know a solution has been found :)

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rfringuello89
                      wrote on last edited by
                      #10

                      Sure, I was very focused on my project and I forgot it!
                      Thank you ;)

                      1 Reply Last reply
                      0

                      • Login

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