本文主要是介绍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窗口组件-菜单栏-工具栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!