QT5综合实例:文本编辑器

实现效果

在这里插入图片描述

具体代码(相关笔记都写在代码注释里了)

在这里插入图片描述
imgprocessor.h文件

#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H

#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
#include "showwidget.h"

class ImgProcessor : public QMainWindow
{
    Q_OBJECT

public:
    ImgProcessor(QWidget *parent = 0);
    ~ImgProcessor();

    void createActions();
    void createMenus();
    void createToolBars();
    void loadFile(QString filename);
    void mergeFormat(QTextCharFormat format);

private:
    //各项菜单栏
    QMenu *fileMenu;//文件菜单
    QMenu *zoomMenu;//编辑菜单
    QMenu *rotateMenu;//旋转菜单
    QMenu *mirrorMenu;//镜像菜单
    //图片
    QImage img;

    QString fileName;

    ShowWidget *showwidget;//调用类声明

    //文件相关菜单项
    QAction *openFileAction;
    QAction *NewFileAction;
    QAction *PrintTextAction;
    QAction *PrintImageAction;
    QAction *exitAction;

    //编辑相关菜单项
    QAction *copyAction;
    QAction *cutAction;
    QAction *pasteAction;
    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;
    QToolBar *fontToolBar;//字体设置工具栏
//    QToolBar *listToolBar;
//*****************************************************************!!!!!!!!!
protected slots:
    void ShowZoomIn();//添加一个放大功能的槽函数
    void ShowZoomOut();//添加一个缩小功能的槽函数
    void ShowRotate90();//旋转
    void ShowRotate180();//旋转
    void ShowRotate270();//旋转
    void ShowMirrorVertical();//纵向镜像
    void ShowMirrorHorizontal();//横向镜像

    void ShowFontComboBox(QString comboStr);
    void ShowSizeSpinBox(QString spinValue);
    void ShowBoldBtn();
    void ShowItalicBtn();
    void ShowUnderlineBtn();
    void ShowColorBtn();
    void ShowCurrentFormatChanged(const QTextCharFormat &fmt);


private:
    QLabel *fontLabel1;
    QFontComboBox *fontComboBox;//字体选择盒子
    QLabel *fontLabel2;
    QComboBox *sizeComboBox;//字体大小选择盒子
    QToolButton *boldBtn;//加粗
    QToolButton *italicBtn;
    QToolButton *underlineBtn;
    QToolButton *colorBtn;

    QLabel *listLabel;     //排序设置项标签
    QComboBox *listComboBox;//排序工具栏
    QActionGroup *actGrp;
    QAction *leftAction;
    QAction *rightAction;
    QAction *centerAction;
    QAction *justifyAction;
//    QAction *listToolBar;


private slots:
    void ShowNewFile();//添加一个显示新建文件的槽函数
    void ShowOpenFile();//添加一个打开文件的槽函数
    void ShowList();//文本排序
    void ShowAlignment(QAction *act);//实现段落对齐
    void ShowCursorPositionChanged();//相应光标处文本对齐的情况



};

#endif // IMGPROCESSOR_H

showwidget.h文件

#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

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

class ShowWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ShowWidget(QWidget *parent = 0);
    QImage img;
    QLabel *imageLabel;
    QTextEdit *text;
signals:

public slots:

};

#endif // SHOWWIDGET_H

imgprocessor.cpp文件

#include "imgprocessor.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QTextCursor>
#include <QColorDialog>
#include <QColor>
#include <QList>
ImgProcessor::ImgProcessor(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Easy Word Editor!"));
    showwidget= new ShowWidget(this);
    setCentralWidget(showwidget);             //界面设定在showWidget中,这里把showwidget直接设置为中心窗口就行了
/*********************文本编辑设置(全局相关直接放构造函数)*****************************************/

    fontLabel1= new QLabel(tr("字体:"));
    fontComboBox= new QFontComboBox;//QFontComboBox()是QComboBox()的一个子类,但是它的内容是不能被编辑的,主要是用来选择字体
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
    //QFontComboBox::ScalableFonts显示可缩放字体

    fontLabel2= new QLabel(tr("字号:"));
    sizeComboBox= new QComboBox;

    QFontDatabase db;//QFontDatabase类提供有关基础窗口系统中可用字体的信息
    foreach(int size,db.standardSizes())
        sizeComboBox->addItem(QString::number(size));
    //foreach是QT中一个用来代替for循环的函数。参数定义:(每个元素的变量,程序中需要循环读取的一个列表)

    //如果是用addwidget的形式加入的工具栏,则需要在按钮处添加设置文本和显示格式
    //如果使用addtoolbar的形式加入的工具栏,则需要先添加动作,动作包含相应的操作和调用,再在toolbar中统一设置显示格式即可

    boldBtn= new QToolButton;
    boldBtn->setIcon(QIcon(":/image/111.jpg"));
    boldBtn->setText("加粗");
    boldBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    boldBtn->setCheckable(true);//设置控件是否可被勾选,true则“可被勾选”

    italicBtn= new QToolButton;
    italicBtn->setIcon(QIcon(":/image/111.jpg"));
    italicBtn->setText("斜体");
    italicBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    italicBtn->setCheckable(true);

    underlineBtn= new QToolButton;
    underlineBtn->setIcon(QIcon(":/image/111.jpg"));
    underlineBtn->setText("下划线");
    underlineBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    underlineBtn->setCheckable(true);

    colorBtn= new QToolButton;
    colorBtn->setIcon(QIcon(":/image/111.jpg"));
    colorBtn->setText("颜色");
    colorBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    colorBtn->setCheckable(true);

//    createActions();


    connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(ShowFontComboBox(QString)));
    //activated(QString)选中就触发,并返回一个QString,SLOT函数则是展示字体
    connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(ShowSizeSpinBox(QString)));
    connect(boldBtn,SIGNAL(clicked()),this,SLOT(ShowBoldBtn()));
    connect(italicBtn,SIGNAL(clicked()),this,SLOT(ShowItalicBtn()));
    connect(underlineBtn,SIGNAL(clicked()),this,SLOT(ShowUnderlineBtn()));
    connect(colorBtn,SIGNAL(clicked()),this,SLOT(ShowColorBtn()));

    connect(showwidget->text,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(ShowCurrentFormatChanged(QTextCharFormat)));
    //currentCharFormatChanged(QTextCharFormat f):当前文本格式控制被改变时发射本信号
    //当文本格式收到变化,即刻改变,调用光标变换重设状态栏显示函数


/********************************************************************************************/

    //创建动作、菜单、工具栏的函数
    createActions();
    createMenus();
    createToolBars();

    if(img.load(":/image/111.jpg"))
    {
        showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    }
}

ImgProcessor::~ImgProcessor()
{

}
/************************************************************************************/

void ImgProcessor::createActions()//动作的实现
{
    //打开
    openFileAction= new QAction(QIcon(":/image/111.jpg"),tr("打开"),this);//设置打开的图标和文字说明
    openFileAction->setShortcut(tr("Ctrl+O"));//设置快捷键
    openFileAction->setStatusTip(tr("打开一个文件"));//设置状态条显示
    connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));

    //新建
    NewFileAction= new QAction(QIcon(":/image/111.jpg"),tr("新建"),this);
    NewFileAction->setShortcut(tr("Ctrl+N"));                       //setShortcut是设置快捷键的文本
    NewFileAction->setStatusTip(tr("新建一个文件"));
    connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));
    //退出
    exitAction= new QAction(QIcon(":/image/111.jpg"),tr("退出"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("退出程序"));
    connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));

    //复制
    //connect连接的是当copyaction被点击的时候触发showwidget里面的字做出的反应
    copyAction= new QAction (QIcon(":/image/111.jpg"),tr("复制"),this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("复制"));
    connect(copyAction,SIGNAL(triggered()),showwidget->text,SLOT(copy()));

    //剪切
    cutAction= new QAction(QIcon(":/image/111.jpg"),tr("剪切"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切"));
    connect(cutAction,SIGNAL(triggered()),showwidget->text,SLOT(cut()));

    //粘贴
    pasteAction= new QAction(QIcon(":/image/111.jpg"),tr("粘贴"),this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("粘贴"));
    connect(pasteAction,SIGNAL(triggered()),showwidget->text,SLOT(paste()));

    //关于
    aboutAction= new QAction(QIcon(":/image/111.jpg"),tr("关于"),this);
    connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt()));

//****************************************************!!!!!!!!!!

    //打印文本
    PrintTextAction= new QAction(QIcon(":/image/111.jpg"),tr("打印文本"),this);
    PrintTextAction->setStatusTip(tr("打印文本"));
    //connect();学到文件后补全

    //打印图像
    PrintImageAction= new QAction(QIcon(":/image/111.jpg"),tr("打印图片"),this);
    PrintImageAction->setStatusTip(tr("打印图片"));
    //connect();学到后补全

    //放大
    zoomInAction= new QAction(QIcon(":/image/111.jpg"),tr("放大"),this);
    zoomInAction->setStatusTip(tr("放大"));
    connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn()));

    //缩小
    zoomOutAction= new QAction(QIcon(":/image/111.jpg"),tr("缩小"),this);
    zoomOutAction->setStatusTip(tr("缩小"));
    connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut()));

    //旋转90
    rotate90Action= new QAction(QIcon(":/image/111.jpg"),tr("旋转90"),this);
    rotate90Action->setStatusTip(tr("旋转90"));
    connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90()));

    //旋转180
    rotate180Action= new QAction(QIcon(":/image/111.jpg"),tr("旋转180"),this);
    rotate180Action->setStatusTip(tr("旋转180"));
    connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180()));

    //旋转270
    rotate270Action= new QAction(QIcon(":/image/111.jpg"),tr("旋转270"),this);
    rotate270Action->setStatusTip(tr("旋转270"));
    connect(rotate270Action,SIGNAL(triggered()),this,SLOT(ShowRotate270()));

    //纵向镜像
    mirrorVerticalAction= new QAction(QIcon(":/image/111.jpg"),tr("纵向镜像"),this);
    mirrorVerticalAction->setStatusTip(tr("纵向镜像"));
    connect(mirrorVerticalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorVertical()));

    //横向镜像
    mirrorHorizontalAction= new QAction(QIcon(":/image/111.jpg"),tr("横向镜像"),this);
    mirrorHorizontalAction->setStatusTip(tr("横向镜像"));
    connect(mirrorHorizontalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorHorizontal()));
    //撤销&恢复
    undoAction= new QAction(QIcon(":/image/111.jpg"),tr("撤销"),this);
    connect(undoAction,SIGNAL(triggered()),showwidget->text,SLOT(undo()));

    redoAction= new QAction(QIcon(":/image/111.jpg"),tr("恢复"),this);
    connect(redoAction,SIGNAL(triggered()),showwidget->text,SLOT(redo()));

    //文本对齐
    actGrp= new QActionGroup(this);
    leftAction= new QAction(QIcon(":/image/111.jpg"),"左对齐",this);
    leftAction->setCheckable(true);//能被点击
    rightAction= new QAction(QIcon(":/image/111.jpg"),"右对齐",this);
    rightAction->setCheckable(true);//能被点击
    centerAction= new QAction(QIcon(":/image/111.jpg"),"中间对齐",this);
    centerAction->setCheckable(true);//能被点击
    justifyAction= new QAction(QIcon(":/image/111.jpg"),"两端对齐",this);
    justifyAction->setCheckable(true);//能被点击
    connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*)));//调用点击函数

}
/************************************************************************************/

void ImgProcessor::createMenus()//菜单
{
     //菜单通过直接调用函数menubar()再调用其下的addmenu()实现,再通过addAction插入子项
     fileMenu= menuBar()->addMenu("文件");
     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(pasteAction);
     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(mirrorHorizontalAction);
     mirrorMenu->addAction(mirrorVerticalAction);

}

void ImgProcessor::createToolBars()//工具栏
{
    //工具条通过函数addToolbar()及addmenu()实现,再通过addAction插入子项
    fileTool= addToolBar("文件");
    fileTool->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    fileTool->addAction(openFileAction);//插入之前设定的动作
    fileTool->addAction(NewFileAction);
    fileTool->addAction(PrintTextAction);
    fileTool->addAction(PrintImageAction);

    zoomTool= addToolBar("编辑");
    zoomTool->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    zoomTool->addAction(copyAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(pasteAction);
    zoomTool->addSeparator();
    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction);

    rotateTool= addToolBar("旋转");
    rotateTool->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);//设置这个工具栏的样式,即图标和文字如何显示,有几种样式可选
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action);

    doToolBar= addToolBar("撤销和恢复");
    doToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction);

    //如果是用addwidget的形式加入的工具栏,则需要在按钮处添加设置文本和显示格式
    //如果使用addtoolbar的形式加入的工具栏,则需要先添加动作,动作包含相应的操作和调用,再在toolbar中统一设置显示格式即可

    fontToolBar= addToolBar("字体");
    fontToolBar->addWidget(fontLabel1);
    fontToolBar->addWidget(fontComboBox);
    fontToolBar->addWidget(fontLabel2);
    fontToolBar->addWidget(sizeComboBox);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(boldBtn);
    fontToolBar->addWidget(italicBtn);
    fontToolBar->addWidget(underlineBtn);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(colorBtn);

    //*****************************************************************!!!!!!!!!

//    listToolBar= addToolBar("对齐方式");
//    listToolBar->addWidget(listLabel);
//    listToolBar->addWidget(listComboBox);
//    listToolBar->addSeparator();
//    listToolBar->addActions(actGrp->actions());

}
/********************************文件相关**********************************************/

void ImgProcessor::ShowNewFile()//新建文件显示
{
    ImgProcessor *newImgProcessor= new ImgProcessor;//就是再开辟一个区域来放一个新窗口
    newImgProcessor->show();                        //跟主函数中的show()是基本一样的
}

void ImgProcessor::ShowOpenFile()//打开文件显示
{
    fileName= QFileDialog::getOpenFileName(this);//首先获取文件名
    if(!fileName.isEmpty())//如果文件名不为空
    {
        if(showwidget->text->document()->isEmpty())//判断当前文件内是否有内容,如果没有则文件在这里打开
                                                   //如果有,则新建一个窗口打开,并且把这个窗口的名字设为读取到的文件名
        {
           loadFile(fileName);
        }
        else
        {
            ImgProcessor *newImgProcessor= new ImgProcessor;
            newImgProcessor->show();
            newImgProcessor->loadFile(fileName);
        }
    }
}



void ImgProcessor::loadFile(QString filename)//文件载入
{
    printf("file name:");
    printf("%s\n",filename.data());
    QFile file(filename);//读取filename文件的路径
    if(file.open(QIODevice::ReadOnly|QIODevice::Text))//QIOdevice:文件打开方式:QIODevice::NotOpen 未打开

    {
        QTextStream textSteam(&file);//读写文件接口
        while(!textSteam.atEnd())//当文件没有到结束的时候,就一直读取文件的下一行,并在控制器打印一个“readLine”
        {
            showwidget->text->append(textSteam.readLine());
            printf("read line\n");
        }
        printf("end\n");//结束停止循环,后台打印提示符
    }

    //        QIODevice::ReadOnly 以只读方式打开
    //        QIODevice::WriteOnly 以只写方式打开
    //        QIODevice::ReadWrite 以读写方式打开
    //        QIODevice::Append 以追加的方式打开
    //        新增加的内容将被追加到文件末尾
    //        QIODevice::Truncate 以重写的方式打开,在写入新的数据时会将原有
    //        数据全部清除,游标设置在文件开头。
    //        QIODevice::Text 在读取时,将行结束符转换成 \n;在写入时,将行结束符转换成本地格式,例如 Win32 平台上是 \r\n
    //        QIODevice::Unbuffered 忽略缓存
}


/********************************图像相关**********************************************/
//QMatrix类提供了世界坐标系统的二维转换功能,可以使窗体(图像)转换变形,实现坐标系统的移动,缩放,变形和旋转功能

void ImgProcessor::ShowZoomIn()//图像放大功能
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.scale(1.1,1.1);//按照比例进行水平和垂直方向上的放大
    img= img.transformed(matrix);//放大之后再把img替换成新的图片
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));//最后再进行图片重设置
}

void ImgProcessor::ShowZoomOut()//图像缩小功能,同放大
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.scale(0.9,0.9);//按照比例进行水平和垂直方向上的缩小
    img= img.transformed(matrix);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate90()//旋转90度
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.rotate(90);
    img= img.transformed(matrix);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate180()//旋转180度
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.rotate(180);
    img= img.transformed(matrix);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate270()//旋转270度
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.rotate(270);
    img= img.transformed(matrix);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
//*******************************************************************
void ImgProcessor::ShowMirrorVertical()//镜像
{
    if(img.isNull())
        return;

    img= img.mirrored(false,true);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowMirrorHorizontal()//镜像
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.rotate(270);
    img= img.mirrored(true,false);
    showwidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

/********************************文本编辑相关**********************************************/

/*************/
void ImgProcessor::ShowFontComboBox(QString comboStr)//设置字体(字体选择的时候)
{
    QTextCharFormat fmt;//创建一个QTextCharFormat对象
    fmt.setFontFamily(comboStr);//给对象设置选中的字体
    mergeFormat(fmt);//新格式给光标区内的字符
}


void ImgProcessor::mergeFormat(QTextCharFormat format)//光标选中设置
{
    QTextCursor cursor= showwidget->text->textCursor();//获取编辑框中的光标

    if(!cursor.hasSelection())//cursor光标
        cursor.select(QTextCursor::WordUnderCursor);//若光标处没有高亮选区,则将光标处的词作为选区,由前后空格或标点符号区分词
    cursor.mergeCharFormat(format);//把参数format所表示的格式应用到光标所在处的字符上
    showwidget->text->mergeCurrentCharFormat(format);//显示出来
}


void ImgProcessor::ShowSizeSpinBox(QString spinValue)//设置大小
{
    QTextCharFormat fmt;
    fmt.setFontPointSize(spinValue.toFloat());//设置字体大小给spinvalue变量并转成浮点数
    showwidget->text->mergeCurrentCharFormat(fmt);//调用QTextEdit的mergeCurrentCharFormat函数将格式应用到选区内的所有字符上
}

/*************/

void ImgProcessor::ShowBoldBtn()//加粗
{
    QTextCharFormat fmt;
    fmt.setFontWeight(boldBtn->isChecked()?QFont::Bold:QFont::Normal);

    showwidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowItalicBtn()//倾斜
{
    QTextCharFormat fmt;
    fmt.setFontItalic(italicBtn->isChecked());
    showwidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowUnderlineBtn()//下划线
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(underlineBtn->isChecked());
    showwidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowColorBtn()//颜色
{
    QColor color= QColorDialog::getColor(Qt::white,this);//弹出标准颜色选择对话框,让用户选择颜色
                              //参数:(默认选中颜色,判断用户选中颜色是否有效【若未选择则返回false】)
    if(color.isValid())
    {
        QTextCharFormat fmt;//若有效,则重新设定光标区域为选中的颜色
        fmt.setForeground(color);
        showwidget->text->mergeCurrentCharFormat(fmt);//(mergeCurrentCharFormat合并当前字符格式)
                                                      // 调用窗口对象showwidget完成指定字符格式显示
    }
}

void ImgProcessor::ShowList()//文本排序
{

}


void ImgProcessor::ShowAlignment(QAction *act)//实现段落对齐
{
//    if(act==leftAction)
//        showwidget->text->setAlignment(Qt::AlignLeft);
//    if(act==rightAction)
//        showwidget->text->setAlignment(Qt::AlignRight);
//    if(act==centerAction)
//        showwidget->text->setAlignment(Qt::AlignCenter);
//    if(act==justifyAction)
//        showwidget->text->setAlignment(Qt::AlignJustify);
}


void ImgProcessor::ShowCursorPositionChanged()//相应光标处文本对齐的情况,相应的在状态栏相应位置显示
{
//    if(showwidget->text->alignment()==Qt::AlignLeft)//alignment 对齐
//        leftAction->setChecked(true);
//    if(showwidget->text->alignment()==Qt::AlignRight)//alignment 对齐
//        rightAction->setChecked(true);
//    if(showwidget->text->alignment()==Qt::AlignCenter)//alignment 对齐
//        centerAction->setChecked(true);
//    if(showwidget->text->alignment()==Qt::AlignJustify)//alignment 对齐
//        justifyAction->setChecked(true);

}


/**************************字符格式设置*************************************/

void ImgProcessor::ShowCurrentFormatChanged(const QTextCharFormat &fmt)//字符格式
//当光标所在处的字符格式发生变化的时候调用这个槽函数,函数根据新的字符格式将工具栏上各个格式控件显示更新
{
   fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));
   sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));
   boldBtn->setChecked(fmt.font().bold());
   italicBtn->setChecked(fmt.fontItalic());
   underlineBtn->setChecked(fmt.fontUnderline());
}



main.cpp文件

#include "imgprocessor.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ImgProcessor w;
    w.show();

    return a.exec();
}

showwidget.cpp文件

#include "showwidget.h"
#include <QHBoxLayout>
ShowWidget::ShowWidget(QWidget *parent) :
    QWidget(parent)
{
    imageLabel= new QLabel;
    imageLabel->setScaledContents(false);//设置自适应显示

    text=new QTextEdit;
    QHBoxLayout *mainLayout= new QHBoxLayout(this);
    mainLayout->addWidget(imageLabel);
    mainLayout->addWidget(text);

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值