qt工具栏和菜单栏

以前用过qt,但是老是忘,得现查。现记录如下

1,一个menubar可以有多个menu

2,一个menu可以有多个action,菜单栏里的各项叫做action,而不是Menu,action可以当作实体,

3,每个action对应事件


比如

1,一个menubar可以有多个menu
 QMenu * theMenu1 = new QMenu(this);
 QMenu * theMenu2 = new QMenu(this);
 QMenu * theMenu3 = new QMenu(this);
 QMenu * theMenu4 = new QMenu(this);
 QMenu * theMenu5 = new QMenu(this);
 QMenu * theMenu6 = new QMenu(this);
 menuBar()->addMenu(theMenu1)->setText("tif and pcl");
 menuBar()->addMenu(theMenu2)->setText("tif and pcl");
 menuBar()->addMenu(theMenu3)->setText("tif and pcl");
 menuBar()->addMenu(theMenu4)->setText("tif and pcl");
 menuBar()->addMenu(theMenu5)->setText("tif and pcl");
 menuBar()->addMenu(theMenu6)->setText("tif and pcl");


2,一个menu可以有多个action
 QAction * theAction1 = new QAction("pcltotif", this);
 QAction * theAction2 = new QAction("pcltotif", this);
 QAction * theAction3 = new QAction("pcltotif", this);
 QAction * theAction4= new QAction("pcltotif", this);
 QAction * theAction5 = new QAction("pcltotif", this);
 QAction * theAction6 = new QAction("pcltotif", this);
 theMenu->addAction(theAction1);

 theMenu->addAction(theAction2);

 theMenu->addAction(theAction3);

 theMenu->addAction(theAction4); 

theMenu->addAction(theAction5); 

theMenu->addAction(theAction6);

3每个action可以对应一个触发事件


 connect(theAction, SIGNAL(triggered()) , this, SLOT(showDialogPCLtoTif()));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt工具栏中创建下拉菜单可以通过以下步骤实现: 1. 首先,在 Qt 设计器中打开你的窗口/对话框界面,将工具栏添加到界面上。 2. 在工具栏上右键单击,选择 "Add Action Group",创建一个动作组。 3. 在动作组中添加一个动作,该动作将作为下拉菜单的标题显示。 4. 在动作组中添加子动作,这些子动作将作为下拉菜单的选项。 5. 右键单击动作组中的子动作,选择 "Set Menu",然后选择对应的菜单。 6. 在代码中,连接菜单的信号和槽函数,以便在选择菜单项时执行相应的操作。 下面是一个简单的示例代码,演示如何在工具栏上创建一个下拉菜单: ```cpp // 创建工具栏 QToolBar* toolbar = new QToolBar(this); addToolBar(toolbar); // 创建动作组 QActionGroup* actionGroup = new QActionGroup(this); // 创建主动作并设置为不可点击 QAction* mainAction = new QAction("下拉菜单", this); mainAction->setCheckable(true); mainAction->setEnabled(false); toolbar->addAction(mainAction); // 创建下拉菜单 QMenu* menu = new QMenu(this); mainAction->setMenu(menu); // 创建子动作并添加到下拉菜单 QAction* action1 = new QAction("选项1", this); QAction* action2 = new QAction("选项2", this); menu->addAction(action1); menu->addAction(action2); // 将子动作添加到动作组 actionGroup->addAction(action1); actionGroup->addAction(action2); // 连接菜单的信号和槽函数 connect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(menuActionTriggered(QAction*))); // 槽函数的实现 void YourClass::menuActionTriggered(QAction* action) { if (action == action1) { // 处理选项1的操作 } else if (action == action2) { // 处理选项2的操作 } } ``` 通过以上步骤,你可以在 Qt工具栏上成功创建一个下拉菜单,并在选择菜单项时执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值