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