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

相关文章

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

idea如何开启菜单栏

《idea如何开启菜单栏》文章介绍了如何通过修改IntelliJIDEA的样式文件`ui.lnf.xml`来重新显示被关闭的菜单栏,并分享了解决问题的步骤... 目录ijsdea开启菜单栏第一步第二步总结idea开启菜单栏手贱关闭了idea的js菜单栏,花费了半个小时终于解决,记录并分享一下第一步找

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

bat脚本启动git bash窗口,并执行命令方式

《bat脚本启动gitbash窗口,并执行命令方式》本文介绍了如何在Windows服务器上使用cmd启动jar包时出现乱码的问题,并提供了解决方法——使用GitBash窗口启动并设置编码,通过编写s... 目录一、简介二、使用说明2.1 start.BAT脚本2.2 参数说明2.3 效果总结一、简介某些情

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

基于Redis有序集合实现滑动窗口限流的步骤

《基于Redis有序集合实现滑动窗口限流的步骤》滑动窗口算法是一种基于时间窗口的限流算法,通过动态地滑动窗口,可以动态调整限流的速率,Redis有序集合可以用来实现滑动窗口限流,本文介绍基于Redis... 滑动窗口算法是一种基于时间窗口的限流算法,它将时间划分为若干个固定大小的窗口,每个窗口内记录了该时间