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

showwidget

showwidget.h

#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QImage>

class showwidget : public QWidget
{
    Q_OBJECT

public:
    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);

    //实现图片旋转
    //旋转90
    Rotate90Action = new QAction(tr("旋转90"),this);
    Rotate90Action->setStatusTip(tr("将一幅图片旋转90"));
    connect(Rotate90Action,&QAction::triggered,this,&Processor::ShowRotate90);
    //旋转180
    Rotate180Action = new QAction(tr("旋转180"),this);
    Rotate180Action->setStatusTip(tr("将一幅图片旋转180"));
    connect(Rotate180Action,&QAction::triggered,this,&Processor::ShowRotate180);
    //旋转270
    Rotate270Action = 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();
}

运行图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值