Results 1 to 12 of 12

Thread: Open subwindow from parent window ctor

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Open subwindow from parent window ctor

    Hi!
    I have a widget (or QDialog, does not matter) that should in some cases open a getOpenFilename dialog immediately.

    But if I do this getOpenFIlename call in the constructor, I get an error... WA_state_created or so.

    One Way aroud it i found already is to call

    QTimer::singleShow(200, this, SLOT(openFile())

    in the end of the constructor but I know that this can make problems and is jsut a hack.

    How can I solve this in a stable way?

    Is there an event or signal that informs me about when the dialog is ready to execute openFile() slot?

    Thanks for your help in advance!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    works fine
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QFileDialog::getOpenFileName();
    5. }
    To copy to clipboard, switch view to plain text mode 
    another example
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QWidget *w = new QWidget();
    5. w->setAttribute(Qt::WA_DeleteOnClose);
    6. w->show();
    7. }
    To copy to clipboard, switch view to plain text mode 
    without setting a parent.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    the reason why it doesnt work is that you are providing the constructor class as parent, right? if the dialog's parent isnt created, following assert fails:
    Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
    dont set a parent and it'll work

  4. #4
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open subwindow from parent window ctor

    Great!
    Thanks!
    For me it does not make a difference, but as expected, now *before* the parent dialog pops up, the fileopen diag shows, and just after it has been closed, the parent diag is shown.

    Just because I am interested:

    Would it also be possible to first pop up the parent, and to overlay the fileopen diage then?

    This is how it would happen by using my QTimer::singleShot workaround..

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    show in main() main window and then show a dialog. e.g.
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. ...
    4. MainWindow mw;
    5. mw.show();
    6. Dialog d;
    7. d.show();
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open subwindow from parent window ctor

    Hmm, I think I didn't explain prcisely enough what I mean.

    After your first replies, my (working) code looks as follows:

    Qt Code:
    1. class EditCustomerImageDialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. EditCustomerImageDialog(Customer * c, QWidget * parent = 0, Qt::WindowFlags f = 0)
    7. : QDialog(parent, f)
    8. {
    9. //...
    10. openFile();
    11. }
    12. public slots:
    13. void openFile()
    14. {
    15. QString filename = QFileDialog::getOpenFileName(
    16. NULL, tr("Open face image"), "",
    17. tr("Image Files (*.jpg)"));
    18. //...
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    If I now open such a dialog from somewhere via
    Qt Code:
    1. EditCustomerImageDialog d (new Customer);
    2. d.exec();
    To copy to clipboard, switch view to plain text mode 

    what happens is that *first*, the openFileName Dialog pops up, nothing else.

    *After* having chosen a file, and after the openfilediag has disappeared, then the EditCustomerDialog pops up.

    Question:
    Can I realise that the EditCustomerDialog pops up, and directly after that, the open FIle diag should pop up ???

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    did you try something like this?
    main.cpp
    Qt Code:
    1. ...
    2. int main(int argc, char **argv)
    3. {
    4. QApplication app(argc, argv);
    5. EditCustomerImageDialog ecid;
    6. ecid.show();
    7. const QString fileName(QFileDialog::getOpenFileName(
    8. NULL, tr("Open face image"), "",
    9. tr("Image Files (*.jpg)")));
    10. ecid.openFile(fileName);
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    EditCustomerImageDialog.cpp
    Qt Code:
    1. class EditCustomerImageDialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. EditCustomerImageDialog(Customer * c, QWidget * parent = 0, Qt::WindowFlags f = 0)
    7. : QDialog(parent, f)
    8. {
    9. //...
    10. //openFile();
    11. }
    12. public slots:
    13. void openFile(const QString &fileName = QString())
    14. {
    15. QString tmpFileName = fileName;
    16. if (tmpFileName.isEmpty())
    17. tmpFileName = QFileDialog::getOpenFileName(
    18. NULL, tr("Open face image"), "",
    19. tr("Image Files (*.jpg)"));
    20. //...
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    olidem (13th May 2009)

  9. #8
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open subwindow from parent window ctor

    I know what you mean, but QDialog::show() returns immediately, while my ::exec() realisation blocks the caller. Ok, one can discuss for hours about this, it is a matter of taste, but I like the blocking thing more, also because this is just a modal dialog on top of my app, which is blocked while the dialog is shown.

    Thanks again for your support, great people out here!

  10. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    ok, another solution
    cpp
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ...
    5. QMetaObject::invokeMethod(this, "showDialog", Qt::QueuedConnection);
    6. ...
    7. }
    8.  
    9. void Test::showDialog()
    10. {
    11. QFileDialog::getOpenFileName(this);
    12. }
    To copy to clipboard, switch view to plain text mode 
    h
    Qt Code:
    1. private slots:
    2. void showDialog();
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. The following user says thank you to spirit for this useful post:

    olidem (13th May 2009)

  12. #10
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    For this situation is usually use a single shot timer, put something like

    Qt Code:
    1. QTimer::singleShot(0, this, SLOT(openFile()));
    To copy to clipboard, switch view to plain text mode 

    at the end of your constructor for the main window. A time of zero will fire the slot after your window is visible.

    ps Hopefully the above code is correct i don't do much (any?) C++ put I think you'll get the idea.

  13. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    Quote Originally Posted by fifth View Post
    For this situation is usually use a single shot timer, put something like

    Qt Code:
    1. QTimer::singleShot(0, this, SLOT(openFile()));
    To copy to clipboard, switch view to plain text mode 

    at the end of your constructor for the main window. A time of zero will fire the slot after your window is visible.

    ps Hopefully the above code is correct i don't do much (any?) C++ put I think you'll get the idea.
    your code has the same behavior as my, because
    The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.
    but I don't use a timer.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #12
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Open subwindow from parent window ctor

    Quote Originally Posted by spirit View Post
    but I don't use a timer.
    You win I didn't know about QMetaObject but will probably use your method now. It is more obvious in the code what is happening.

    For PyQt, just need to make sure you include the signature for the slot.

    Qt Code:
    1. class MainWindow(QWidget):
    2. def __init__(self, parent = None):
    3. super(MainWindow, self).__init__(parent)
    4.  
    5. QMetaObject.invokeMethod(self, "showDialog", Qt.QueuedConnection);
    6.  
    7. @pyqtSignature("showDialog()")
    8. def showDialog(self):
    9. QFileDialog.getOpenFileName(self)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Best way to open a Qt window from web page?
    By montylee in forum Qt Programming
    Replies: 9
    Last Post: 19th January 2009, 09:04
  2. connecting child to parent window...
    By sumit in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2008, 22:28
  3. How to close parent window from child window?
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 12:40
  4. Getting a parent window from a QT/Linux library
    By nicolas44 in forum Qt Programming
    Replies: 8
    Last Post: 31st May 2007, 09:17
  5. move parent window to the front.
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2007, 09:41

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.