Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with inheritance and QDialog



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



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



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



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



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



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



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


  • Lifetime Qt Champion

    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.



  • 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 :(


  • Lifetime Qt Champion

    Are-you using QDataWidgetMapper ?



  • Not at the moment.


  • Lifetime Qt Champion

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


Log in to reply