Results 1 to 7 of 7

Thread: unable to pop a dialog

  1. #1
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default unable to pop a dialog

    Hi All,


    I am attempting to write a function that pops a dialog. My ultimate goal is to call this function via signal/slot. But for now I am just trying to validate that the function will pop a dialog.

    I designed a toy dialog for testing purposes. Here is the code for the dialog:

    Qt Code:
    1. class ShareDialog : public QDialog
    2. {
    3. //Q_OBJECT
    4. public:
    5. ShareDialog(QWidget *parent=0);
    6.  
    7. private:
    8. QPushButton *exitButton;
    9. QLabel *label;
    10. };
    11.  
    12. ShareDialog::ShareDialog(QWidget *parent) : QDialog(parent)
    13. {
    14. label = new QLabel("Hello! This this is the 'Select' dialog!");
    15. exitButton = new QPushButton("Bugout!");
    16.  
    17. connect(exitButton, SIGNAL(clicked()), this, SLOT(close()) );
    18.  
    19. QHBoxLayout * theLayout = new QHBoxLayout;
    20. theLayout->addWidget(label);
    21. theLayout->addWidget(exitButton);
    22. setLayout(theLayout);
    23. }
    To copy to clipboard, switch view to plain text mode 

    The dialog works as expected if I invoke it from main():

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. ShareDialog * shareDialog = new ShareDialog;
    6. shareDialog->show();
    7.  
    8. HistoryWindow w;
    9.  
    10. w.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    When I invoke show() from main it works just as I expected. But, when I attempt to the same thing from a different function it crashes the app:

    Qt Code:
    1. void HistoryWindow::PopShareDialog()
    2. {
    3. if (!dlg)
    4. {
    5. dlg = new ShareDialog(this);
    6. }
    7.  
    8. Q_ASSERT( dlg != NULL);
    9. dlg->show(); // app crashes if this executes
    10. //shareDialog->raise();
    11. //shareDialog->activateWindow();
    12. }
    13.  
    14. HistoryWindow::HistoryWindow(QWidget *parent) : QMainWindow(parent),
    15. ui(new Ui::HistoryWindow)
    16. {
    17. ui->setupUi(this);
    18. createModelAndView();
    19.  
    20. PopShareDialog(); // invoking the dialog from here doesn't work
    21. }
    To copy to clipboard, switch view to plain text mode 

    I do not understand why invoking show() from within PopShareDialog() causes a crash. Does anyone have a clue?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: unable to pop a dialog

    Post a cout or qDebug() output in if, to see if the pointer is initialized:
    Qt Code:
    1. if (!dlg)
    2. {
    3. std::cout << "About to initialize the dialog\n";
    4. dlg = new ShareDialog(this);
    5. }
    To copy to clipboard, switch view to plain text mode 
    In C++ the pointers are not initialized with NULL when they are declared, some compilers might do that (in release build) but it is still not safe to assume that.

  3. #3
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to pop a dialog

    FYI, an uninitialized pointer does not have to be 0. It can be anything. Just because it is not null, does not mean it is valid. Checking for null only makes sense if you previously initialized it to 0. Is dlg a global or what? How is it initialized (if at all)?
    Last edited by Timoteo; 2nd December 2010 at 19:17. Reason: Bah, Zlatomir beat me to it

  4. The following user says thank you to Timoteo for this useful post:

    ajax (2nd December 2010)

  5. #4
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unable to pop a dialog

    DOH! (slaps forehead)

    I forgot to init the dlg member to NULL.

    (curses self)

    Thank you, Timoteo. Good catch.


    Added after 6 minutes:


    Hmmm....

    std::cout << "some text\n";
    Isn't working for me. Not even from within main():

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. std::cout << "yoohoo!\n" << endl;
    5.  
    6. HistoryWindow w;
    7.  
    8. w.show();
    9.  
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    I have only been working with Qt for about a week and this is the first time I attempted to use std::cout.

    I expected to see the output in the "application output" window. I also ran the app from the command line and expected to see something barfed back to the screen. It didn't happen.

    Why wouldn't I get something back in the "application output" window?
    Last edited by ajax; 2nd December 2010 at 19:43.

  6. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to pop a dialog

    qDebug() does go to the application output window in Creator. You can use that. As for why std::cout doesn't do the same, it is because you need an actual console for that.
    Qt Code:
    1. CONFIG += console
    To copy to clipboard, switch view to plain text mode 

    Creator is passing /susbsystem:windows to the linker (you are using windows, right?) instead of /subsystem:console when you specifiy
    Qt Code:
    1. QT += gui
    To copy to clipboard, switch view to plain text mode 
    by default. Which makes stdout not exist.

    Clarification: qmake is what ends up passing this to the linker, not Creator. Still the same concept.

    Second edit: The 2nd code fragment originally read "CONFIG += gui" and this is wrong. Changed for those who might find this later so it isn't confusing. I guess I do need that 2nd pot of coffee.
    Last edited by Timoteo; 2nd December 2010 at 21:07. Reason: clarification + random wrongness on my part

  7. The following user says thank you to Timoteo for this useful post:

    ajax (2nd December 2010)

  8. #6
    Join Date
    Nov 2010
    Location
    Houston, Texas, USA
    Posts
    30
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unable to pop a dialog

    Thanks Timoteo! That worked like a charm!

    Yes, I am developing on Windows, for now.

    It's going to be very nice having cout around the house.

  9. #7
    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: unable to pop a dialog

    Try to familarize yourself with qDebug(), it's much more useful than cout when it comes to Qt apps.
    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.


Similar Threads

  1. Replies: 0
    Last Post: 5th October 2010, 07:41
  2. closing child dialog closes parent dialog
    By sparticus_37 in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 19:46
  3. How to blur parent dialog when child dialog is displayed
    By abhilashajha in forum Qt Programming
    Replies: 4
    Last Post: 10th June 2009, 13:01
  4. Dialog to Dialog communication
    By jjbabu in forum Qt Tools
    Replies: 1
    Last Post: 1st October 2008, 10:54
  5. call the other dialog through dialog
    By narumi in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2008, 09:30

Tags for this Thread

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.