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.
-
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. -
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?
-
Hello, I have same problem.
QWidgetAction * wa = new QWidgetAction(ui->menubar); QPushButton *btn = new QPushButton("abc"); wa->setDefaultWidget(btn); ui->menubar->addAction(wa);Not visible on ui. What is the problem here ?
-
Hello, I have same problem.
QWidgetAction * wa = new QWidgetAction(ui->menubar); QPushButton *btn = new QPushButton("abc"); wa->setDefaultWidget(btn); ui->menubar->addAction(wa);Not visible on ui. What is the problem here ?
Did you test the code of @Chris-Kawa ?
Which version of Qt are you using ?
On which OS ?
If Linux which desktop environment ?
Which window manager ? -
I did not see any thing, I want to use QMenuBar, not QToolBar
class MyWidgetAction : public QWidgetAction { public: MyWidgetAction(QObject* parent = 0) : QWidgetAction(parent) {} QWidget* createWidget(QWidget *parent) { QPushButton* pb = new QPushButton("abc"); return pb; } }; QMenu* trayMenu = new QMenu(this); MyWidgetAction* widgetAction = new MyWidgetAction(this); ui->menubar->addAction(widgetAction); trayMenu->addAction(widgetAction); QSystemTrayIcon* trayIcon = new QSystemTrayIcon(this); trayIcon->setContextMenu(trayMenu); trayIcon->show();Qt: 6.10.2
Os: Ubuntu 24
desktop environment : GNOME -
Can you check under KDE ?
-
@SGaist Why ? I use gnome. What is the different ?
-
@SGaist Why ? I use gnome. What is the different ?
@Joe-von-Habsburg they don't have the same design. Gnome is closer to macOS and handles menu bars differently from KDE. This could be one of the reason you don't get the result you are looking for.
-
thanks for your reply and explain