Results 1 to 3 of 3

Thread: Creating a widget AFTER the window has loaded :)

  1. #1
    Join Date
    Apr 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Arrow Creating a widget AFTER the window has loaded :)

    Hi, so being a newbie at Qt (and a bit with C++) I've launched into this crazy DirectX project in Visual Studio using Qt. I don't like using the designer so I deleted my .UI file and ended up getting more control over my UI.h file. So now in my project I got 3 files:


    1. studio.cpp
    2. studio.h
    3. EditorUI.h


    (I combined my main.cpp into studio.cpp to make things simpler)

    In my studio.cpp I have to create a custom DirectX widget but it has to be created AFTER the window has been loaded and created. So I added it to my entry point, like so:

    Qt Code:
    1. //Show the main window
    2. Studio MainWindow;
    3. MainWindow.show();
    4.  
    5.  
    6. //Create our DirectX widget
    7. MainWindow.CreateViewport();
    To copy to clipboard, switch view to plain text mode 

    Right before the entry point I declare CreateViewport();

    Qt Code:
    1. void Studio::CreateViewport()
    2. {
    3. //Instead of creating the DirectX widget, let's see if we can create a simple button on runtime
    4. BtnInitDevice = new QPushButton(this);
    5. BtnInitDevice->setObjectName(QString::fromUtf8("BtnInitDevice"));
    6. BtnInitDevice->setGeometry(QRect(20, 545, 200, 23));
    7. BtnInitDevice->winId();
    8. BtnInitDevice->setText(QApplication::translate("StudioClass", "Initialize DirectX11 Device", 0, QApplication::UnicodeUTF8));
    9.  
    10. /* DirectX widget
    11. //Aristos Portal
    12. MainViewport = new AristosPortal(this);
    13. MainViewport->PortalHandle = MainViewport->winId();
    14.  
    15. MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
    16. MainViewport->setGeometry(QRect(20, 60, 640, 480));
    17. MainViewport->setAutoFillBackground(false);
    18. MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
    19.  
    20. //Must be after the window has been created!!!
    21. MainViewport->startAristos();
    22. */
    23. }
    To copy to clipboard, switch view to plain text mode 

    As you can see in the code above, I didn't try to create the custom DirectX widget, instead I try to do a button.

    This function gets executed right after MainWindow.show(); but it does not create the button! I also tried creating a QWidget window but it flashes and appears only for a split second, it then vanishes!

    So my question is whether or not am I using the correct procedure to create a widget after the window has been created?

    Many thanks,

    PS: Here is my full code

    studio.cpp
    Qt Code:
    1. #include "studio.h"
    2. #include <QtGui>
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QWidget>
    5. #include "AristosPortal.h"
    6.  
    7. //Some Declarations
    8. QPushButton *BtnInitDevice;
    9. AristosPortal *MainViewport;
    10.  
    11.  
    12. Studio::Studio(QWidget *parent, Qt::WFlags flags)
    13. : QMainWindow(parent, flags)
    14. {
    15. //Create the UI
    16. ui.setupUi(this);
    17.  
    18. }
    19.  
    20.  
    21. //Tries to only create a button
    22. void Studio::CreateViewport()
    23. {
    24.  
    25. BtnInitDevice = new QPushButton(this);
    26. BtnInitDevice->setObjectName(QString::fromUtf8("BtnInitDevice"));
    27. BtnInitDevice->setGeometry(QRect(20, 545, 200, 23));
    28. BtnInitDevice->winId();
    29. BtnInitDevice->setText(QApplication::translate("StudioClass", "Initialize DirectX11 Device", 0, QApplication::UnicodeUTF8));
    30.  
    31. /*
    32. //Aristos Portal
    33. MainViewport = new AristosPortal(this);
    34. MainViewport->PortalHandle = MainViewport->winId();
    35.  
    36. MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
    37. MainViewport->setGeometry(QRect(20, 60, 640, 480));
    38. MainViewport->setAutoFillBackground(false);
    39. MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
    40.  
    41. //Must be after the window has been created!!!
    42. MainViewport->startAristos();
    43. */
    44. }
    45.  
    46. Studio::~Studio()
    47. {
    48. //Fires when the app closes?
    49. }
    50.  
    51.  
    52.  
    53. //Entry point to the applicaton
    54.  
    55. int main(int argc, char *argv[])
    56. {
    57. QApplication::setStyle(new QPlastiqueStyle);
    58. QApplication a(argc, argv);
    59.  
    60.  
    61. Studio MainWindow;
    62. MainWindow.show();
    63.  
    64.  
    65. //Create our viewport (or button, whatever)
    66. MainWindow.CreateViewport();
    67.  
    68. // This creates a window that flashes. Why? Why!?
    69. //QWidget MyWidget;
    70. //MyWidget.show();
    71.  
    72.  
    73. return a.exec();
    74. }
    To copy to clipboard, switch view to plain text mode 

    studio.h
    Qt Code:
    1. #ifndef STUDIO_H
    2. #define STUDIO_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6.  
    7. #include "EditorUI.h"
    8.  
    9.  
    10. class Studio : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Studio(QWidget *parent = 0, Qt::WFlags flags = 0);
    16. ~Studio();
    17. void CreateViewport();
    18.  
    19.  
    20.  
    21.  
    22. private:
    23. Ui::StudioClass ui;
    24.  
    25. };
    26.  
    27. #endif // STUDIO_H
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Creating a widget AFTER the window has loaded :)

    Sure it creates the QPushButton object. However, your code does not show() that widget or add to the layout of a widget that is already being shown (whatever it is that setupUi() builds). Consequently, the widget is not visible.

    Other notes:
    • The declarations at lines 8-9 of your CPP files should be member variables of the Studio class.
    • Line 28 of your cpp file listing is probably pointless.

  3. #3
    Join Date
    Apr 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Cool Re: Creating a widget AFTER the window has loaded :)

    Thank you so much! I didn't know you add to "show" or "add" the widget to something. I just thought that if you provided the window or widget handle at it's creation it would create it as a child window to that handle.

    BTW: Line 28 makes Qt draw the widgets as windows so each one has a window handle. It's necessary for directX

Similar Threads

  1. Replies: 0
    Last Post: 11th September 2011, 12:19
  2. Replies: 2
    Last Post: 8th September 2010, 02:09
  3. Creating a Window help
    By doforumda in forum Newbie
    Replies: 3
    Last Post: 12th July 2010, 23:59
  4. Replies: 2
    Last Post: 29th March 2010, 11:23
  5. Creating a Main Window Application
    By gt.beta2 in forum Qt Tools
    Replies: 13
    Last Post: 24th February 2009, 19:45

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.