Important: Please read the Qt Code of Conduct -

QMenu in a base class

  • Hello!

    I have this base class, implemented by other classes:

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

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

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

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

  • Qt Champions 2017

    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