PDA

View Full Version : Creating a widget AFTER the window has loaded :)



aaron1a12
17th April 2012, 18:37
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();


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 = 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();
*/
}


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
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();
}


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

ChrisW67
18th April 2012, 00:42
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.

aaron1a12
18th April 2012, 21:19
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