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. QMenu in a base class

QMenu in a base class

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.2k 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.
  • F Offline
    F Offline
    fermatqt
    wrote on last edited by A Former User
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      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
      3
      • F Offline
        F Offline
        fermatqt
        wrote on last edited by
        #3

        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.

        jsulmJ 1 Reply Last reply
        0
        • F fermatqt

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by dheerendra
            #5
            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.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            4

            • Login

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