Widget::Widget(QWidget *parent) : QWidget(parent) {
file.open(QFile::ReadOnly);
QWidget *w = loader.load(&file);
// MyWidget *w = new MyWidget(loader.load(&file));
vLayout.addWidget(w);
file.close();
vLayout.addWidget(&bt);
}
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) {
file.open(QFile::ReadOnly);
// QWidget *w = loader.load(&file);
MyWidget *w = new MyWidget(loader.load(&file));
vLayout.addWidget(w);
file.close();
vLayout.addWidget(&bt);
}
Where MyWidget
:
// mywidget.h
#include <QWidget>
class MyWidget : public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
};
// mywidget.cpp
#include "mywidget.h"
#include <QDebug>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
qDebug() << "do something here";
}
Question 1: Why the widget (group box) isn't showing there? How to make it appear?
Ok, question 1 completed thanks to @KubaOber. Now I would like to access the widgets inside untitled.ui
, I tried:
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
foreach (QLabel *label, parent->findChildren<QLabel*>())
if(label->objectName().contains("Value"))
qDebug() << "label" << label;
}
But it returns nothing. Instead, passing a parent in loader.load(&file, vLayoutWidget))
makes the untitled.ui
widgets available but also craps the contents of vLayoutWidget
. I guess the question is what should be passed in as parent in the loader.load())
?