Results 1 to 8 of 8

Thread: How to execute a qDialog without passing a parent

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to execute a qDialog without passing a parent

    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
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to execute a qDialog without passing a parent

    Pass null as the parent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to execute a qDialog without passing a parent

    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:
    Qt Code:
    1. #ifndef PREFERENCESDIALOG_H
    2. #define PREFERENCESDIALOG_H
    3.  
    4. #include "ui_PreferencesDialog.h"
    5.  
    6. class PreferencesDialog : public QDialog, private Ui::PreferencesDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. int exec();
    12.  
    13. public:
    14. explicit PreferencesDialog(QWidget* parent = 0);
    15.  
    16. private slots:
    17. void restoreState();
    18. void saveState();
    19. };
    20.  
    21. #endif // PREFERENCESDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    The ctor of the dialog
    Qt Code:
    1. PreferencesDialog::PreferencesDialog(QWidget* parent) : QDialog(parent)
    2. {
    3. setupUi(this);
    4.  
    5. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    6.  
    7. QPushButton* resetButton = buttonBox->button(QDialogButtonBox::Reset);
    8. connect(resetButton, SIGNAL(clicked()), SLOT(restoreState()));
    9.  
    10. databasePortEdit->setValidator(new QIntValidator(1024, 65536, this));
    11.  
    12. QSize sizeHint = this->sizeHint();
    13. sizeHint.setWidth(350);
    14. setMinimumSize(sizeHint);
    15. setMaximumSize(sizeHint);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Here I construct the dialog:
    Qt Code:
    1. EyeIdServer::EyeIdServer(QObject* parent) : QObject(parent)
    2. {
    3. ...code..
    4. // Initialize dialogs
    5.  
    6. m_preferencesDialog = new PreferencesDialog(0);
    7. ...more code..
    8. }
    To copy to clipboard, switch view to plain text mode 

    and from this routine I execute it
    Qt Code:
    1. void EyeIdServer::showPreferencesDialog()
    2. {
    3. m_preferencesDialog->exec();
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    Regards
    Last edited by franco.amato; 29th September 2014 at 01:23.
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to execute a qDialog without passing a parent

    It is not because you use "this", that is perfectly fine. Do you by any chance use threads in your application?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to execute a qDialog without passing a parent

    Quote Originally Posted by franco.amato View Post
    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.

    Quote Originally Posted by franco.amato View Post
    Do you have any idea on why I get the crash?
    No, but the backtrace would have given at least some hints.

    Quote Originally Posted by franco.amato View Post
    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,
    _

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to execute a qDialog without passing a parent

    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.

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to execute a qDialog without passing a parent

    Thank you very much to all

    This is the restoreState code

    Qt Code:
    1. void PreferencesDialog::restoreState()
    2. {
    3. // Restore database settings
    4.  
    5. databaseHostEdit->setText(preferences->databaseHost());
    6. databasePortEdit->setText(QString::number(preferences->databasePort()));
    7. databaseUsernameEdit->setText(preferences->databaseUsername());
    8. databasePasswordEdit->setText(preferences->databasePassword());
    9. databaseNameEdit->setText(preferences->databaseName());
    10. }
    To copy to clipboard, switch view to plain text mode 

    Best
    Franco Amato

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to execute a qDialog without passing a parent

    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.
    Last edited by ChrisW67; 30th September 2014 at 21:28. Reason: updated contents

Similar Threads

  1. passing data between parent and child widget
    By rakefet in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2014, 23:26
  2. Passing a object between QDialog and QMainWindow
    By m_bishop in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2013, 14:53
  3. Replies: 3
    Last Post: 22nd September 2011, 05:47
  4. Replies: 7
    Last Post: 23rd January 2010, 22:59
  5. Passing event to parent
    By QPissedOff in forum Newbie
    Replies: 1
    Last Post: 26th April 2006, 16:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.