Inheritance of GroupBox
-
Hello! :-)
I have a simple example GUI-Application with a Group Box in the Tab 1 (see Screenshot). What I want now is the same Group Box in Tab 2 and I don't want to copy-paste the Group Box because if I want to do a change in the Group Box in Tab 1, the change should also be visible in Tab 2. I think inheritance is the solution. Can someone tell me where I should declare the second Group Box for Tab2?
Thank you :-)
-
One way to do it is to create only one group box and change its parent for display when tabs are switched. Then you will be able to see the exact same thing. No inheritance is involved.
Its default parent is tab1.
change its parent to tab2 after tab2 button is clicked.
change its parent to tab1 after tab1 button is clicked. -
Hi,
thank you, but behind the buttons in tab 1 and tab 2 shoud work different functions. I really need two Group Boxes.
-
Hi,
thank you, but behind the buttons in tab 1 and tab 2 shoud work different functions. I really need two Group Boxes.
@buzz_lightzyear In this case, you need inheritance to do that.
-
Hi again :-)
Ok, I tried it with inheritance. But my idea is not working :-(
I have created a simple class group box with buttons:
groupboxwithbuttons.cpp
#include "groupboxwithbuttons.h" #include "ui_groupboxwithbuttons.h" GroupBoxWithButtons::GroupBoxWithButtons(QWidget *parent) : QGroupBox(parent), ui(new Ui::GroupBoxWithButtons) { ui->setupUi(this); } GroupBoxWithButtons::~GroupBoxWithButtons() { delete ui; } void GroupBoxWithButtons::on_pushButton_clicked() { qDebug() << "Button1 clicked"; } void GroupBoxWithButtons::on_pushButton_2_clicked() { qDebug() << "Button2 clicked"; } void GroupBoxWithButtons::on_pushButton_3_clicked() { qDebug() << "Button3 clicked"; } void GroupBoxWithButtons::on_pushButton_4_clicked() { qDebug() << "Button4 clicked"; }
groupboxwithbuttons.h
#ifndef GROUPBOXWITHBUTTONS_H #define GROUPBOXWITHBUTTONS_H #include <QGroupBox> namespace Ui { class GroupBoxWithButtons; } class GroupBoxWithButtons : public QGroupBox { Q_OBJECT public: explicit GroupBoxWithButtons(QWidget *parent = nullptr); ~GroupBoxWithButtons(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); private: Ui::GroupBoxWithButtons *ui; }; #endif // GROUPBOXWITHBUTTONS_H
From this base class I have derived a class groupboxtab1.cpp for my tab1. I know that I only derive from the class, not from the ui... but how do I do that? Nothing is shown on my MainForm :-(
groupboxtab1.cpp
#include "groupboxtab1.h" #include "ui_groupboxtab1.h" GroupBoxTab1::GroupBoxTab1(QWidget *parent) : GroupBoxWithButtons(parent), ui(new Ui::GroupBoxTab1) { ui->setupUi(this); } GroupBoxTab1::~GroupBoxTab1() { delete ui; }
groupboxtab1.h
#ifndef GROUPBOXTAB1_H #define GROUPBOXTAB1_H #include "groupboxwithbuttons.h" #include <QGroupBox> namespace Ui { class GroupBoxTab1; } class GroupBoxTab1 : public GroupBoxWithButtons { Q_OBJECT public: explicit GroupBoxTab1(QWidget *parent = nullptr); ~GroupBoxTab1(); private: Ui::GroupBoxTab1 *ui; }; #endif // GROUPBOXTAB1_H
I have tried it with google, but it seems that there is not so much help for that... is there any other way with QT to solve this problem?
Thx in advance :-)
-
Hi again :-)
Ok, I tried it with inheritance. But my idea is not working :-(
I have created a simple class group box with buttons:
groupboxwithbuttons.cpp
#include "groupboxwithbuttons.h" #include "ui_groupboxwithbuttons.h" GroupBoxWithButtons::GroupBoxWithButtons(QWidget *parent) : QGroupBox(parent), ui(new Ui::GroupBoxWithButtons) { ui->setupUi(this); } GroupBoxWithButtons::~GroupBoxWithButtons() { delete ui; } void GroupBoxWithButtons::on_pushButton_clicked() { qDebug() << "Button1 clicked"; } void GroupBoxWithButtons::on_pushButton_2_clicked() { qDebug() << "Button2 clicked"; } void GroupBoxWithButtons::on_pushButton_3_clicked() { qDebug() << "Button3 clicked"; } void GroupBoxWithButtons::on_pushButton_4_clicked() { qDebug() << "Button4 clicked"; }
groupboxwithbuttons.h
#ifndef GROUPBOXWITHBUTTONS_H #define GROUPBOXWITHBUTTONS_H #include <QGroupBox> namespace Ui { class GroupBoxWithButtons; } class GroupBoxWithButtons : public QGroupBox { Q_OBJECT public: explicit GroupBoxWithButtons(QWidget *parent = nullptr); ~GroupBoxWithButtons(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); private: Ui::GroupBoxWithButtons *ui; }; #endif // GROUPBOXWITHBUTTONS_H
From this base class I have derived a class groupboxtab1.cpp for my tab1. I know that I only derive from the class, not from the ui... but how do I do that? Nothing is shown on my MainForm :-(
groupboxtab1.cpp
#include "groupboxtab1.h" #include "ui_groupboxtab1.h" GroupBoxTab1::GroupBoxTab1(QWidget *parent) : GroupBoxWithButtons(parent), ui(new Ui::GroupBoxTab1) { ui->setupUi(this); } GroupBoxTab1::~GroupBoxTab1() { delete ui; }
groupboxtab1.h
#ifndef GROUPBOXTAB1_H #define GROUPBOXTAB1_H #include "groupboxwithbuttons.h" #include <QGroupBox> namespace Ui { class GroupBoxTab1; } class GroupBoxTab1 : public GroupBoxWithButtons { Q_OBJECT public: explicit GroupBoxTab1(QWidget *parent = nullptr); ~GroupBoxTab1(); private: Ui::GroupBoxTab1 *ui; }; #endif // GROUPBOXTAB1_H
I have tried it with google, but it seems that there is not so much help for that... is there any other way with QT to solve this problem?
Thx in advance :-)
GroupBoxTab1::GroupBoxTab1(QWidget *parent) : GroupBoxWithButtons(parent), ui(new Ui::GroupBoxTab1) { ui->setupUi(this); }
you set two different ui for the same object !!?
-
Hello! :-)
I have a simple example GUI-Application with a Group Box in the Tab 1 (see Screenshot). What I want now is the same Group Box in Tab 2 and I don't want to copy-paste the Group Box because if I want to do a change in the Group Box in Tab 1, the change should also be visible in Tab 2. I think inheritance is the solution. Can someone tell me where I should declare the second Group Box for Tab2?
Thank you :-)
@buzz_lightzyear said in Inheritance of GroupBox:
I think inheritance is the solution
No, it's not.
You can't have same instance of a widget in two different locations at the same time.
You will have to use two different instances, one for tab1 and one for tab2 and synchronise the changes between them. -
@jsulm
ok thx... in the meantime I already thought thats not the right way... because I found nothing with google.But now I have the next problem... here is my mainwindow.cpp:
#include "mainwindow.h" #include "groupboxwithbuttons.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); GroupBoxWithButtons* gbwb = new GroupBoxWithButtons(this->ui->tab); GroupBoxWithButtons* gbwb2 = new GroupBoxWithButtons(this->ui->tab_2); } MainWindow::~MainWindow() { delete ui; }
I know the delete is missing ;-)
This shows me the buttons on my two tabs but how can I access the elements? For example I want to give a button another name... I hope anyone knows what I mean :-Dthx :-)
-
@jsulm
ok thx... in the meantime I already thought thats not the right way... because I found nothing with google.But now I have the next problem... here is my mainwindow.cpp:
#include "mainwindow.h" #include "groupboxwithbuttons.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); GroupBoxWithButtons* gbwb = new GroupBoxWithButtons(this->ui->tab); GroupBoxWithButtons* gbwb2 = new GroupBoxWithButtons(this->ui->tab_2); } MainWindow::~MainWindow() { delete ui; }
I know the delete is missing ;-)
This shows me the buttons on my two tabs but how can I access the elements? For example I want to give a button another name... I hope anyone knows what I mean :-Dthx :-)
@buzz_lightzyear said in Inheritance of GroupBox:
I know the delete is missing ;-)
There's no need to ...
Do the two groupbox instances share functionalities ?
-
Hello,
no, they do not. The idea behind that is a simple biking/hiking logger in which I can enter the data from my gps device. In one tab the bike tours, in the other tab the hiking tour. The gui is the same (with minimal text change on labels), but the data is stored in a different table. so there is no shared functionality.
good bye
-
@jsulm
ok thx... in the meantime I already thought thats not the right way... because I found nothing with google.But now I have the next problem... here is my mainwindow.cpp:
#include "mainwindow.h" #include "groupboxwithbuttons.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); GroupBoxWithButtons* gbwb = new GroupBoxWithButtons(this->ui->tab); GroupBoxWithButtons* gbwb2 = new GroupBoxWithButtons(this->ui->tab_2); } MainWindow::~MainWindow() { delete ui; }
I know the delete is missing ;-)
This shows me the buttons on my two tabs but how can I access the elements? For example I want to give a button another name... I hope anyone knows what I mean :-Dthx :-)
@buzz_lightzyear said in Inheritance of GroupBox:
how can I access the elements?
Simply make gbwb and gbwb2 member variables in MainWindow...
-
Thank you, but how do I give for example the button another name? If I make a member of the groupbox in the main window it only shows me the functions and members of groupbox... but I want to access the elements IN the group box.
thx :-)
-
Thank you, but how do I give for example the button another name? If I make a member of the groupbox in the main window it only shows me the functions and members of groupbox... but I want to access the elements IN the group box.
thx :-)
@buzz_lightzyear said in Inheritance of GroupBox:
but how do I give for example the button another name?
Why do you need this?
And why do you want to access internal implementation details of your group boxes outside of GroupBoxWithButtons? This is bad practise... -
The idea is to overwrite. Lets say there is a label with "biking" for tab 1 and a label for "hiking" on tab2. And for the future another tab with... for example... climbing ;-) but I want the same group box on all three tabs, but with little changes. Do you know what I mean? For this the first idea was to inherite the group box and overwrite.
thx :-)
-
The idea is to overwrite. Lets say there is a label with "biking" for tab 1 and a label for "hiking" on tab2. And for the future another tab with... for example... climbing ;-) but I want the same group box on all three tabs, but with little changes. Do you know what I mean? For this the first idea was to inherite the group box and overwrite.
thx :-)
@buzz_lightzyear said in Inheritance of GroupBox:
Do you know what I mean?
Not really. Now you're talking about labels.
If you mean you want to trigger specific actions if a button in one of the groups is pressed then do it like is always done in Qt: emit a signal in GroupBoxWithButtons if a button is pressed and connect this signal to a slot in MainWindow to do what ever you need:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); gbwb = new GroupBoxWithButtons(this->ui->tab); connect(gbwb, &GroupBoxWithButtons::button1Pressed, this, &MainWindow::doWhateverYouWant);
-
The idea is to overwrite. Lets say there is a label with "biking" for tab 1 and a label for "hiking" on tab2. And for the future another tab with... for example... climbing ;-) but I want the same group box on all three tabs, but with little changes. Do you know what I mean? For this the first idea was to inherite the group box and overwrite.
thx :-)
@buzz_lightzyear said in Inheritance of GroupBox:
but I want the same group box on all three tabs, but with little changes.
Firstly, everything as @jsulm has replied. Secondly, so far as I can see, you don't really want the "same" group box anywhere. You have different buttons with different actions, and so on. I don't know why you are not just using two quite separate
QGroupBox
es, no inheritance or special class, which have their own buttons, and that's it. Especially if using the Designer it's easier than trying to write code. -
@buzz_lightzyear said in Inheritance of GroupBox:
but I want the same group box on all three tabs, but with little changes.
Firstly, everything as @jsulm has replied. Secondly, so far as I can see, you don't really want the "same" group box anywhere. You have different buttons with different actions, and so on. I don't know why you are not just using two quite separate
QGroupBox
es, no inheritance or special class, which have their own buttons, and that's it. Especially if using the Designer it's easier than trying to write code.@JonB said in Inheritance of GroupBox:
I don't know why you are not just using two quite separate QGroupBox
That's why I asked if they share functionality,
and the reponse was no,
therefore I don't see the need of the GroupBoxWithButtons class at all.I don't understand why the OP simply don't create the two tabs in the Designer.
Or at least with a simple method in MainWindow:createTab(int no)
{
// create tab 1
if( no==2)
// create or modify elements for tab2
}