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:
- studio.cpp
- studio.h
- 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:
//Show the main window
Studio MainWindow;
MainWindow.show();
//Create our DirectX widget
MainWindow.CreateViewport();
//Show the main window
Studio MainWindow;
MainWindow.show();
//Create our DirectX widget
MainWindow.CreateViewport();
To copy to clipboard, switch view to plain text mode
Right before the entry point I declare CreateViewport();
void Studio::CreateViewport()
{
//Instead of creating the DirectX widget, let's see if we can create a simple button on runtime
BtnInitDevice
->setObjectName
(QString::fromUtf8("BtnInitDevice"));
BtnInitDevice
->setGeometry
(QRect(20,
545,
200,
23));
BtnInitDevice->winId();
BtnInitDevice
->setText
(QApplication::translate("StudioClass",
"Initialize DirectX11 Device",
0,
QApplication::UnicodeUTF8));
/* DirectX widget
//Aristos Portal
MainViewport = new AristosPortal(this);
MainViewport->PortalHandle = MainViewport->winId();
MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
MainViewport->setGeometry(QRect(20, 60, 640, 480));
MainViewport->setAutoFillBackground(false);
MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
//Must be after the window has been created!!!
MainViewport->startAristos();
*/
}
void Studio::CreateViewport()
{
//Instead of creating the DirectX widget, let's see if we can create a simple button on runtime
BtnInitDevice = new QPushButton(this);
BtnInitDevice->setObjectName(QString::fromUtf8("BtnInitDevice"));
BtnInitDevice->setGeometry(QRect(20, 545, 200, 23));
BtnInitDevice->winId();
BtnInitDevice->setText(QApplication::translate("StudioClass", "Initialize DirectX11 Device", 0, QApplication::UnicodeUTF8));
/* DirectX widget
//Aristos Portal
MainViewport = new AristosPortal(this);
MainViewport->PortalHandle = MainViewport->winId();
MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
MainViewport->setGeometry(QRect(20, 60, 640, 480));
MainViewport->setAutoFillBackground(false);
MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
//Must be after the window has been created!!!
MainViewport->startAristos();
*/
}
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
#include "studio.h"
#include <QtGui>
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include "AristosPortal.h"
//Some Declarations
AristosPortal *MainViewport;
Studio
::Studio(QWidget *parent, Qt
::WFlags flags
){
//Create the UI
ui.setupUi(this);
}
//Tries to only create a button
void Studio::CreateViewport()
{
BtnInitDevice
->setObjectName
(QString::fromUtf8("BtnInitDevice"));
BtnInitDevice
->setGeometry
(QRect(20,
545,
200,
23));
BtnInitDevice->winId();
BtnInitDevice
->setText
(QApplication::translate("StudioClass",
"Initialize DirectX11 Device",
0,
QApplication::UnicodeUTF8));
/*
//Aristos Portal
MainViewport = new AristosPortal(this);
MainViewport->PortalHandle = MainViewport->winId();
MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
MainViewport->setGeometry(QRect(20, 60, 640, 480));
MainViewport->setAutoFillBackground(false);
MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
//Must be after the window has been created!!!
MainViewport->startAristos();
*/
}
Studio::~Studio()
{
//Fires when the app closes?
}
//Entry point to the applicaton
int main(int argc, char *argv[])
{
Studio MainWindow;
MainWindow.show();
//Create our viewport (or button, whatever)
MainWindow.CreateViewport();
// This creates a window that flashes. Why? Why!?
//QWidget MyWidget;
//MyWidget.show();
return a.exec();
}
#include "studio.h"
#include <QtGui>
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include "AristosPortal.h"
//Some Declarations
QPushButton *BtnInitDevice;
AristosPortal *MainViewport;
Studio::Studio(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
//Create the UI
ui.setupUi(this);
}
//Tries to only create a button
void Studio::CreateViewport()
{
BtnInitDevice = new QPushButton(this);
BtnInitDevice->setObjectName(QString::fromUtf8("BtnInitDevice"));
BtnInitDevice->setGeometry(QRect(20, 545, 200, 23));
BtnInitDevice->winId();
BtnInitDevice->setText(QApplication::translate("StudioClass", "Initialize DirectX11 Device", 0, QApplication::UnicodeUTF8));
/*
//Aristos Portal
MainViewport = new AristosPortal(this);
MainViewport->PortalHandle = MainViewport->winId();
MainViewport->setObjectName(QString::fromUtf8("MainViewport"));
MainViewport->setGeometry(QRect(20, 60, 640, 480));
MainViewport->setAutoFillBackground(false);
MainViewport->setStyleSheet(QString::fromUtf8("background-color:red;"));
//Must be after the window has been created!!!
MainViewport->startAristos();
*/
}
Studio::~Studio()
{
//Fires when the app closes?
}
//Entry point to the applicaton
int main(int argc, char *argv[])
{
QApplication::setStyle(new QPlastiqueStyle);
QApplication a(argc, argv);
Studio MainWindow;
MainWindow.show();
//Create our viewport (or button, whatever)
MainWindow.CreateViewport();
// This creates a window that flashes. Why? Why!?
//QWidget MyWidget;
//MyWidget.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
studio.h
#ifndef STUDIO_H
#define STUDIO_H
#include <QtGui/QMainWindow>
#include "EditorUI.h"
{
Q_OBJECT
public:
Studio
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~Studio();
void CreateViewport();
private:
Ui::StudioClass ui;
};
#endif // STUDIO_H
#ifndef STUDIO_H
#define STUDIO_H
#include <QtGui/QMainWindow>
#include "EditorUI.h"
class Studio : public QMainWindow
{
Q_OBJECT
public:
Studio(QWidget *parent = 0, Qt::WFlags flags = 0);
~Studio();
void CreateViewport();
private:
Ui::StudioClass ui;
};
#endif // STUDIO_H
To copy to clipboard, switch view to plain text mode
Bookmarks