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. UI (Qt Designer) and inheritance

UI (Qt Designer) and inheritance

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 983 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.
  • G Offline
    G Offline
    G_ka
    wrote on 19 Jun 2019, 19:17 last edited by
    #1

    Hello,
    I'm trying to create a base window class, and then some derived windows with their own UI.
    Let's assume I have BaseWindow and DerivedWindow. Both were created with Qt Designer.
    BaseWindow has an action "test". DerivedWindow also has an action "test", and a checkbox.
    Sources files (probably useless for this question):

    BaseWindow.h

    namespace Ui {
    class BaseWindow;
    }
    
    class BaseWindow : public QMainWindow
    {
    
    public:
        explicit BaseWindow(QWidget *parent = nullptr) :
            QMainWindow(parent),
            ui(new Ui::BaseWindow)
        {
            ui->setupUi(this);
            connect(ui->actionTest, &QAction::triggered, this, [&]()
            {
                QTextStream(stdout) << "Test action triggered from base window";
            });
        }
        ~BaseWindow()
        {
            delete ui;
        }
    
    private:
        Ui::BaseWindow *ui;
    };
    

    DerivedWindow.h

    namespace Ui {
    class DerivedWindow;
    }
    
    class DerivedWindow : public BaseWindow
    {
    
    public:
        explicit DerivedWindow(QWidget *parent = nullptr) :
            BaseWindow (parent),
            ui(new Ui::DerivedWindow)
        {
            ui->setupUi(this);
    
            connect(ui->checkBox, &QCheckBox::clicked, this, [&](){
                QTextStream(stdout) << "Checkbox clicked in derived window";
            });
        }
    
        ~DerivedWindow()
        {
            delete ui;
        }
    
    private:
        Ui::DerivedWindow *ui;
    };
    

    If I click the "test" action in DerivedWindow, nothing will happen. This is expected, since I haven't connected it and both "test" actions are completely separate.

    My question is: is it possible to make an UI file inherit from another UI file? So that if I click the "test" action in DerivedWindow, it will work as if it had been clicked in BaseWindow, and print a message?

    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Jun 2019, 20:38 last edited by
      #2

      Hi,

      I may be wrong but I don't think you can do that in an automated way.

      What is you use case exactly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        G_ka
        wrote on 19 Jun 2019, 21:11 last edited by
        #3

        It will be better explained with screenshots.
        TES5 SSE FO4

        As you can see, those three windows are very similar, but they have some differences. So, making a common base would allow to remove duplicated code.

        J 1 Reply Last reply 20 Jun 2019, 00:29
        0
        • G G_ka
          19 Jun 2019, 21:11

          It will be better explained with screenshots.
          TES5 SSE FO4

          As you can see, those three windows are very similar, but they have some differences. So, making a common base would allow to remove duplicated code.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 20 Jun 2019, 00:29 last edited by
          #4

          @G_ka said in UI (Qt Designer) and inheritance:

          those three windows are very similar, but they have some differences. So, making a common base would allow to remove duplicated code.

          Instead of creating 3 different window classes, consider using just one class. Dynamically show() or hide() the group boxes depending on which test you want to run.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          G 1 Reply Last reply 20 Jun 2019, 09:37
          2
          • J JKSH
            20 Jun 2019, 00:29

            @G_ka said in UI (Qt Designer) and inheritance:

            those three windows are very similar, but they have some differences. So, making a common base would allow to remove duplicated code.

            Instead of creating 3 different window classes, consider using just one class. Dynamically show() or hide() the group boxes depending on which test you want to run.

            G Offline
            G Offline
            G_ka
            wrote on 20 Jun 2019, 09:37 last edited by
            #5

            @JKSH I was hoping for a better solution, but this looks like this solution is the easier one. Thanks.

            1 Reply Last reply
            0

            1/5

            19 Jun 2019, 19:17

            • Login

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