callum perks
callum perks

Reputation: 97

Qt subclassing a ui form shows a blank window

I have a qt .ui form and I am trying to use the subclassing approach described on their website to use it in a program. However, when I run the program, i just get an empty window.

subclass header:

#ifndef HOMEPAGE_H
#define HOMEPAGE_H
#include "ui_homepage.h"

class HomePage : public QWidget, public Ui::HomePage
{
public:
    HomePage(QMainWindow* window);
};

#endif // HOMEPAGE_H

subclass cpp file:

#include "homepage.h"

HomePage::HomePage(QMainWindow* window)
{
    setupUi(window);
}

program file:

#include <QApplication>
#include "homepage.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow *window = new QMainWindow();

    HomePage homepage(window);
    homepage.show();

    return app.exec();
}

Upvotes: 1

Views: 297

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

It should be like this:

HomePage::HomePage(QMainWindow* window) : QWidget(parent)
{
    setupUi(this);
}

You're calling setupUi on the parent.

I would add the Q_OBJECT macro as well, if you're about to use signals and slots.

class HomePage : public QWidget, public Ui::HomePage
{
    Q_OBJECT
public:
    HomePage(QMainWindow* window);
};

Also, I would call show on both HomePage and QMainWindow:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow *window = new QMainWindow();

    HomePage homepage(window);
    homepage.show();
    window->show();

    return app.exec();
}

Upvotes: 3

Related Questions