How can I use QWidgetAction
-
Hi all :
I am a fresher of QT, and I want to implement the function like the below!(click the ToolButton/QAction, then a custom defined widget)
image urland my code is as below:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QToolBar *toolBar; QToolButton *toolButton01; QMenuBar *menuBar01; QMenu *menu01; QWidgetAction *widgetAction; QWidget *tempWidget; QAction *tempAction; }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->resize(500, 500); this->setWindowTitle("Hello World"); toolBar = new QToolBar(this); toolButton01 = new QToolButton(toolBar); toolButton01->setText("HJY"); toolButton01->setAutoRaise(true); toolButton01->setToolButtonStyle(Qt::ToolButtonTextOnly); toolButton01->setPopupMode(QToolButton::MenuButtonPopup); toolButton01->setToolTip(tr("Nice")); menu01 = new QMenu(toolButton01); #if 1 widgetAction = new QWidgetAction(menu01); tempWidget = new QWidget(); tempWidget->resize(100, 100); widgetAction->setDefaultWidget(tempWidget); menu01->addAction(widgetAction); #else tempAction = new QAction("HHH", toolButton01); menu01->addAction(tempAction); #endif toolButton01->setMenu(menu01); toolBar->addWidget(toolButton01); this->addToolBar(toolBar); } MainWindow::~MainWindow() { }

But when I add QWidgetAction, It can`t show my defined widget!
any kindly person can point my fault? thank you in advance!!!
-
Hi all :
I am a fresher of QT, and I want to implement the function like the below!(click the ToolButton/QAction, then a custom defined widget)
image urland my code is as below:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QToolBar *toolBar; QToolButton *toolButton01; QMenuBar *menuBar01; QMenu *menu01; QWidgetAction *widgetAction; QWidget *tempWidget; QAction *tempAction; }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->resize(500, 500); this->setWindowTitle("Hello World"); toolBar = new QToolBar(this); toolButton01 = new QToolButton(toolBar); toolButton01->setText("HJY"); toolButton01->setAutoRaise(true); toolButton01->setToolButtonStyle(Qt::ToolButtonTextOnly); toolButton01->setPopupMode(QToolButton::MenuButtonPopup); toolButton01->setToolTip(tr("Nice")); menu01 = new QMenu(toolButton01); #if 1 widgetAction = new QWidgetAction(menu01); tempWidget = new QWidget(); tempWidget->resize(100, 100); widgetAction->setDefaultWidget(tempWidget); menu01->addAction(widgetAction); #else tempAction = new QAction("HHH", toolButton01); menu01->addAction(tempAction); #endif toolButton01->setMenu(menu01); toolBar->addWidget(toolButton01); this->addToolBar(toolBar); } MainWindow::~MainWindow() { }

But when I add QWidgetAction, It can`t show my defined widget!
any kindly person can point my fault? thank you in advance!!!
@leekOnionAgge said in How can I use QWidgetAction:
toolButton01->setToolTip(tr("f*** !!"));Please think about young children reading your post as their introduction to programming!
Your code is correct. Your problem is that the added widget does not have anything on it taking space to show. If you replace it with
tempWidget = new QLabel("Hello")it will be visible. Once you start putting stuff on yourQWidgetit will show, e.g.tempWidget = new QWidget(); // tempWidget = new QLabel("Jon"); tempWidget->resize(100, 100); QGridLayout *grid = new QGridLayout; grid->addWidget(new QLabel("Hello")); tempWidget->setLayout(grid);you will see it. For whatever reason, the
tempWidget->resize(...);is having no effect. -
@leekOnionAgge said in How can I use QWidgetAction:
toolButton01->setToolTip(tr("f*** !!"));Please think about young children reading your post as their introduction to programming!
Your code is correct. Your problem is that the added widget does not have anything on it taking space to show. If you replace it with
tempWidget = new QLabel("Hello")it will be visible. Once you start putting stuff on yourQWidgetit will show, e.g.tempWidget = new QWidget(); // tempWidget = new QLabel("Jon"); tempWidget->resize(100, 100); QGridLayout *grid = new QGridLayout; grid->addWidget(new QLabel("Hello")); tempWidget->setLayout(grid);you will see it. For whatever reason, the
tempWidget->resize(...);is having no effect.@JonB sorry for may rudely behavior! and I will keep more polite in the future!!
thank you for you advice!!!