[Qt] Use the Tool Button control to create a drop-down menu button

Functional description

Use qt for interface design and development, and create drop-down buttons.

Detailed implementation

1. Use the design to open the .ui file on the qt side toolbar

2. Create a button

Create a Tool Button button, and select the MenuButtonPopup property in the QToolButton column in the property window.
insert image description here

3. Create action

Create the corresponding action in the Action editor as the option of the drop-down menu. Pay attention to the name of the first column, which is the corresponding object name in the code for calling.
insert image description here

4. Create a menu, add action and set the menu for the button.

Create it in the constructor of the main window.

 	QMenu *switchMenu = new QMenu(this);

Then add action to the newly created menu, where ui is the instantiated object of the .ui interface file, which can be instantiated in the main window class of the header file.

    switchMenu->addAction(ui->actionVentChamber);
    switchMenu->addAction(ui->actionPumpChamber);

Set the menu for the Tool Button control, where pump is the object name of the Tool Button in the ui.

	ui->pump->setMenu(switchMenu);

All steps are now complete.

Show results

At this time, the mouse cursor turns white when it is on the VentChamber.
insert image description here

Guess you like

Origin blog.csdn.net/freezing_00/article/details/132008037