Hello. There is a class ComparisionTable inherited from QWidget with widgets: two QLabel and one QTableWidget.

The layout was spelled out in the constructor:

QVBoxLayout *pVBox = new QVBoxLayout(); pVBox->addWidget(lblIS, 0, Qt::AlignTop); pVBox->addWidget(lblOS, 0, Qt::AlignTop); QHBoxLayout *pHBox = new QHBoxLayout(this); pHBox->addWidget(pTable); pHBox->addLayout(pVBox); this->setLayout(pHBox); 

You need to create the PairwiseComp class, which will inherit from the above-written class, just add another QLabel and the necessary methods for it. Created:

 PairwiseComp::PairwiseComp(ComparisionTableData data, QString name, QWidget *parent) : ComparisionTable(data, parent) { lblName = new QLabel(this); lblName->setText("<font color=blue>"+name+"</font>"); 

And it will be necessary to change the layout. How can I change the layout? Simply adding to the constructor is not an option.

  • Just in the ComparisionTable class, do not make a lineup in the constructor, but make it as a separate virtual function. Then all methods will be preserved. And in the inherited class, override it with a new lineup. - Madisson
  • @Madisson created. Made virtual function, redefined. But all the same, the layout starts from the parent class first, and then comes from the override, only with an error, saying that there is already a layer. - Di3go

1 answer 1

test.h

 #ifndef TEST_H #define TEST_H #include <QDialog> #include <QLabel> #include <QHBoxLayout> namespace Ui { class Test; } class Test : public QDialog { Q_OBJECT public: explicit Test(QWidget *parent = 0); void method1(); void method2(); void method3(); virtual void DrawScreen(); ~Test(); private: Ui::Test *ui; }; #endif // TEST_H 

test.cpp

 #include "test.h" #include "ui_test.h" Test::Test(QWidget *parent) : QDialog(parent), ui(new Ui::Test) { ui->setupUi(this); } void Test::DrawScreen() { QHBoxLayout *lay = new QHBoxLayout; QLabel *l1 = new QLabel("1234"); QLabel *l2 = new QLabel("5678"); lay->addWidget(l1); lay->addWidget(l2); setLayout(lay); show(); } Test::~Test() { delete ui; } 

pairwisecomp.h

 #ifndef PAIRWISECOMP_H #define PAIRWISECOMP_H #include <QWidget> #include <QVBoxLayout> #include "test.h" class PairwiseComp : public Test { Q_OBJECT public: explicit PairwiseComp(QWidget *parent = 0); void DrawScreen(); ~PairwiseComp(); }; #endif // PAIRWISECOMP_H 

pairwisecomp.cpp

 #include "pairwisecomp.h" PairwiseComp::PairwiseComp(QWidget *parent) : Test(parent) { } void PairwiseComp::DrawScreen() { QVBoxLayout *lay = new QVBoxLayout; QLabel *l1 = new QLabel("1234"); QLabel *l2 = new QLabel("5678"); QLabel *l3 = new QLabel("1234"); lay->addWidget(l1); lay->addWidget(l2); lay->addWidget(l3); setLayout(lay); show(); } PairwiseComp::~PairwiseComp() { } 

This example is quite efficient. Change what and how you need.