Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Inheritance of GroupBox
Forum Updated to NodeBB v4.3 + New Features

Inheritance of GroupBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    buzz_lightzyear
    wrote on last edited by
    #3

    Hi,

    thank you, but behind the buttons in tab 1 and tab 2 shoud work different functions. I really need two Group Boxes.

    JoeCFDJ 1 Reply Last reply
    0
    • B buzz_lightzyear

      Hi,

      thank you, but behind the buttons in tab 1 and tab 2 shoud work different functions. I really need two Group Boxes.

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #4

      @buzz_lightzyear In this case, you need inheritance to do that.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        buzz_lightzyear
        wrote on last edited by buzz_lightzyear
        #5

        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 :-)

        M 1 Reply Last reply
        0
        • B buzz_lightzyear

          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 :-)

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #6

          @buzz_lightzyear

          GroupBoxTab1::GroupBoxTab1(QWidget *parent) :
              GroupBoxWithButtons(parent),
              ui(new Ui::GroupBoxTab1)
          {
              ui->setupUi(this);
          
          }
          
          

          you set two different ui for the same object !!?

          1 Reply Last reply
          0
          • B buzz_lightzyear

            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?

            Screenshot_20221216_181106.png

            Thank you :-)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • B Offline
              B Offline
              buzz_lightzyear
              wrote on last edited by
              #8

              @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 :-D

              thx :-)

              M jsulmJ 2 Replies Last reply
              0
              • B buzz_lightzyear

                @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 :-D

                thx :-)

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #9

                @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 ?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  buzz_lightzyear
                  wrote on last edited by
                  #10

                  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

                  1 Reply Last reply
                  0
                  • B buzz_lightzyear

                    @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 :-D

                    thx :-)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @buzz_lightzyear said in Inheritance of GroupBox:

                    how can I access the elements?

                    Simply make gbwb and gbwb2 member variables in MainWindow...

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      buzz_lightzyear
                      wrote on last edited by
                      #12

                      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 :-)

                      jsulmJ 1 Reply Last reply
                      0
                      • B buzz_lightzyear

                        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 :-)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @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...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          buzz_lightzyear
                          wrote on last edited by
                          #14

                          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 :-)

                          jsulmJ JonBJ 2 Replies Last reply
                          0
                          • B buzz_lightzyear

                            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 :-)

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #15

                            @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);
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • B buzz_lightzyear

                              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 :-)

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #16

                              @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 QGroupBoxes, 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.

                              M 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @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 QGroupBoxes, 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.

                                M Offline
                                M Offline
                                mpergand
                                wrote on last edited by mpergand
                                #17

                                @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
                                }

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved