Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWidgetAction not visible



  • Hi

    I want a QMenu with oridnary QActions and a QProgressBar in it, so I use na QWidgetAction. The problem is - progress bar isn't visible at all. There's an empty row in the menu, but nothing in it. I've tried to change QProgressBar for something simpier, for example a QPushButton, but the effect is the same - an empty row. Even QLabel doesn't work.

    Code:
    @QWidgetAction *widgetAction = new QWidgetAction(this);
    widgetAction->setDefaultWidget(new QPushButton("Test button"));
    menu->addAction(widgetAction); @

    What am I doing wrong?

    I forgot to mention that it's a popup menu that appears when user clicks on a tray icon.


  • Moderators

    setDefaultWidget() works only for a single container showing the action, so are you adding widgetAction also somewhere else (like toolbar or other menu that is shown earlier)?
    To show a widget for each container that shows the action subclass QWidgetAction, implement virtual method createWidget() and return a new instance of that widget.



  • I've already read about it and no - this is the only place where I put it.

    But talking about this - I also tried to subclass QWidgetAction and override createWidget() in the first place - the same story.


  • Moderators

    Hm, that's odd.

    This works for me:

    class MyWidgetAction : public QWidgetAction {
    public:
        MyWidgetAction(QObject* parent = 0) : QWidgetAction(parent) {}
        QWidget* createWidget(QWidget *parent) {
            QProgressBar* pb = new QProgressBar(parent);
            pb->setRange(0,0);
            return pb;
        }
    };
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QMenu* trayMenu = new QMenu(this);
        MyWidgetAction* widgetAction = new MyWidgetAction(this);
    
        ui->mainToolBar->addAction(widgetAction); //without this works too
        trayMenu->addAction(widgetAction);
    
        QSystemTrayIcon* trayIcon = new QSystemTrayIcon(this);
        trayIcon->setContextMenu(trayMenu);
        trayIcon->show();
    }
    

    Can you see any relevant differences with what you have?



  • Not a single difference.

    I ran your code and the progress bar doesn't show.

    I'm using Ubuntu 12.04 (gnome3, unity).


Log in to reply