[Qt-interest] Basic signals between classes question

Jeroen De Wachter Jeroen.DeWachter at elis.ugent.be
Tue Jul 6 10:13:18 CEST 2010


Hi Phil,

I don't know how custom your dialog is, but if you just need to get a
text string from the user, is there any reason why you couldn't just use
the getText convenience function in QInputDialog?

(copy paste from http://doc.trolltech.com/4.6/qinputdialog.html)

bool ok;
QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                     tr("User name:"), QLineEdit::Normal,
                                     QDir::home().dirName(), &ok);
if (ok && !text.isEmpty())
    textLabel->setText(text);

QInputDialog also provides lots of signals you can connect to your class
that notify you when the input value changes, should you want to do
that.

Kind regards,

Jeroen


On Tue, 2010-07-06 at 17:44 +1000, Phil wrote:
> Thank you for reading this.
> 
> This is another of those unresolved problems that I have never been able to 
> solve although I have found a work around in the past, but not this time.
> 
> I have a main window and a dialog box that contains a line edit box. I'm
> trying to pass the contents of the line edit box back to the main window, the 
> parent.
> 
> A signal / slot mechanism like this seems be the logical option but "parent" 
> is null for some reason. There must be a simple way around this.
> 
> connect(m_ui->elevationLineEdit, SIGNAL(textChanged(QString)), parent, 
> SLOT(updateParkElevationSlot(QString)));
> 
> The main class looks like this:
> 
> Pest::Pest (QWidget* parent, Qt::WFlags fl)
>                 : QMainWindow (parent, fl), Ui::MainWindow(),
>                     m_workerThread(new PollingThread(this))
> 
> Another method tried was to use a signal / slot connection within the dialog 
> class itself to read the edit box and then have a main class function retrieve 
> the variable el (as a reference) as follows:
>  
> void Pest::showRotatorDlg()
> {
>     QString el;
> 
>     if(rotatorDlg->showDialog())
>        rotatorDlg->getParkAngels(el);
> }
> 
> The slot does retrieve the contents of the edit box but it seems that after 
> the OK button on the dialog box is pressed that a new instance of the 
> rotatorDlg class is created and so el then contains "".
> 
> I have read through the documentation and many of the examples but I 
> cannot see a solution to this problem.
> 
> As always, any help offered will be greatly appreciated.
> 





More information about the Qt-interest-old mailing list