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. Problem with inheritance and QDialog
Forum Update on Monday, May 27th 2025

Problem with inheritance and QDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 872 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
    Stefanoxjx
    wrote on 1 Oct 2020, 15:25 last edited by Stefanoxjx 10 Jan 2020, 15:28
    #1

    Hi, I've a problem I don't understand with inheritance.
    I wrote a class named GenericLists that creates a Window and Toolbar.
    Now, I wrote another class named JobList that inherit GenericLists, but I can't add a Layout because I can't find the specific method.
    This is a part of code:

    GenericLists.h

    #ifndef GENERICLISTS_H
    #define GENERICLISTS_H
    
    #include <QDialog>
    #include  ...
    
    class GenericLists : public QDialog
    {
        Q_OBJECT
    
    public:
        GenericLists(QWidget *parent = nullptr);
    
    private:
        //*** Toolbar
        QAction             *tbApply;
        QAction             *tbBack;
        QAction             *tbAdd;
        QAction             *tbDelete;
        QAction             *tbEdit;
        QAction             *tbFind;
        QAction             *tbExit;
    
        void closeEvent         (QCloseEvent *);
    
    public slots:
                void        sltCloseDialog      (void) {this->close();}
                void        sltDelete           (void) {}
                bool        sltBack             (void) {return true;}
        virtual void        sltAdd              (void) {};
        virtual void        sltEdit             (void) {};
        uint32_t    sltFind             (void) {return 0;}
        virtual void        sltSelected         (void) {};
        virtual void        sltDataConfirm      (void) {};
    };
    
    #endif // GENERICLISTS_H
    

    GenericLists.cpp

    #include <QGuiApplication>
    #include <QScreen>
    #include <QToolBar>
    #include <QVBoxLayout>
    #include "genericlists.h"
    
    GenericLists::GenericLists(QWidget *parent) : QDialog(parent)
    {
        //Toolbar
        QToolBar *tb = new QToolBar();
        tb->setMovable(false);
    
        tbApply = tb->addAction("Confirm");
        tbApply->setIcon(QIcon(":/Icons/Apply.ico"));
        tbApply->setVisible(false);
    
        tbBack = tb->addAction("Cancel");
        tbBack->setIcon(QIcon(":/Icons/Back.ico"));
        tbBack->setVisible(false);
    
        tbAdd = tb->addAction("Add New");
        tbAdd->setIcon(QIcon(":/Icons/Add.ico"));
    
        tbDelete = tb->addAction("Delete");
        tbDelete->setIcon(QIcon(":/Icons/Delete.ico"));
        tbDelete->setEnabled(false);
    
        tbEdit = tb->addAction("Edit");
        tbEdit->setIcon(QIcon(":/Icons/Edit.ico"));
        tbEdit->setEnabled(false);
    
        tbFind = tb->addAction("Find");
        tbFind->setIcon(QIcon(":/Icons/Find.ico"));
        tbFind->setShortcutVisibleInContextMenu(true);
        tbFind->setShortcut(Qt::CTRL+Qt::Key_L);
    
        tbExit = tb->addAction("Exit");
        tbExit->setIcon(QIcon(":/Icons/Exit.ico"));
    
        QMainWindow *mainWindow = new QMainWindow;
        mainWindow->addToolBar(tb);
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(mainWindow);
        setLayout(layout);
    
        //Signals
        connect(tbApply, SIGNAL(triggered()), this, SLOT(sltUserConfirm()));
        connect(tbBack, SIGNAL(triggered()), this, SLOT(sltBack()));
        connect(tbAdd, SIGNAL(triggered()), this, SLOT(sltAdd()));
        connect(tbDelete, SIGNAL(triggered()), this, SLOT(sltDelete()));
        connect(tbEdit, SIGNAL(triggered()), this, SLOT(sltEdit()));
        connect(tbFind, SIGNAL(triggered()), this, SLOT(sltFind()));
        connect(tbExit, SIGNAL(triggered()), this, SLOT(sltCloseDialog()));
    }
    
    void GenericLists::closeEvent(QCloseEvent *) {}
    void GenericLists::ResetEditWindow() {}
    

    JobList.h

    #ifndef JOBSLIST_H
    #define JOBSLIST_H
    
    #include ...
    #include "genericlists.h"
    
    class JobsList : public GenericLists
    {
        Q_OBJECT
    
    public:
        JobsList(QWidget *parent = nullptr);
    
    private:
        QLineEdit               *leInput;
    
    ...
    };
    
    #endif // JOBSLIST_H
    

    JobList.cpp

    #include ...
    #include "jobslist.h"
    
    JobsList::JobsList(QWidget *parent) : GenericLists(parent)
    {
        this->setWindowTitle("Job list");
        this->setWindowIcon(QIcon(":/Icons/Jobs.ico"));
    
        QHBoxLayout *hl1 = new QHBoxLayout;
    
    //   *** Here comes the problem, I can't found method layout.addLayout()
    //   *** Where I wrong?
    
        this->exec();
    }
    

    Where I wrong?
    Thanks.

    J 1 Reply Last reply 1 Oct 2020, 15:48
    0
    • S Stefanoxjx
      1 Oct 2020, 15:25

      Hi, I've a problem I don't understand with inheritance.
      I wrote a class named GenericLists that creates a Window and Toolbar.
      Now, I wrote another class named JobList that inherit GenericLists, but I can't add a Layout because I can't find the specific method.
      This is a part of code:

      GenericLists.h

      #ifndef GENERICLISTS_H
      #define GENERICLISTS_H
      
      #include <QDialog>
      #include  ...
      
      class GenericLists : public QDialog
      {
          Q_OBJECT
      
      public:
          GenericLists(QWidget *parent = nullptr);
      
      private:
          //*** Toolbar
          QAction             *tbApply;
          QAction             *tbBack;
          QAction             *tbAdd;
          QAction             *tbDelete;
          QAction             *tbEdit;
          QAction             *tbFind;
          QAction             *tbExit;
      
          void closeEvent         (QCloseEvent *);
      
      public slots:
                  void        sltCloseDialog      (void) {this->close();}
                  void        sltDelete           (void) {}
                  bool        sltBack             (void) {return true;}
          virtual void        sltAdd              (void) {};
          virtual void        sltEdit             (void) {};
          uint32_t    sltFind             (void) {return 0;}
          virtual void        sltSelected         (void) {};
          virtual void        sltDataConfirm      (void) {};
      };
      
      #endif // GENERICLISTS_H
      

      GenericLists.cpp

      #include <QGuiApplication>
      #include <QScreen>
      #include <QToolBar>
      #include <QVBoxLayout>
      #include "genericlists.h"
      
      GenericLists::GenericLists(QWidget *parent) : QDialog(parent)
      {
          //Toolbar
          QToolBar *tb = new QToolBar();
          tb->setMovable(false);
      
          tbApply = tb->addAction("Confirm");
          tbApply->setIcon(QIcon(":/Icons/Apply.ico"));
          tbApply->setVisible(false);
      
          tbBack = tb->addAction("Cancel");
          tbBack->setIcon(QIcon(":/Icons/Back.ico"));
          tbBack->setVisible(false);
      
          tbAdd = tb->addAction("Add New");
          tbAdd->setIcon(QIcon(":/Icons/Add.ico"));
      
          tbDelete = tb->addAction("Delete");
          tbDelete->setIcon(QIcon(":/Icons/Delete.ico"));
          tbDelete->setEnabled(false);
      
          tbEdit = tb->addAction("Edit");
          tbEdit->setIcon(QIcon(":/Icons/Edit.ico"));
          tbEdit->setEnabled(false);
      
          tbFind = tb->addAction("Find");
          tbFind->setIcon(QIcon(":/Icons/Find.ico"));
          tbFind->setShortcutVisibleInContextMenu(true);
          tbFind->setShortcut(Qt::CTRL+Qt::Key_L);
      
          tbExit = tb->addAction("Exit");
          tbExit->setIcon(QIcon(":/Icons/Exit.ico"));
      
          QMainWindow *mainWindow = new QMainWindow;
          mainWindow->addToolBar(tb);
      
          QVBoxLayout *layout = new QVBoxLayout;
          layout->addWidget(mainWindow);
          setLayout(layout);
      
          //Signals
          connect(tbApply, SIGNAL(triggered()), this, SLOT(sltUserConfirm()));
          connect(tbBack, SIGNAL(triggered()), this, SLOT(sltBack()));
          connect(tbAdd, SIGNAL(triggered()), this, SLOT(sltAdd()));
          connect(tbDelete, SIGNAL(triggered()), this, SLOT(sltDelete()));
          connect(tbEdit, SIGNAL(triggered()), this, SLOT(sltEdit()));
          connect(tbFind, SIGNAL(triggered()), this, SLOT(sltFind()));
          connect(tbExit, SIGNAL(triggered()), this, SLOT(sltCloseDialog()));
      }
      
      void GenericLists::closeEvent(QCloseEvent *) {}
      void GenericLists::ResetEditWindow() {}
      

      JobList.h

      #ifndef JOBSLIST_H
      #define JOBSLIST_H
      
      #include ...
      #include "genericlists.h"
      
      class JobsList : public GenericLists
      {
          Q_OBJECT
      
      public:
          JobsList(QWidget *parent = nullptr);
      
      private:
          QLineEdit               *leInput;
      
      ...
      };
      
      #endif // JOBSLIST_H
      

      JobList.cpp

      #include ...
      #include "jobslist.h"
      
      JobsList::JobsList(QWidget *parent) : GenericLists(parent)
      {
          this->setWindowTitle("Job list");
          this->setWindowIcon(QIcon(":/Icons/Jobs.ico"));
      
          QHBoxLayout *hl1 = new QHBoxLayout;
      
      //   *** Here comes the problem, I can't found method layout.addLayout()
      //   *** Where I wrong?
      
          this->exec();
      }
      

      Where I wrong?
      Thanks.

      J Offline
      J Offline
      JonB
      wrote on 1 Oct 2020, 15:48 last edited by JonB 10 Jan 2020, 15:51
      #2

      @Stefanoxjx said in Problem with inheritance and QDialog:

      // *** Here comes the problem, I can't found method layout.addLayout()

      I don't claim to know whether you're doing the right thing or not, but there is no layout variable if that's what you are trying. Everything in Qt is methods, not variables, so are you looking for QLayout *QWidget::layout() const?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Stefanoxjx
        wrote on 1 Oct 2020, 15:55 last edited by
        #3

        Hi JonB,
        please, have patient, I don't know C++ very well and I try to experimented to understand.
        If you look in GenericLists.cpp you can find this:

            QVBoxLayout *layout = new QVBoxLayout;
            layout->addWidget(mainWindow);
            setLayout(layout);
        

        I think that layout must inherited from JobList

        I think wrong?

        J 1 Reply Last reply 1 Oct 2020, 16:10
        0
        • S Stefanoxjx
          1 Oct 2020, 15:55

          Hi JonB,
          please, have patient, I don't know C++ very well and I try to experimented to understand.
          If you look in GenericLists.cpp you can find this:

              QVBoxLayout *layout = new QVBoxLayout;
              layout->addWidget(mainWindow);
              setLayout(layout);
          

          I think that layout must inherited from JobList

          I think wrong?

          J Offline
          J Offline
          JonB
          wrote on 1 Oct 2020, 16:10 last edited by JonB 10 Jan 2020, 17:12
          #4

          @Stefanoxjx
          I am patient, but I don't know what you're trying to do! Maybe you mean layout()->addLayout(...)? Maybe you mean setLayout(hl1), is that what you're trying to do?

          Completely separate from your question. First, you do not want to be going this->exec(); in the JobsList::JobsList() constructor at all. Only the caller who creates this dialog should call exec(). Second, you are creating a QMainWindow inside a dialog; technically thins works, but it's an odd thing to want to do, main windows should be windows, not inside dialogs.

          S 1 Reply Last reply 1 Oct 2020, 17:11
          2
          • J JonB
            1 Oct 2020, 16:10

            @Stefanoxjx
            I am patient, but I don't know what you're trying to do! Maybe you mean layout()->addLayout(...)? Maybe you mean setLayout(hl1), is that what you're trying to do?

            Completely separate from your question. First, you do not want to be going this->exec(); in the JobsList::JobsList() constructor at all. Only the caller who creates this dialog should call exec(). Second, you are creating a QMainWindow inside a dialog; technically thins works, but it's an odd thing to want to do, main windows should be windows, not inside dialogs.

            S Offline
            S Offline
            Stefanoxjx
            wrote on 1 Oct 2020, 17:11 last edited by
            #5

            You're right, excuse me for omission...

            @JonB said in Problem with inheritance and QDialog:

            @Stefanoxjx
            I am patient, but I don't know what you're trying to do! Maybe you mean layout()->addLayout(...)? Maybe you mean setLayout(hl1), is that what you're trying to do?

            I would like to add layout with: layout().addlayout(hl1), but under layout() I found only methods: activate, addItem, addWidget, alignment.

            Completely separate from your question. First, you do not want to be going this->exec(); in the JobsList::JobsList(0 constructor at all. Only the caller who creates this dialog should call exec().

            Ops!!!
            I've many dialogs with same toolbar, changes only the body of dialog.
            What's the better way to have dialog creation and toolbar managed from single class?

            Second, you are creating a QMainWindow inside a dialog; technically thins works, but it;s an odd thing to want to do, main windows should be windows, not inside dialogs.

            About this, I've seen this method in StackOverflow: https://stackoverflow.com/questions/18435801/can-you-add-a-toolbar-to-qdialog

            Maybe I've to create QMainWindow instead QDialog?

            J 1 Reply Last reply 1 Oct 2020, 17:32
            0
            • S Stefanoxjx
              1 Oct 2020, 17:11

              You're right, excuse me for omission...

              @JonB said in Problem with inheritance and QDialog:

              @Stefanoxjx
              I am patient, but I don't know what you're trying to do! Maybe you mean layout()->addLayout(...)? Maybe you mean setLayout(hl1), is that what you're trying to do?

              I would like to add layout with: layout().addlayout(hl1), but under layout() I found only methods: activate, addItem, addWidget, alignment.

              Completely separate from your question. First, you do not want to be going this->exec(); in the JobsList::JobsList(0 constructor at all. Only the caller who creates this dialog should call exec().

              Ops!!!
              I've many dialogs with same toolbar, changes only the body of dialog.
              What's the better way to have dialog creation and toolbar managed from single class?

              Second, you are creating a QMainWindow inside a dialog; technically thins works, but it;s an odd thing to want to do, main windows should be windows, not inside dialogs.

              About this, I've seen this method in StackOverflow: https://stackoverflow.com/questions/18435801/can-you-add-a-toolbar-to-qdialog

              Maybe I've to create QMainWindow instead QDialog?

              J Offline
              J Offline
              JonB
              wrote on 1 Oct 2020, 17:32 last edited by JonB 10 Jan 2020, 18:00
              #6

              @Stefanoxjx said in Problem with inheritance and QDialog:

              I would like to add layout with: layout().addlayout(hl1), but under layout() I found only methods: activate, addItem, addWidget, alignment.

              It would be layout()->addlayout(hl1). OK, I get it, layout() returns QLayout* and it doesn't know of yours is a QBoxLayout* for addLayout(). This could be addressed, but I'm not convinced you even want to be doing an addLayout() here....

              What's the better way to have dialog creation and toolbar managed from single class?

              The dialog's constructor is for creating the dialog. Not for showing/executing it. the caller --- the one who does dialog = new QDialog() --- is the place to go dialog->exec().

              I accept one can put a toolbar, or a main window, in dialogs. The question is why would you want to? Why do you have dialogs which you want to look like main windows with toolbars etc.? The real question is: why dialogs?

              If you can get rid of the need for dialogs, a common scenario is to have a QMainWindow --- so it has a common toolbar, menu etc. --- with the main widget being a QStackedWidget which holds "pages" (widgets) in the central area. Users clicks/takes actions and which one page is being shown in the main window changes appropriately. Is something like that what you want?

              S 1 Reply Last reply 1 Oct 2020, 19:53
              1
              • J JonB
                1 Oct 2020, 17:32

                @Stefanoxjx said in Problem with inheritance and QDialog:

                I would like to add layout with: layout().addlayout(hl1), but under layout() I found only methods: activate, addItem, addWidget, alignment.

                It would be layout()->addlayout(hl1). OK, I get it, layout() returns QLayout* and it doesn't know of yours is a QBoxLayout* for addLayout(). This could be addressed, but I'm not convinced you even want to be doing an addLayout() here....

                What's the better way to have dialog creation and toolbar managed from single class?

                The dialog's constructor is for creating the dialog. Not for showing/executing it. the caller --- the one who does dialog = new QDialog() --- is the place to go dialog->exec().

                I accept one can put a toolbar, or a main window, in dialogs. The question is why would you want to? Why do you have dialogs which you want to look like main windows with toolbars etc.? The real question is: why dialogs?

                If you can get rid of the need for dialogs, a common scenario is to have a QMainWindow --- so it has a common toolbar, menu etc. --- with the main widget being a QStackedWidget which holds "pages" (widgets) in the central area. Users clicks/takes actions and which one page is being shown in the main window changes appropriately. Is something like that what you want?

                S Offline
                S Offline
                Stefanoxjx
                wrote on 1 Oct 2020, 19:53 last edited by
                #7

                @JonB said in Problem with inheritance and QDialog:

                If you can get rid of the need for dialogs, a common scenario is to have a QMainWindow --- so it has a common toolbar, menu etc. --- with the main widget being a QStackedWidget which holds "pages" (widgets) in the central area. Users clicks/takes actions and which one page is being shown in the main window changes appropriately. Is something like that what you want?

                Yes I can get rid of QDialog and have only one MainWindow with internal Widgets, but I can have user interface with many widgets and another with a single QLineedit.
                The latter would look very bad in a large QMainWindow (a drop of water in the middle of the desert :D)
                With QDialog I thought to minimize this case because I can have a QDialog with properly size about number of widget.
                I hope I explained good :)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 1 Oct 2020, 20:48 last edited by
                  #8

                  Hi,

                  Can you explain a bit more what your application does with that variety of widgets ?

                  There might lie the idea for a good UI design.

                  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
                  • S Offline
                    S Offline
                    Stefanoxjx
                    wrote on 3 Oct 2020, 07:34 last edited by
                    #9

                    Hi,
                    simply, the application can have forms with a single QLabel+QLineEdit to edit a table with one column and forms with many Widgets to manage Tables with many Columns.
                    I don't like use QTableWidget to do it, I prefer a classic form.
                    If I create a QMainWindow with appropriate dimension to insert many widgets and after I use same QMainWindow to place a single QLabel+QLineEdit you understand that UI it will suck :(

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Oct 2020, 21:05 last edited by
                      #10

                      Are-you using QDataWidgetMapper ?

                      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
                      • S Offline
                        S Offline
                        Stefanoxjx
                        wrote on 3 Oct 2020, 21:36 last edited by
                        #11

                        Not at the moment.

                        M 1 Reply Last reply 4 Oct 2020, 13:10
                        0
                        • S Stefanoxjx
                          3 Oct 2020, 21:36

                          Not at the moment.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 4 Oct 2020, 13:10 last edited by
                          #12

                          @Stefanoxjx
                          Hi
                          The layout in the base class is just a local variable
                          QVBoxLayout *layout = new QVBoxLayout;

                          So to access this from a base class you need to store it as a member variable in
                          GenericLists.h (in the class def) so it can be inherited so to speak.

                          so in .h
                          QVBoxLayout *layout;

                          and then instead in .cpp
                          layout = new QVBoxLayout;

                          Then your JobsList can also use it.

                          1 Reply Last reply
                          0

                          7/12

                          1 Oct 2020, 19:53

                          • Login

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