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. [SOLVED] Submenu from QToolBar button
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Submenu from QToolBar button

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 11.2k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bambo
    wrote on last edited by
    #1

    Hi,
    i have QToolBar like this:
    !http://www.stud.fit.vutbr.cz/~xadami06/bar.png(QToolBar)!
    and i need submenu from QToolBar button, for example:
    If I click on save button, the save button submenu will show
    !http://www.stud.fit.vutbr.cz/~xadami06/bar2.png(QToolBar submenu)!

    Please, how can I do this effect?
    Thanks for reply!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hpollak
      wrote on last edited by
      #2

      you can work with states.

      see: "qt-doc":http://qt-project.org/doc/qt-5.0/qtquick/qmlexampletoggleswitch.html

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        Get a reference to the "QToolButton":http://qt-project.org/doc/qt-4.8/qtoolbutton.html for the action, set the "popupMode":http://qt-project.org/doc/qt-4.8/qtoolbutton.html#popupMode-prop and add actions to it thus:

        @
        #include <QtGui>

        class MainWindow : public QMainWindow
        {
        public:
        MainWindow(QWidget *parent=0)
        : QMainWindow(parent)
        {
        toolBar=addToolBar("Main Tool Bar");
        toolBar->addAction(new QAction("&New", this));
        toolBar->addAction(new QAction("&Open", this));

            QAction *saveAction=new QAction("&Save", this);
            toolBar->addAction(saveAction);
            QToolButton *saveButton=
                    dynamic_cast<QToolButton*>(toolBar->widgetForAction(saveAction));
            saveButton->setPopupMode(QToolButton::InstantPopup);
            saveButton->addAction(new QAction("Save &As", this));
            saveButton->addAction(new QAction("Save All", this));
        }
        

        private:
        QToolBar *toolBar;
        };

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
        }
        @

        Hope this helps ;o)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bambo
          wrote on last edited by
          #4

          Thanks jazzycamel, this is very helpful.
          But i have a little problem with it. Because my example was wrong :)

          How do I reach effect, that the button in menu has not been seen in the submenu? Example of the save buttons is stupid, but it doesnt matter. The point is, that the button in menu is NOT button, its only label for buttons in the submenu.

          For better understanding look at this picture:
          !http://www.stud.fit.vutbr.cz/~xadami06/bar4.png(bar)!

          Thank you for your advice.

          1 Reply Last reply
          0
          • jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by jazzycamel
            #5

            If I understand you correctly, you want the QToolBar to act more like a menu where the only result of pressing the 'Save' button/label is to open a sub menu with the 'Save As' and 'Save All' actions in it? If that's the case, then the following slightly modified example might be the solution:

            #include <QtGui>
            
            class MainWindow : public QMainWindow
            {
            public:
                MainWindow(QWidget *parent=0)
                    : QMainWindow(parent)
                {
                    toolBar=addToolBar("Main Tool Bar");
                    toolBar->addAction(new QAction("&New", this));
                    toolBar->addAction(new QAction("&Open", this));
            
                    QToolButton *saveButton=new QToolButton(this);
                    saveButton->setText("Save");
                    saveButton->setPopupMode(QToolButton::InstantPopup);
                    QMenu *saveMenu=new QMenu(saveButton);
                    saveMenu->addAction(new QAction("Save &As", this));
                    saveMenu->addAction(new QAction("Save All", this));
                    saveButton->setMenu(saveMenu);
                    toolBar->addWidget(saveButton);
                }
            private:
                QToolBar *toolBar;
            };
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MainWindow w;
                w.show();
                return a.exec();
            }
            

            I hope I've understood you correctly and this is the answer you were looking for, if not post back ;o)

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            1 Reply Last reply
            1
            • B Offline
              B Offline
              Bambo
              wrote on last edited by
              #6

              Yes, you undestood me correctly. It works great!

              Thank you very much and have a nice day.

              1 Reply Last reply
              0

              • Login

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