LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2007, 05:46 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
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?
 
Old 02-20-2007, 10:22 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-21-2007, 07:23 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Oh, so the instance still exists? But when does the instance go away, then?
 
Old 02-21-2007, 08:21 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Typically when the application closes, but if you wish then you can explicitly delete the object.
 
Old 02-21-2007, 08:25 AM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
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?

Last edited by Ephracis; 02-21-2007 at 08:27 AM.
 
Old 02-21-2007, 08:58 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 02-21-2007, 09:09 AM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
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..

Code:
class MyDialog : public QDialog
{
    Q_OBJECT
    public:
        MyDialog(QWidget *parent=0);
        void setName(QString _name) { m_name = _name; }
        QString name() { return m_name; }
    private:
        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())
        reject();
    
    setName(nameEdit->text());
    accept();
}
What's wrong here?
 
Old 02-21-2007, 11:49 AM   #8
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
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
Code:
MyDialog dialog(this);

QStringList list;
list << "Item1";
list << "Second item";
dialog.setList(list);

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
make args in portinstall breaks config dialogs matiasquestions *BSD 1 09-09-2006 10:23 AM
LXer: Create GUI dialogs for GNOME and KDE LXer Syndicated Linux News 0 07-26-2006 01:54 PM
kde open/save dialogs consume 50% CPU... Sammael Suse/Novell 0 06-22-2006 06:23 AM
Signal 11 error dialogs linus321 Linux - Software 5 05-28-2005 07:24 AM
Mplayer Works, But Gives Error Dialogs mac57 Mandriva 3 03-17-2005 01:02 PM


All times are GMT -5. The time now is 09:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration