Results 1 to 5 of 5

Thread: How to display widget in calling method, called from main

  1. #1
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to display widget in calling method, called from main

    Hi, this is just a general question, I've been wondering, is it possible if I call a method from main which displays a widget.

    Recently, I tried writing a simple code for quick coding. I wrote a main method, and decided to call another method to draw widgets. Problem is, the widget will not
    stay and display it, even though I directed the widget to be shown (using widget->show()) in the calling method.

    My guess is that main method is the only one that called QApplication and asked it to perform event exec(), waiting for exit signal or something, while the calling method is only executed once and on-the-go (no wait flag or anything). Can anyone explaina bit further on this?

    Also, if I decided to code from main, and then calling in methods that draws widgets, how can I make them stay displayed? Is there any function to keep the widget being displayed?

    Here is a sample code snippet:

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QPushButton>
    4.  
    5.  
    6. void newWindow()
    7. {
    8. main.show();
    9. }
    10.  
    11.  
    12. int main(int argc, char*argv[])
    13. {
    14. QApplication app(argc,argv);
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    The problem with this code is that, the widget won't display at all, or more accurately, it'll display the widget when entering method newWindow, but when returning to main, it'll be destroyed.

    Is there some way to make the QMainWindow still being displayed, even after returning to main method?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to display widget in calling method, called from main

    The "QMainWindow main" goes out of scope when the execution returns from newWindow(). You would have to create the main window on the heap (with keyword "new") to keep it's instance around. For example:
    Qt Code:
    1. void newWindow()
    2. {
    3. QMainWindow* main = new QMainWindow;
    4. main->setAttribute(QT::WA_DeleteOnClose);
    5. main->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display widget in calling method, called from main

    Thank you for the feedback, unfortunately, the widget in the calling method will still be destroyed after returning to main method. I don't think there is anyway to do it either.

    The reason why I wanted to do this is because I've coded several methods, and wanted to call those methods in main. Some of the methods constructs a set of widgets.
    The methods that did not require widgets display works fine, but those that do, experience this problem.

    Hmm.. I think the only way to solve this. is for all those methods that constructs widgets inside to be created as classes instantiating QMainWindow or something.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to display widget in calling method, called from main

    Maybe it's just the problem with the example, but main() in the first post is not calling newWindow()?

    Here are a couple of examples. Two windows appear with self-explanatory titles:
    Qt Code:
    1. // main.cpp
    2. #include <QApplication>
    3. #include <QMainWindow>
    4.  
    5. void newWindow()
    6. {
    7. QMainWindow* window = new QMainWindow;
    8. window->setWindowTitle("Created in newWindow()");
    9. window->setAttribute(Qt::WA_DeleteOnClose);
    10. window->show();
    11. }
    12.  
    13. QMainWindow* createWindow()
    14. {
    15. QMainWindow* window = new QMainWindow;
    16. window->setWindowTitle("Created in createWindow()");
    17. window->setAttribute(Qt::WA_DeleteOnClose);
    18. return window;
    19. }
    20.  
    21. int main(int argc, char* argv[])
    22. {
    23. QApplication a(argc, argv);
    24. newWindow();
    25. QMainWindow* window = createWindow();
    26. window->show();
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    kaydknight (10th March 2007)

  6. #5
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display widget in calling method, called from main

    Opps! Silly me, I did not include newWindow in the first post.

    Emmm, anyway, as per previous situation, I did call method newWindow from main, Just that it was still destroyed after returning to main.

    But you know what's weird? I tried running your original solution again, and this time it worked!!! I don't know why. I suspect it must have something to do with not cleaning my previous built project solution (I use vc2005 express) and kept some of the old settings, or something else maybe

    Well, the example you showed now works! I'm also able to add other widgets into the main widget. Thank you very much!
    Last edited by kaydknight; 10th March 2007 at 19:34.

Similar Threads

  1. Display multiple Images in a widget
    By jeetu_happy in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2007, 12:24
  2. QTabWidget Placement and Display
    By mclark in forum Newbie
    Replies: 1
    Last Post: 16th January 2007, 21:34
  3. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 11:35
  4. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 13:07
  5. Replies: 3
    Last Post: 12th April 2006, 09:20

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.