1

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();
}
callum perks
  • 77
  • 2
  • 8

1 Answers1

3

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();
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35