-   Programming (
-   -   Qt dialogs return data (

Ephracis 02-20-2007 06:46 PM

Qt dialogs return data
I am programming a small app in Qt 4 and I am looking for a way to send data from a dialog..

I have a widget within the mainwindow which opens up a dialog, within the dialog the user is asked to input some data and then the user is supposed to press the Add-button. Now, when this button is pressed the dialog should close and the calling widget should take care of all the data the user gave the dialog and process it.

So my question is: how can I make the dialog send the data to the calling widget / make the calling widget fetch the data from the dialog?

graemef 02-20-2007 11:22 PM

Typically the way it works is as follows:
  • You create your own dialog class that extends QDialog
  • This class will have its own properties and methods
  • You then create an instance of the dialog
  • You then display the dialog
  • The user adds some information and closes the dialog
  • The dialog instance still exists so you can now look at the properties (This may require you to write some methods to move the data from widgets on the dialog into the properties of the dialog.

Typically all the interaction with the dialog would be done via methods of your dialog class.

Ephracis 02-21-2007 08:23 AM

Oh, so the instance still exists? But when does the instance go away, then?

graemef 02-21-2007 09:21 AM

Typically when the application closes, but if you wish then you can explicitly delete the object.

Ephracis 02-21-2007 09:25 AM

But I have created a button to close the dialog, if I connect it to the slot close() I get seg faults when I try to call the methods of the dialog..

EDIT: no, I got it now.. :)
But I have one question remaning: I've seen that the dialog can return a DialogCode.. how do I use that?

graemef 02-21-2007 09:58 AM

There are several ways you can achieve this:

You can attach the button to a particular slot, such as the accept() or reject() slots.
If you want to return a more complex value (still an integer) then you can use the done(int returnValue) slot.

Check out the documentation

Ephracis 02-21-2007 10:09 AM

Ok.. Thank you..

I have connected the button to my own slot called saveAndClose().. This slot will check all QLineEdit and issue a warning if any of them are empty.. but whenever I try to access the text within a lineedit I get a seg fault..


class MyDialog : public QDialog
        MyDialog(QWidget *parent=0);
        void setName(QString _name) { m_name = _name; }
        QString name() { return m_name; }
        QString m_name;
        QLineEdit *nameEdit;
        QPushButton *addBut;

MyDialog::MyDialog(QWidget *parent)
    : QDialog(parent)
    QLineEdit *nameEdit = new QLineEdit;
    QPushButton *addBut = new QPushButton("Add");
    connect(addBut, SIGNAL(clicked()),
            this, SLOT(saveAndClose()));

void MyDialog::saveAndClose()
    if (nameEdit->text().isEmpty())

What's wrong here?

Ephracis 02-21-2007 12:49 PM

I fixed the problem by connecting the lineedit signal textChanged to update m_name and then use that one instead in the saveAndClose() slot.

But I have a new problem. Now I want the dialog to fetch a QStringList from the calling widget..

I tried to do this in the calling widget

MyDialog dialog(this);

QStringList list;
list << "Item1";
list << "Second item";

int ret = dialog.exec();

But it seems that after dialog.exec() gets called the internal property (QStringList m_list) gets cleared. Before I call dialog.exec I can call dialog.list() and I will get back my two strings. But in the constructor of the dialog the QStringList is empty..

All times are GMT -5. The time now is 08:52 PM.