网站建设计划方案模板,网站文章编辑器代码,商丘至开网络科技有限公司,yande搜索引擎官网入口1、概述
QToolBar是Qt框架中的一个控件#xff0c;用于在工具栏中显示一组操作按钮和其他控件。它提供了一种方便的方式来组织和管理应用程序中的工具和操作。工具栏通常位于软件或应用程序界面的上方#xff0c;包含一系列常用工具和命令按钮#xff0c;用于快速访问和执行…1、概述
QToolBar是Qt框架中的一个控件用于在工具栏中显示一组操作按钮和其他控件。它提供了一种方便的方式来组织和管理应用程序中的工具和操作。工具栏通常位于软件或应用程序界面的上方包含一系列常用工具和命令按钮用于快速访问和执行常用功能。这些工具和命令通常以图标、文本或图标加文本的形式展示用户可以通过单击这些按钮来执行相应的操作从而提高工作效率。
2、重要方法
QToolBar提供了多种方法来定制和管理工具栏的外观和行为以下是一些重要的方法
addAction()向工具栏中添加一个动作QAction。这个动作可以是一个按钮、下拉菜单项或其他可交互的控件。addSeparator()在工具栏中添加一个分隔符用于将工具栏中的控件分组。addWidget()向工具栏中添加一个自定义的QWidget控件。setAllowedAreas()设置工具栏可以停靠的位置。这些位置包括左侧、右侧、顶部和底部等。setFloatable()设置工具栏是否可以浮动。如果设置为true工具栏可以在窗口中自由拖动如果设置为false工具栏则固定在其停靠位置。setMovable()设置工具栏是否可以移动。如果设置为true工具栏可以在其允许的停靠位置之间拖动如果设置为false工具栏则固定在当前位置。setIconSize()设置工具栏中图标的大小。setStyleSheet()设置工具栏的样式表用于自定义工具栏的外观。
3、重要信号
actionTriggered(QAction* action)当工具栏上的某个动作被触发时发出该信号。参数action指向被触发的动作。
#include QApplication
#include QMainWindow
#include QToolBar
#include QAction
#include QMessageBox
#include QVBoxLayout
#include QPushButtonint main(int argc, char *argv[])
{QApplication app(argc, argv);// 创建一个QMainWindow对象作为主窗口QMainWindow window;window.setWindowTitle(QToolBar Example);window.resize(800, 600);// 创建一个QToolBar对象QToolBar *toolBar new QToolBar(Main Toolbar, window);window.addToolBar(toolBar);// 创建动作QAction *newAction new QAction(QIcon(QApplication::style()-standardIcon(QStyle::SP_FileDialogNewFolder)), New, window);QAction *openAction new QAction(QIcon(QApplication::style()-standardIcon(QStyle::SP_DialogOpenButton)), Open, window);QAction *saveAction new QAction(QIcon(QApplication::style()-standardIcon(QStyle::SP_DialogSaveButton)), Save, window);QAction *exitAction new QAction(Exit, window);// 将动作添加到工具栏toolBar-addAction(newAction);toolBar-addAction(openAction);toolBar-addAction(saveAction);toolBar-addSeparator();toolBar-addAction(exitAction);// 设置工具提示newAction-setToolTip(Create a new file);openAction-setToolTip(Open an existing file);saveAction-setToolTip(Save the current file);exitAction-setToolTip(Exit the application);// 连接动作触发信号到槽函数QObject::connect(newAction, QAction::triggered, []() {QMessageBox::information(nullptr, New, New file created.);});QObject::connect(openAction, QAction::triggered, []() {QMessageBox::information(nullptr, Open, File opened.);});QObject::connect(saveAction, QAction::triggered, []() {QMessageBox::information(nullptr, Save, File saved.);});QObject::connect(exitAction, QAction::triggered, window, QMainWindow::close);// 显示窗口window.show();return app.exec();
}觉得有帮助的话打赏一下呗。。