qt-19 QMainWindow窗口组件-菜单栏-工具栏

2024-08-26 23:52

本文主要是介绍qt-19 QMainWindow窗口组件-菜单栏-工具栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

QMainWindow窗口组件-菜单栏-工具栏

  • showwidget
    • showwidget.h
    • showwidget.cpp
  • processor
    • processor.h
    • processor.cpp
  • main.cpp
  • 运行图

showwidget

showwidget.h

#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QImage>class showwidget : public QWidget
{Q_OBJECTpublic:showwidget(QWidget *parent = nullptr);~showwidget();QImage* Img;QLabel* ImageLabel;QTextEdit* Text;};
#endif // SHOWWIDGET_H}showwidget::~showwidget() {}

showwidget.cpp

#include "showwidget.h"
#include <QHBoxLayout>showwidget::showwidget(QWidget *parent): QWidget(parent)
{ImageLabel = new QLabel;ImageLabel->setScaledContents(true);//图片可以大小自动缩放Text = new QTextEdit;QHBoxLayout* MainLayout = new QHBoxLayout(this);MainLayout->addWidget(ImageLabel);MainLayout->addWidget(Text);
}showwidget::~showwidget() {}

processor

processor.h

#ifndef PROCESSOR_H
#define PROCESSOR_H
#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
#include <QFileDialog>
#include <QTextStream>
#include <QFile>
#include <QDebug>
#include "showwidget.h"
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QTransform>class Processor : public QMainWindow
{Q_OBJECT
public:Processor(QWidget *parent = nullptr);~Processor();void CreateActions();void CreateMenus();void CreateToolBars();void LoadFile(QString tFileName);void MergeFormat(QTextCharFormat);private:showwidget* ShowWidget;//各项菜单栏QMenu* FileMenu;QMenu* ZoomMenu;QMenu* RotateMenu;QMenu* MirrorMenu;QImage Img;QString FileName;//文件菜单项QAction* OpenFileAction;//文件菜单QAction* NewFileAction;QAction* ExitAction;QAction* PrintTextAction;QAction* PrintImageAction;QAction* EditAction;QAction* CopyAction; //编辑菜单项QAction* CutAction;QAction* PastAction;QAction* AboutAction;QAction* ZoomInAction;QAction* ZoomOutAction;QAction* Rotate90Action;//旋转菜单项QAction* Rotate180Action;QAction* Rotate270Action;QAction* MirrorVerticalAction;//镜像菜单项QAction* MirrorHorizontalAction;QAction* UndoAction;QAction* RedoAction;QToolBar* FileTool;//工具栏QToolBar* ZoomTool;QToolBar* RotateTool;QToolBar* MirrorTool;QToolBar* DoToolBar;
signals:
protected slots:void ShowNewFile();void ShowOpenFile();void ShowPrintImage();void ShowZoomIn();void ShowZoomOut();void ShowRotate90();void ShowRotate180();void ShowRotate270();void ShowMirrorVertical();void ShowMirrorHorizontal();};#endif // PROCESSOR_H

processor.cpp

#include "processor.h"
#include <QApplication>
#include <QToolBar>Processor::Processor(QWidget *parent): QMainWindow{parent}
{setWindowTitle(tr("Easy Word"));ShowWidget = new showwidget(this);setCentralWidget(ShowWidget);//创建动作、菜单、工具栏CreateActions();CreateMenus();CreateToolBars();if(Img.load("text.png")){//把图片放到ImageLabel  标签中ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));}// ShowWidget->ImageLabel->setPixmap(QApplication::style()->standardPixmap(QStyle::SP_TitleBarCloseButton));}Processor::~Processor()
{}void Processor::CreateActions()
{//打开OpenFileAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(42)),tr("打开"),this);OpenFileAction->setShortcut(tr("Ctrl+O"));OpenFileAction->setStatusTip(tr("打开一个文件"));connect(OpenFileAction,&QAction::triggered,this,&Processor::ShowOpenFile);//新建NewFileAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(32)),tr("新建"),this);NewFileAction->setShortcut(tr("Ctrl+N"));NewFileAction->setStatusTip(tr("新建一个文件"));connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));//退出ExitAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(11)),tr("退出"),this);ExitAction->setShortcut(tr("Ctrl+Q"));ExitAction->setStatusTip(tr("退出程序"));connect(ExitAction,SIGNAL(triggered()),this,SLOT(close()));//复制CopyAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(21)),tr("复制"),this);CopyAction->setShortcut(tr("Ctrl+C"));CopyAction->setStatusTip(tr("复制文件"));//connect(CopyAction,SIGNAL(triggered()),this,SLOT(copy()));//剪切CutAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(22)),tr("剪切"),this);CutAction->setShortcut(tr("Ctrl+X"));CutAction->setStatusTip(tr("剪切文件"));//connect(CutAction,SIGNAL(triggered()),this,SLOT(cut()));//粘贴PastAction = new QAction(QApplication::style()->standardIcon(QStyle::StandardPixmap(22)),tr("粘贴"),this);PastAction->setShortcut(tr("Ctrl+V"));PastAction->setStatusTip(tr("粘贴文件"));//connect(PastAction,SIGNAL(triggered()),this,SLOT(paste()));//关于AboutAction = new QAction(tr("关于"),this);connect(AboutAction,&QAction::triggered,this,[=]() { QApplication::aboutQt(); });//打印文本PrintTextAction = new QAction(tr("打印文本"),this);PrintTextAction->setStatusTip(tr("打印一个文本"));//打印图片PrintImageAction = new QAction(tr("打印图片"),this);PrintImageAction->setStatusTip(tr("打印一个图片"));connect(PrintImageAction,&QAction::triggered,this,&Processor::ShowPrintImage);//放大动作ZoomInAction = new QAction(tr("放大动作"),this);ZoomInAction->setStatusTip(tr("放大一幅图片"));connect(ZoomInAction,&QAction::triggered,this,&Processor::ShowZoomIn);//缩小动作ZoomOutAction = new QAction(tr("缩小动作"),this);ZoomOutAction->setStatusTip(tr("缩小一幅图片"));connect(ZoomOutAction,&QAction::triggered,this,&Processor::ShowZoomOut);//实现图片旋转//旋转90Rotate90Action = new QAction(tr("旋转90"),this);Rotate90Action->setStatusTip(tr("将一幅图片旋转90"));connect(Rotate90Action,&QAction::triggered,this,&Processor::ShowRotate90);//旋转180Rotate180Action = new QAction(tr("旋转180"),this);Rotate180Action->setStatusTip(tr("将一幅图片旋转180"));connect(Rotate180Action,&QAction::triggered,this,&Processor::ShowRotate180);//旋转270Rotate270Action = new QAction(tr("旋转270"),this);Rotate270Action->setStatusTip(tr("将一幅图片旋转270"));connect(Rotate270Action,&QAction::triggered,this,&Processor::ShowRotate270);//实现横向和纵向MirrorVerticalAction = new QAction(tr("纵向图片"),this);MirrorVerticalAction->setStatusTip(tr("将一幅图片纵向"));connect(MirrorVerticalAction,&QAction::triggered,this,&Processor::ShowMirrorVertical);MirrorHorizontalAction = new QAction(tr("横向图片"),this);MirrorHorizontalAction->setStatusTip(tr("将一幅图片横向"));connect(MirrorHorizontalAction,&QAction::triggered,this,&Processor::ShowMirrorHorizontal);//撤销和重做UndoAction =new QAction(tr("撤销"),this);connect(UndoAction,SIGNAL(triggered()),ShowWidget->Text,SLOT(undo()));RedoAction =new QAction(tr("重做"),this);connect(RedoAction,SIGNAL(triggered()),ShowWidget->Text,SLOT(redo()));}void Processor::CreateMenus()
{//文件类FileMenu = menuBar()->addMenu(tr("文件"));FileMenu->addAction(OpenFileAction);FileMenu->addAction(NewFileAction);FileMenu->addAction(PrintTextAction);FileMenu->addAction(PrintImageAction);FileMenu->addSeparator();FileMenu->addAction(ExitAction);//缩放菜单ZoomMenu = menuBar()->addMenu(tr("编辑"));ZoomMenu->addAction(CopyAction);ZoomMenu->addAction(CutAction);ZoomMenu->addAction(PastAction);ZoomMenu->addAction(AboutAction);ZoomMenu->addSeparator();//增加一根横线ZoomMenu->addAction(ZoomInAction);ZoomMenu->addAction(ZoomOutAction);//旋转菜单RotateMenu = menuBar()->addMenu(tr("旋转"));RotateMenu->addAction(Rotate90Action);RotateMenu->addAction(Rotate180Action);RotateMenu->addAction(Rotate270Action);//镜像菜单MirrorMenu = menuBar()->addMenu(tr("镜像"));MirrorMenu->addAction(MirrorVerticalAction);MirrorMenu->addAction(MirrorHorizontalAction);
}void Processor::CreateToolBars()
{//文件工具条FileTool = addToolBar("File");FileTool->addAction(OpenFileAction);FileTool->addAction(NewFileAction);FileTool->addAction(PrintTextAction);FileTool->addAction(PrintImageAction);//编辑工具条ZoomTool = addToolBar("Edit");ZoomTool->addAction(CopyAction);ZoomTool->addAction(CutAction);ZoomTool->addAction(PastAction);ZoomTool->addSeparator();ZoomTool->addAction(ZoomInAction);ZoomTool->addAction(ZoomOutAction);//旋转工具条RotateTool = addToolBar("Rotate");RotateTool->addAction(Rotate90Action);RotateTool->addAction(Rotate180Action);RotateTool->addAction(Rotate270Action);RotateTool->addSeparator();RotateTool->addAction(UndoAction);RotateTool->addAction(RedoAction);//撤销和重做工具条 5.15debug 版本超过 4个addToolBar 会导致程序崩溃/*DoToolBar = addToolBar("123");DoToolBar->addAction(UndoAction);DoToolBar->addAction(RedoAction);*/
}void Processor::LoadFile(QString tFileName)
{qDebug()<< "filename:" << tFileName;QFile File(tFileName);if(File.open(QIODevice::ReadOnly|QIODevice::Text)){QTextStream TextStream(&File);while(!TextStream.atEnd()){ShowWidget->Text->append(TextStream.readLine());printf("read line\n");}qDebug()<< "end\n";}
}void Processor::MergeFormat(QTextCharFormat)
{}void Processor::ShowNewFile()
{Processor* NewProcessor = new Processor;NewProcessor->show();
}void Processor::ShowOpenFile()
{FileName = QFileDialog::getOpenFileName(this);if(!FileName.isEmpty()){if(ShowWidget->Text->document()->isEmpty()){LoadFile(FileName);}else{Processor* NewProcessor = new Processor;NewProcessor->show();NewProcessor->LoadFile(FileName);}}}void Processor::ShowPrintImage()
{QPrinter Printer;QPrintDialog PrintDialog(&Printer,this);if(PrintDialog.exec()){QPainter Painter(&Printer);QRect Rect =Painter.viewport();QSize Size =Img.size();Size.scale(Rect.size(),Qt::KeepAspectRatio);Painter.setViewport(Rect.x(),Rect.y(),Rect.width(),Rect.height());Painter.setWindow(Img.rect());Painter.drawImage(0,0,Img);}}void Processor::ShowZoomIn()
{if(Img.isNull()){return;}QTransform transform;transform.scale(2, 2);Img = Img.transformed(transform);//重新设置显示图形ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));}void Processor::ShowZoomOut()
{if(Img.isNull()){return;}QTransform transform;transform.scale(0.5, 0.5);Img = Img.transformed(transform);//重新设置显示图形ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));}void Processor::ShowRotate90()
{if(Img.isNull()){return;}QTransform transform;transform.rotate(90);Img = Img.transformed(transform);//重新设置显示图形ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));
}void Processor::ShowRotate180()
{if(Img.isNull()){return;}QTransform transform;transform.rotate(180);Img = Img.transformed(transform);//重新设置显示图形ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));
}void Processor::ShowRotate270()
{if(Img.isNull()){return;}QTransform transform;transform.rotate(270);Img = Img.transformed(transform);//重新设置显示图形ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));
}void Processor::ShowMirrorVertical()
{if(Img.isNull()){return;}Img = Img.mirrored(false,true);ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));
}void Processor::ShowMirrorHorizontal()
{if(Img.isNull()){return;}Img = Img.mirrored(true,false);ShowWidget->ImageLabel->setPixmap(QPixmap::fromImage(Img));
}

main.cpp

#include "processor.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Processor w;w.show();return a.exec();
}

运行图

在这里插入图片描述

这篇关于qt-19 QMainWindow窗口组件-菜单栏-工具栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110045

相关文章

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

vue2 组件通信

props + emits props:用于接收父组件传递给子组件的数据。可以定义期望从父组件接收的数据结构和类型。‘子组件不可更改该数据’emits:用于定义组件可以向父组件发出的事件。这允许父组件监听子组件的事件并作出响应。(比如数据更新) props检查属性 属性名类型描述默认值typeFunction指定 prop 应该是什么类型,如 String, Number, Boolean,

kubelet组件的启动流程源码分析

概述 摘要: 本文将总结kubelet的作用以及原理,在有一定基础认识的前提下,通过阅读kubelet源码,对kubelet组件的启动流程进行分析。 正文 kubelet的作用 这里对kubelet的作用做一个简单总结。 节点管理 节点的注册 节点状态更新 容器管理(pod生命周期管理) 监听apiserver的容器事件 容器的创建、删除(CRI) 容器的网络的创建与删除

使用JS/Jquery获得父窗口的几个方法(笔记)

<pre name="code" class="javascript">取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);如题: $(selector, window.top.document);//获得顶级窗口里面的元素 $(

【QT】基础入门学习

文章目录 浅析Qt应用程序的主函数使用qDebug()函数常用快捷键Qt 编码风格信号槽连接模型实现方案 信号和槽的工作机制Qt对象树机制 浅析Qt应用程序的主函数 #include "mywindow.h"#include <QApplication>// 程序的入口int main(int argc, char *argv[]){// argc是命令行参数个数,argv是

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点

火语言RPA流程组件介绍--浏览网页

🚩【组件功能】:浏览器打开指定网址或本地html文件 配置预览 配置说明 网址URL 支持T或# 默认FLOW输入项 输入需要打开的网址URL 超时时间 支持T或# 打开网页超时时间 执行后后等待时间(ms) 支持T或# 当前组件执行完成后继续等待的时间 UserAgent 支持T或# User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器

使用Qt编程QtNetwork无法使用

使用 VS 构建 Qt 项目时 QtNetwork 无法使用的问题 - 摘叶飞镖 - 博客园 (cnblogs.com) 另外,强烈建议在使用QNetworkAccessManager之前看看这篇文章: Qt 之 QNetworkAccessManager踏坑记录-CSDN博客 C++ Qt开发:QNetworkAccessManager网络接口组件 阅读目录 1.1 通用API函数