jeudi 30 juin 2016

QUiLoader widget not showing if loaded in derived QWidget

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *vLayout = new QVBoxLayout(this);

    QUiLoader loader;

    QFile file("untitled.ui");
    file.open(QFile::ReadOnly);
    QWidget *w = loader.load(&file);
//    MyWidget *w = new MyWidget(loader.load(&file));
    vLayout->addWidget(w);
    file.close();

    QPushButton *btB = new QPushButton;
    vLayout->addWidget(btB);
}

enter image description here

The code above produces the image above. But I would like to do something with the widget just loaded, that's why I derived MyWidget from QWidget. And there seems to be the problem.

If I replace QWidget by MyWidget then it doesn't show there anymore. The code below produces the image below.

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *vLayout = new QVBoxLayout(this);

    QUiLoader loader;

    QFile file("untitled.ui");
    file.open(QFile::ReadOnly);
//    QWidget *w = loader.load(&file);
    MyWidget *w = new MyWidget(loader.load(&file));
    vLayout->addWidget(w);
    file.close();

    QPushButton *btB = new QPushButton;
    vLayout->addWidget(btB);
}

enter image description here

Where mywidget.h:

#include <QWidget>

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

signals:

public slots:

};

And mywidget.cpp:

#include "mywidget.h"

#include <QDebug>
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    qDebug() << "do something here";
}

Why the widget (group box) isn't showing there? How to make it appear?

Aucun commentaire:

Enregistrer un commentaire