赞
踩
1、主窗口中的工具栏
工具栏的概念和意义
— 应用程序中 集成各种功能 实现快捷使用的一个区域
— 工具栏并不是应用程序中必须存在的组件
— 工具栏中的元素可以是各种窗口组件
— 工具栏中的元素通常以图标按钮的方式存在
在 Qt 中提供与工具栏相关的类组件
在 Qt 主窗口中创建工具栏
QToolBar 的关键成员函数
— void setFloatable(bool floatable)
设置当前工具栏能不能悬浮,参数一般设置成 false
— void setMovable(bool movable)
设置当前工具栏能不能移动,参数一般设置成 false
— void setIconSize(const QSize& iconSize)
设置工具栏的每一个图标大小
QToolBar 中可以加入任意的QWidget 组件
工具栏初体验:
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); }; #endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h" #include "QToolBar" #include <QAction> #include <QPushButton> #include <QLabel> #include <QLineEdit> MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) { QToolBar* tb = addToolBar("ToolBar"); QAction* action = new QAction("", nullptr); action->setToolTip("Open"); action->setIcon(QIcon(":/Res/open.png")); QPushButton* btn = new QPushButton("btn"); QLabel* label = new QLabel("label"); QLineEdit* edit = new QLineEdit("xiebs"); tb->setFloatable(false); tb->setMovable(false); tb->addAction(action); tb->addWidget(btn); tb->addWidget(label); tb->addWidget(edit); } MainWindow::~MainWindow() { }
main.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
工具栏实例:
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMenuBar> #include <QToolBar> #include <QMenu> #include <QAction> class MainWindow : public QMainWindow { Q_OBJECT private: MainWindow(); MainWindow(const MainWindow&); MainWindow& operator= (const MainWindow&); bool construct(); bool initMenuBar(); bool initToolBar(); bool initFileMenu(QMenuBar* mb); bool initEditMenu(QMenuBar* mb); bool initFormatMenu(QMenuBar* mb); bool initViewMenu(QMenuBar* mb); bool initHelpMenu(QMenuBar* mb); bool initFileToolItem(QToolBar* tb); bool initEditToolItem(QToolBar* tb); bool initFormatToolItem(QToolBar* tb); bool initViewToolItem(QToolBar* tb); bool makeAction(QAction*& action, QString text, int key); bool makeAction(QAction*& action, QString tip, QString icon); public: static MainWindow* NewInstance(); ~MainWindow(); }; #endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h" #include <QIcon> MainWindow::MainWindow() { resize(500, 400); } MainWindow* MainWindow::NewInstance() { MainWindow* ret = new MainWindow(); if(!(ret && ret->construct())) { delete ret; ret = nullptr; } return ret; } bool MainWindow::construct() { bool ret = true; ret = ret && initMenuBar(); ret = ret && initToolBar(); return ret; } bool MainWindow::initMenuBar() { </
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。