PDA

View Full Version : Add a custom widget in code to a MainWindow



Steff
18th November 2009, 08:45
Hy there,

i am new to Qt and Creator and have the following problem.

I whant to add an OpenGL QGLWidget to a QGuiApplication that i have created with the new project wizard. Then I add a new QTWidget (QT Designer Form Class) also over the wizard and change


class gl : public QWidget {

to


class gl : public QGLWidget {

in .h and .cpp

i alos add


#include "QGLWidget"

in the .h file

Now i need to create a new instance of my class here and connect it to the ui


GLTest::GLTest(QWidget *parent)
: QMainWindow(parent), ui(new Ui::GLTest)
{
// have to add it here
ui->setupUi(this);
// or here and how ?
}

But i do not get it to work.
Can you give me an smale example?
I know that i have to add it to the CentraWidget.

milot
18th November 2009, 15:05
Hi Steff,

You have to create an instance of gl class like the following:



gl *g = new gl();


where gl is the class that I've seen from your code.

The complete code from your question will be as follows:



GLTest::GLTest(QWidget *parent)
: QMainWindow(parent), ui(new Ui::GLTest)
{

ui->setupUi(this);
gl *g = new gl();

setCentralWidget(g); // where g is your instance of gl class.
}



If you have any further questions, please don't hesitate to ask :)

Steff
19th November 2009, 08:17
Thank you for the fast reply.

Becaus of the defination of the Widget is:


class gl : public QGLWidget {
Q_OBJECT
public:
gl(QWidget *parent = 0);
~gl();

protected:
void changeEvent(QEvent *e);

private:
Ui::gl *m_ui;
};

I thought i must add the parent element to the creation call.


gl *g = new gl(ui);

or


gl *g = new gl(this);

But i din not try just to call it without this :)

Thank you :)