[solved] QWidgetAction not visible in menu of small QToolBar



  • 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();
    }
    

  • Qt Champions 2016

    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.



  • @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 :-(


  • Qt Champions 2016

    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.



  • @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.


Log in to reply
 

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