[Solved] get object from dialog when accepted
-
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 ;)
-
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
-
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
-
Can you show the code where you use your dialog ?
-
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_);
};
@ -
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() == QDialog::Accepted) { contact myContact(new_c_dialog->name(), new_c_dialog->mobile(), new_c_dialog->email()); doSomethingWithNewContact(myContact); }
}
@
-
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_);
};
@ -
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 ;)
-
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 :)
-
Sure, I was very focused on my project and I forgot it!
Thank you ;)