PDA

View Full Version : How to emit QLineEdit's text after pressing a QPushButton in a modeless QDialog



Wasabi
14th September 2010, 19:40
The title pretty much states it all.

I have a modeless dialog in my application, and within it is a QLineEdit object and a QPushButton. How can I emit the value of the QLineEdit object (i.e. the user input) to the rest of the application when the user pressed the QPushButton (i.e. "Ok")?

If it were a modal dialog, then it'd simply be the return value of the calling function, but being modeless... any suggestions?

tbscope
14th September 2010, 19:46
Simply emit a signal from the slot connected to the Ok button.

Then in your program connect a slot to this signal.

Wasabi
14th September 2010, 20:18
Please excuse my ignorance, but I'm not quite following. To keep things simple, here's the code:

bool MyCanvas::HorizonDialog()
{
QDialog* dlg = new QDialog(this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->setWindowTitle("New Horizon");
QHBoxLayout* layout = new QHBoxLayout;
QPushButton* ok = new QPushButton("OK",dlg);
ok->setDefault(true);
connect(ok,SIGNAL(clicked()),dlg,SLOT(close()));
connect(ok,SIGNAL(clicked()),this,SLOT(NewHorizon( )));
QPushButton* cancel = new QPushButton("Cancel",dlg);
QLineEdit* text = new QLineEdit(dlg);
layout->addWidget(text);
layout->addWidget(ok);
layout->addWidget(cancel);
dlg->setLayout(layout);
dlg->raise();
dlg->show();
return true;
}

Are you suggesting placing a

connect(ok,SIGNAL(clicked()), //.... I honestly don't know what to connect that signal to.

wysota
14th September 2010, 20:58
tbscope is suggesting to subclass QDialog, move most of the code from your HorizonDialog() method to its constructor and add a signal to the class. Besides the signal add a slot that will be connected to your ok button's clicked signal. From within that slot emit the dialog signal and make it carry the value you want.

Wasabi
15th September 2010, 00:22
Ah, yes indeed, that would do the trick, wouldn't it?