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] QWidgetAction not visible in menu of small QToolBar
Forum Updated to NodeBB v4.3 + New Features

[solved] QWidgetAction not visible in menu of small QToolBar

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.4k 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.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by Wurgl
    #1

    I have some application having a toolbar and this toolbar shall hold not only buttons, but a QSpinBox too. So I used QWidgetAction with its method setDefaultWidget() which seemed to work pretty well.

    However, when the toolbar needs more space than the containing widget provides, the simple QAction members can be accessed in some menu at the right side where a "»" appears. But members created with QWidgetAction do not appear in this menu.

    This is the default layout of the toolbar of the example below:
    http://abload.de/img/finep2c70.png

    When I make is really small, it looks like here:
    http://abload.de/img/badv3dv5.png

    And the menu with the missing QWidgetAction
    http://abload.de/img/menuxff64.png

    So what to change to get this QWidgetAction accessible in the menu too?

    Here is a small example to demonstrate:

    /#include <QApplication>
    #include <QDialog>
    #include <QBoxLayout>
    #include <QToolBar>
    #include <QStyle>
    #include <QWidgetAction>
    #include <QSpinBox>
     
    int main(int argc, char *argv[])
    {
     QApplication a(argc, argv);
    
     QDialog d;
     QVBoxLayout *l = new QVBoxLayout(&d);
     QToolBar *tb = new QToolBar(&d);
     l->addWidget(tb);
    
     tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowLeft), "Left");
     tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowRight), "Right");
     QWidgetAction *wa = new QWidgetAction(tb);
     QSpinBox *sb = new QSpinBox(tb);
     sb->setToolTip("Spin");
     wa->setDefaultWidget(sb);
     tb->addAction(wa);
     tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowUp), "Up");
     tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowDown), "Down");
     
     d.show();
     
     return a.exec();
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      is this on OSX ?
      the doc says
      Mac OS X: If you add a widget to a menu in the application's menu bar on Mac OS X, the widget will be added and it will function but with some limitations:

      The widget is reparented away from the QMenu to the native menu view. If you show the menu in some other place (e.g. as a popup menu), the widget will not be there.

      Kinda sounds like that situation.

      W 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        is this on OSX ?
        the doc says
        Mac OS X: If you add a widget to a menu in the application's menu bar on Mac OS X, the widget will be added and it will function but with some limitations:

        The widget is reparented away from the QMenu to the native menu view. If you show the menu in some other place (e.g. as a popup menu), the widget will not be there.

        Kinda sounds like that situation.

        W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        @mrjj It is Linux with KDE as Desktop. I grep'ed through the examples, but did not find any code where a QWidgetAction is used with a QToolBar (actually there is only one example where QWidgetAction is used). Qt4 shows the same behaviour :-(

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, It seems to be the same issue as when it get reparent to the "overflow" menu, the
          widgets are not shown anymore.
          Using QWidgetAction is not so common, I didn't see any samples
          with it and also saw post on forum that using sub menus also had issues.
          I guess it could qualify as a bug.

          W 1 Reply Last reply
          0
          • mrjjM mrjj

            Well, It seems to be the same issue as when it get reparent to the "overflow" menu, the
            widgets are not shown anymore.
            Using QWidgetAction is not so common, I didn't see any samples
            with it and also saw post on forum that using sub menus also had issues.
            I guess it could qualify as a bug.

            W Offline
            W Offline
            Wurgl
            wrote on last edited by
            #5

            @mrjj

            I was reading the documentation again, in the toolbar I read:
            When a QToolBar is not a child of a QMainWindow, it loses the ability to populate the extension pop up with widgets added to the toolbar using addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead.

            So I changed the example a little bit and it works …

            #include <QApplication>
            #include <QDialog>
            #include <QBoxLayout>
            #include <QToolBar>
            #include <QStyle>
            #include <QWidgetAction>
            #include <QSpinBox>
            
            class MyWidgetAction : public QWidgetAction
            {
            public:
             MyWidgetAction(QObject *parent) : QWidgetAction(parent) {}
             virtual ~MyWidgetAction() {}
            protected:
             virtual QWidget *createWidget(QWidget *parent) {
              QSpinBox *sb = new QSpinBox(parent);
              sb->setToolTip("Spin");
              return sb;
             }
            };
             
            int main(int argc, char *argv[])
            {
             QApplication a(argc, argv);
            
             QDialog d;
             QVBoxLayout *l = new QVBoxLayout(&d);
             QToolBar *tb = new QToolBar(&d);
             l->addWidget(tb);
            
             tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowLeft), "Left");
             tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowRight), "Right");
             QWidgetAction *wa = new MyWidgetAction(tb);
             tb->addAction(wa);
             tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowUp), "Up");
             tb->addAction(a.style()->standardIcon(QStyle::SP_ArrowDown), "Down");
             
             d.show();
             
             return a.exec();
            }
            

            So the only thing I need to to, is to get both instances of the spinbox in the toolbar in sync.

            1 Reply Last reply
            1

            • Login

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