Qt之菜单栏工具栏入门

该文详细介绍了如何使用Qt进行图形用户界面(GUI)设计,包括创建菜单栏、菜单项、行为以及工具栏,并通过连接槽函数实现功能响应。具体步骤涉及QMenuBar、QMenu、QAction类的使用,以及多级菜单的构建和工具栏的自定义。同时,展示了如何设置行为的可选中状态并改变相应图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建菜单栏

QMenuBar *menuBar = new QMenuBar(this); //1.创建菜单栏
menuBar->setGeometry(,,width(),);   //设置大小
 
QMenu *fileMenu = new QMenu("File",this);   //2.创建菜单
//3.创建行为(Action)
QAction *fileCreateAction = new QAction("create",this);
QAction *fileSaveAction = new QAction("save",this);
QAction *fileImportAction = new QAction("import",this);
QAction *fileExportAction = new QAction("export",this);
//4.将行为添加到菜单
fileMenu->addAction(fileSaveAction);
fileMenu->addAction(fileImportAction);
fileMenu->addAction(fileExportAction);
fileMenu->addAction(fileCreateAction);
//5.将菜单添加到菜单栏
menuBar->addMenu(fileMenu);

2.可以通过自定义槽函数实现想要的功能

//行为连接到具体的槽函数
connect(fileCreateAction,SIGNAL(triggered()),this,SLOT(createFile()));  //槽函数需要实现
connect(fileSaveAction,SIGNAL(triggered()),this,SLOT(saveFile()));
connect(fileImportAction,SIGNAL(triggered()),this,SLOT(importFile()));
connect(fileExportAction,SIGNAL(triggered()),this,SLOT(exportFile()));

3.为菜单再添加菜单实现多级菜单

QMenu *optionMenu = new QMenu("Option",this);   //创建菜单
QAction *optionSystemAction = new QAction("System",this);
QMenu *ComparisionMenu = new QMenu("Comparison",this);//**这里创建再一个菜单
QAction *optionFindEdgeAction = new QAction("Find edge",this);
QAction *optionSetDecimalAction = new QAction("Set decima",this);
optionMenu->addAction(optionSystemAction);
//添加二级菜单
optionMenu->addMenu(ComparisionMenu);//***
QAction *openAction = new QAction("open",this);
QAction *closeAction = new QAction("close",this);
//为二级菜单添加行为
ComparisionMenu->addAction(openAction);
ComparisionMenu->addAction(closeAction);
 
optionMenu->addAction(optionFindEdgeAction);
optionMenu->addAction(optionSetDecimalAction);
menuBar->addMenu(optionMenu);

4.工具栏

//新建一个工具栏
    QToolBar *toolBar = new QToolBar("toolbar",this);
    //设置大小
    toolBar->setGeometry(,,width(),);
 
    //新建行为(Action)
    tAction1 = new QAction(QIcon(":/icon/icon/Vegetables-_11.png"),"Vegetables",this);
    tAction2 = new QAction(QIcon(":/icon/icon/Vegetables-_12.png"),"Vegetables",this);
    tAction3 = new QAction(QIcon(":/icon/icon/Vegetables-_13.png"),"Vegetables",this);
 
    //设置可选中,默认为false,triggered(bool)信号传递的bool永远为false
    tAction1->setCheckable(true);
    tAction2->setCheckable(true);
    tAction3->setCheckable(true);
 
    //可以通过判断是否选中设置不同的样式
    connect(tAction1,SIGNAL(triggered(bool)),this,SLOT(changeIcon(bool)));
 
    //添加行为
    toolBar->addAction(tAction1);
    toolBar->addAction(tAction2);
    toolBar->addAction(tAction3);
void MainWindow::changeIcon(bool boolValue)
{
    if(boolValue)
        tAction1->setIcon(QIcon(":/icon/icon/Vegetables-_1.png"));
    else
        tAction1->setIcon(QIcon(":/icon/icon/Vegetables-_11.png"));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值