PDA

View Full Version : How to execute a qDialog without passing a parent



franco.amato
28th September 2014, 23:20
Good morning,
I have to show a qDialog from a class inherited from QObject so I can not pass a parent.
How can do in this case?

Regards,

Franco

wysota
29th September 2014, 00:21
Pass null as the parent.

franco.amato
29th September 2014, 02:06
Hi Wysota,
thank you. Yes I tried to pass null but I got a crash.
I paste come source code here.

The header file of dialog:

#ifndef PREFERENCESDIALOG_H
#define PREFERENCESDIALOG_H

#include "ui_PreferencesDialog.h"

class PreferencesDialog : public QDialog, private Ui::PreferencesDialog
{
Q_OBJECT

public:
int exec();

public:
explicit PreferencesDialog(QWidget* parent = 0);

private slots:
void restoreState();
void saveState();
};

#endif // PREFERENCESDIALOG_H

The ctor of the dialog

PreferencesDialog::PreferencesDialog(QWidget* parent) : QDialog(parent)
{
setupUi(this);

setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

QPushButton* resetButton = buttonBox->button(QDialogButtonBox::Reset);
connect(resetButton, SIGNAL(clicked()), SLOT(restoreState()));

databasePortEdit->setValidator(new QIntValidator(1024, 65536, this));

QSize sizeHint = this->sizeHint();
sizeHint.setWidth(350);
setMinimumSize(sizeHint);
setMaximumSize(sizeHint);
}

Here I construct the dialog:


EyeIdServer::EyeIdServer(QObject* parent) : QObject(parent)
{
...code..
// Initialize dialogs

m_preferencesDialog = new PreferencesDialog(0);
...more code..
}


and from this routine I execute it

void EyeIdServer::showPreferencesDialog()
{
m_preferencesDialog->exec();
}

Do you have any idea on why I get the crash?
I think is because I use the "this" pointer inside the dialog

Regards

wysota
29th September 2014, 08:42
It is not because you use "this", that is perfectly fine. Do you by any chance use threads in your application?

anda_skoa
29th September 2014, 08:43
Hi Wysota,
thank you. Yes I tried to pass null but I got a crash.

Unrelated. The parent argument of QDialog even defaults to 0.



Do you have any idea on why I get the crash?

No, but the backtrace would have given at least some hints.



I think is because I use the "this" pointer inside the dialog

No, this is always valid within a class' instance.

Btw, what's your reason for overwriting exec()?

Cheers,
_

aamer4yu
29th September 2014, 08:48
What error are you getting on crash ?
Also whats the code in restoreState() ? You are running m_preferencesDialog->exec(); and performing something in restoreState..
May be thats causing crash...

You could also simply check the value of m_preferencesDialog->exec() and perform necessary action.

franco.amato
29th September 2014, 16:13
Thank you very much to all

This is the restoreState code


void PreferencesDialog::restoreState()
{
// Restore database settings

databaseHostEdit->setText(preferences->databaseHost());
databasePortEdit->setText(QString::number(preferences->databasePort()));
databaseUsernameEdit->setText(preferences->databaseUsername());
databasePasswordEdit->setText(preferences->databasePassword());
databaseNameEdit->setText(preferences->databaseName());
}

Best

ChrisW67
30th September 2014, 22:22
What are buttonBox, databasePortEdit etc. and where are these pointers declared and initialised?

Edit: Ignore that... I just noticed you are inheriting the UI object.

However, your code will crash if the button box does not contain a standard ResetButton.