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. Inheriting without Inheriting the Ui, how?
QtWS25 Last Chance

Inheriting without Inheriting the Ui, how?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 164 Views
  • 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.
  • S Offline
    S Offline
    SunWithIssues
    wrote on last edited by
    #1

    So I have two dialogs (eg DialogA, and DialogB) and they each should have there own Ui form but the underlying logic of DialogB replicates many of the methods of DialogA, how could I inherit from DialogA without also inheriting its Ui form?

    // -------------------------------
    // dialoga.h
    // -------------------------------
    class DialogA : public QDialog 
    {
        Q_OBJECT
    
         public:
             void init(QStringList info);
             .....
    
         private:
             Ui::DialogA *ui;
             QStringList* info;
             
             void someOtherFunc();
             void willOverload();
    }
    
    // -------------------------------
    // dialogb.h
    // -------------------------------
    class DialogB : public DialogA
    {
        Q_OBJECT
    
    
        private:
             Ui::DialogB *ui;
             
             void willOverload();
    }
    
    // -------------------------------
    // dialoga.cpp
    // -------------------------------
    DialogA::DialogA(QWidget *parent)
        : QDialog(parent)
        , ui(new Ui::DialogA)
    {
        ui->setupUi(this);
    
       info = new QStringList();
       (ui->oldPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
    
        willOverload();
    }
    
    void DialogA::willOverload()
    {
       // additional ui setup happens here
    }
    
    // -------------------------------
    // dialogb.cpp
    // -------------------------------
    DialogB::DialogB(QWidget *parent)
        : DialogA(parent)
        , ui(new Ui::DialogB)
    {
        ui->setupUi(this);
    
       info = new QStringList();
       (ui->newPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
    
        willOverload();
    }
    
    void DialogB::willOverload()
    {
       // additional ui setup happens here
    }
    
    

    Is this not possible? As can be seen, DialogB should not repeat any of ui objects of DialogA even if the logic might replicate. (eg oldPushButton and newPushButton have different names/labels and interact with other objects in their corresponding ui slightly differently.)

    jsulmJ 1 Reply Last reply
    0
    • S SunWithIssues

      So I have two dialogs (eg DialogA, and DialogB) and they each should have there own Ui form but the underlying logic of DialogB replicates many of the methods of DialogA, how could I inherit from DialogA without also inheriting its Ui form?

      // -------------------------------
      // dialoga.h
      // -------------------------------
      class DialogA : public QDialog 
      {
          Q_OBJECT
      
           public:
               void init(QStringList info);
               .....
      
           private:
               Ui::DialogA *ui;
               QStringList* info;
               
               void someOtherFunc();
               void willOverload();
      }
      
      // -------------------------------
      // dialogb.h
      // -------------------------------
      class DialogB : public DialogA
      {
          Q_OBJECT
      
      
          private:
               Ui::DialogB *ui;
               
               void willOverload();
      }
      
      // -------------------------------
      // dialoga.cpp
      // -------------------------------
      DialogA::DialogA(QWidget *parent)
          : QDialog(parent)
          , ui(new Ui::DialogA)
      {
          ui->setupUi(this);
      
         info = new QStringList();
         (ui->oldPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
      
          willOverload();
      }
      
      void DialogA::willOverload()
      {
         // additional ui setup happens here
      }
      
      // -------------------------------
      // dialogb.cpp
      // -------------------------------
      DialogB::DialogB(QWidget *parent)
          : DialogA(parent)
          , ui(new Ui::DialogB)
      {
          ui->setupUi(this);
      
         info = new QStringList();
         (ui->newPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
      
          willOverload();
      }
      
      void DialogB::willOverload()
      {
         // additional ui setup happens here
      }
      
      

      Is this not possible? As can be seen, DialogB should not repeat any of ui objects of DialogA even if the logic might replicate. (eg oldPushButton and newPushButton have different names/labels and interact with other objects in their corresponding ui slightly differently.)

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

      @SunWithIssues said in Inheriting without Inheriting the Ui, how?:

      the underlying logic of DialogB replicates many of the methods of DialogA

      You could move the common logic in a common non-ui base class and use multiple inheritance.

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

      1 Reply Last reply
      4
      • S Offline
        S Offline
        SunWithIssues
        wrote on last edited by
        #2

        Basically, I am not thinking of just making a static c++ class which both dialogA and dialogB can inherit from, since i am not seeing anything in the docs which allow to not inherit the ui

        1 Reply Last reply
        0
        • S SunWithIssues

          So I have two dialogs (eg DialogA, and DialogB) and they each should have there own Ui form but the underlying logic of DialogB replicates many of the methods of DialogA, how could I inherit from DialogA without also inheriting its Ui form?

          // -------------------------------
          // dialoga.h
          // -------------------------------
          class DialogA : public QDialog 
          {
              Q_OBJECT
          
               public:
                   void init(QStringList info);
                   .....
          
               private:
                   Ui::DialogA *ui;
                   QStringList* info;
                   
                   void someOtherFunc();
                   void willOverload();
          }
          
          // -------------------------------
          // dialogb.h
          // -------------------------------
          class DialogB : public DialogA
          {
              Q_OBJECT
          
          
              private:
                   Ui::DialogB *ui;
                   
                   void willOverload();
          }
          
          // -------------------------------
          // dialoga.cpp
          // -------------------------------
          DialogA::DialogA(QWidget *parent)
              : QDialog(parent)
              , ui(new Ui::DialogA)
          {
              ui->setupUi(this);
          
             info = new QStringList();
             (ui->oldPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
          
              willOverload();
          }
          
          void DialogA::willOverload()
          {
             // additional ui setup happens here
          }
          
          // -------------------------------
          // dialogb.cpp
          // -------------------------------
          DialogB::DialogB(QWidget *parent)
              : DialogA(parent)
              , ui(new Ui::DialogB)
          {
              ui->setupUi(this);
          
             info = new QStringList();
             (ui->newPushButton, &QPushButton::clicked, this, &DialogA::someOtherFunc);
          
              willOverload();
          }
          
          void DialogB::willOverload()
          {
             // additional ui setup happens here
          }
          
          

          Is this not possible? As can be seen, DialogB should not repeat any of ui objects of DialogA even if the logic might replicate. (eg oldPushButton and newPushButton have different names/labels and interact with other objects in their corresponding ui slightly differently.)

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

          @SunWithIssues said in Inheriting without Inheriting the Ui, how?:

          the underlying logic of DialogB replicates many of the methods of DialogA

          You could move the common logic in a common non-ui base class and use multiple inheritance.

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

          1 Reply Last reply
          4
          • S SunWithIssues has marked this topic as solved on
          • S Offline
            S Offline
            SunWithIssues
            wrote on last edited by
            #4

            Yeah, I think that's what I got to do.

            1 Reply Last reply
            0

            • Login

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