Qt开发过程中如何将菜单变换成图标

目录

一、给菜单栏的菜单项添加图标

二、给工具栏添加图标

1.给工具栏添加图标

2.若是需要在工具栏的每个图标之间添加竖线分隔,可以加上以下代码

3.如需修改图标大小和文字大小在代码上添加以下代码

4.如需调整工具栏的内部间距和删除工具栏的边界线需要添加以下代码

5.如需图标与文字一起显示


一、给菜单栏的菜单项添加图标

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    QMainWindow mainWindow;
    mainWindow.setWindowTitle("窗口名字");

    // 创建菜单栏
    QMenuBar *menuBar = mainWindow.menuBar();

    // 创建菜单
    QMenu *menu = new QMenu("Menu", &mainWindow);

    // 创建菜单项,并设置图标
    QAction *iconAction = new QAction(&mainWindow);
    iconAction->setIcon(QIcon(":/path/to/icon.png")); // 设置图标路径

    // 隐藏菜单项的文本
    iconAction->setText("");

    // 将菜单项添加到菜单中
    menu->addAction(iconAction);

    // 将菜单添加到菜单栏
    menuBar->addMenu(menu);

    // 显示窗口
    mainWindow.show();

    return app.exec();
}

二、给工具栏添加图标

1.给工具栏添加图标

#include <QApplication>
#include <QMainWindow>
#include <QToolBar>
#include <QAction>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    QMainWindow mainWindow;
    mainWindow.setWindowTitle("窗口名称");

    // 创建工具栏
    QToolBar *toolBar = new QToolBar("Main Toolbar", &mainWindow);
    mainWindow.addToolBar(toolBar);

    // 创建工具栏项,并设置图标
    QAction *action1 = new QAction(QIcon(":/path/to/icon1.png"), "", &mainWindow); // 图标项1
    QAction *action2 = new QAction(QIcon(":/path/to/icon2.png"), "", &mainWindow); // 图标项2
    QAction *action3 = new QAction(QIcon(":/path/to/icon3.png"), "", &mainWindow); // 图标项3

    // 将工具栏项添加到工具栏
    toolBar->addAction(action1);
    toolBar->addAction(action2);
    toolBar->addAction(action3);

    // 显示窗口
    mainWindow.show();

    return app.exec();
}

2.若是需要在工具栏的每个图标之间添加竖线分隔,可以加上以下代码

toolBar->addSeparator();  // 添加分隔符

3.如需修改图标大小和文字大小在代码上添加以下代码

更改图标大小:使用QToolBarsetIconSize()方法来设置工具栏中所有图标的大小。

更改文字大小:可以通过设置工具栏的样式表来改变文字的大小。

#include <QFont>

// 设置图标大小
toolbar->setIconSize(QSize(60, 60));
// 设置字体大小
QFont font = toolbar->font();
// 设置文字大小,此时文字大小为16像素
font.setPointSize(16);
toolbar->setFont(font);

4.如需调整工具栏的内部间距和删除工具栏的边界线需要添加以下代码

#include <QToolBar>

// 调整工具栏内部间距
toolbar->setContentsMargins(10, 10, 10, 10);  // 设置工具栏内容的外边距
toolbar->setStyleSheet("QToolBar { spacing: 20px; }");  // 设置工具栏元素间距
// 移除工具栏边界线
toolbar->setStyleSheet("QToolBar { border: none; }");

5.如需图标与文字一起显示

//设置图标文字一起显示
toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

总结,工具栏完整的代码如下

#include <QApplication>
#include <QMainWindow>
#include <QToolBar>
#include <QAction>
#include <QFont>
#include <QToolBar>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    QMainWindow mainWindow;
    mainWindow.setWindowTitle("窗口名称");

    // 创建工具栏
    QToolBar *toolBar = new QToolBar("Main Toolbar", &mainWindow);
    mainWindow.addToolBar(toolBar);

    // 设置图标大小
    toolbar->setIconSize(QSize(60, 60));
    // 设置字体大小
    QFont font = toolbar->font();
    // 设置文字大小,此时文字大小为16像素
    font.setPointSize(16);
    toolbar->setFont(font);

    // 创建工具栏项,并设置图标
    QAction *action1 = new QAction(QIcon(":/path/to/icon1.png"), "", &mainWindow); // 图标项1
    QAction *action2 = new QAction(QIcon(":/path/to/icon2.png"), "", &mainWindow); // 图标项2
    QAction *action3 = new QAction(QIcon(":/path/to/icon3.png"), "", &mainWindow); // 图标项3

    // 将工具栏项添加到工具栏
    toolBar->addAction(action1);
    toolBar->addSeparator();  // 添加分隔符
    toolBar->addAction(action2);
    toolBar->addSeparator();  // 添加分隔符
    toolBar->addAction(action3);

    // 调整工具栏内部间距
    toolbar->setContentsMargins(10, 10, 10, 10);  // 设置工具栏内容的外边距
    toolbar->setStyleSheet("QToolBar { spacing: 20px; }");  // 设置工具栏元素间距
    // 移除工具栏边界线
    toolbar->setStyleSheet("QToolBar { border: none; }");  

    //设置图标文字一起显示
    toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);  

    // 显示窗口
    mainWindow.show();

    return app.exec();
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值