QMenu in a base class



  • Hello!

    I have this base class, implemented by other classes:

    #ifndef ABSTRACTQWIDGET_H
    #define ABSTRACTQWIDGET_H
    
    #include <QWidget>
    #include <QMenu>
    #include <QTableWidget>
    #include <QLineEdit>
    #include <QMenuBar>
    #include "savetable.h"
    
    class AbstractQWidget : public QWidget {
        Q_OBJECT
    
    public:
        AbstractQWidget(QWidget *parent  = nullptr) : QWidget(parent) {}
        virtual ~AbstractQWidget() = 0;
    
    protected slots:
        virtual void setToUpper(const QString &text) = 0;
        virtual void saveTable() = 0;
    
    protected:
        void toUpper(const QString &text) {
            QLineEdit *qle = qobject_cast<QLineEdit *>(sender());
            if (!qle) return;
            qle->setText(text.toUpper());
        }
    
        void exportTable(QString strFile, QTableWidget *tbl) {
            SaveTable st(strFile, tbl);
        }
    };
    
    #endif // ABSTRACTQWIDGET_H
    

    I would like to create a common menu for all classes that extend this.
    how can I do?


  • Lifetime Qt Champion

    Hi,

    You can either create a protected member variable that you will access from the subclass or make it private and create protected functions to manipulate it from the subclasses.



  • hi!

    thanks for the reply.
    looking on google, I read that it would be better to use QMainWindow instead of QWidget.
    I think I'll use that, it seems to me easier.


  • Moderators

    @fermatqt You mean QMainWindow as base class for AbstractQWidget ?
    I think this is not a good idea: you will end up having many main windows!



    1. Create your own QToolBar object inside your base class. Add the required action to it.
    2. Create the verticalLayout and apply the same to your base class widget. Add the toolbar widget to to verticalLaoyout.
    3. Give public method inside your base class for the user add any widget if he wants.
    4. Inside the method you should add the widget to vertical layout.

    Hope this helps.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.