首页 > 其他分享 >QT:简易文本编辑器

QT:简易文本编辑器

时间:2024-11-07 22:21:09浏览次数:1  
标签:showWidget 文本编辑 QT ImgProcessor tr void QAction 简易 new

 

 

效果

 

main.cpp 代码:

#include "imgprocessor.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFont f("ZYSong18030",12);//设置显示字段格式
    a.setFont(f);
    ImgProcessor w;
    w.show();
    return a.exec();
}

  

imgprocessor.h 代码:

#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H

#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QFontComboBox>
#include <QToolBar>
#include <QToolButton>
#include <QTextCharFormat>
#include <QTextStream>
#include <QPrinter>
#include <QPrintDialog>
#include <QTextDocument>
#include <QPainter>

#include "showwidget.h"

class ImgProcessor : public QMainWindow
{
    Q_OBJECT

public:
    ImgProcessor(QWidget *parent = nullptr);
    ~ImgProcessor();
    void createActions();//创建动作
    void createMenus();//创建菜单
    void createToolBars();//创建工具栏
    void loadFile(QString filename);
    void mergeFormat(QTextCharFormat format);

protected slots:
    void ShowNewFile(); // 新建文件
    void ShowOpenFile(); // 打开文件
    void ShowPrintText();// 打印文件
    void ShowPrintImage(); //图像打印
    void ShowZoomIn(); //放大
    void ShowZoomOut(); // 缩小
    void ShowRotate90(); //旋转90度
    void ShowRotate180();//旋转180度
    void ShowRotate270();//旋转270度
    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);

     /*  排版功能 */
    void ShowList(int );
    void ShowAlignment(QAction *act);
    void ShowCursorPositionChanged();

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;
    /*  文本相关工具 */
    QLabel *fontLabel1;
    QFontComboBox *fontComboBox;
    QLabel *fontLabel2;
    QComboBox *sizeComboBox;
    QToolButton *boldBtn;
    QToolButton *italicBtn;
    QToolButton *underlineBtn;
    QToolButton *colorBtn;
    QToolBar *fontToolBar; // 字体工具栏

    /*  排版功能 */
    QLabel *listLabel;  // 排序设置项
    QComboBox *listComboBox;
    QActionGroup *actGrp;
    QAction *leftAction;
    QAction *rightAction;
    QAction *centerAction;
    QAction *justifyAction;
    QToolBar *listToolBar; // 排版工具
};
#endif // IMGPROCESSOR_H

  

imgprocessor.cpp 代码:

#include "imgprocessor.h"
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QPrinter>
#include <QColor>
#include <QColorDialog>
#include <QTextBlock>
#include <QTextList>

ImgProcessor::ImgProcessor(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Easy Word")); //设置窗体标题
    showWidget = new ShowWidget(this);
    setCentralWidget(showWidget);

    // 在工具栏上嵌入控件
    //设置字体  p143
    fontLabel1 = new QLabel(tr("字体:"));
    fontComboBox = new QFontComboBox;
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
    fontLabel2 = new QLabel(tr("字号:"));
    sizeComboBox = new QComboBox;
    QFontDatabase db;
    foreach(int size ,db.standardSizes())
        sizeComboBox->addItem(QString::number(size));
    boldBtn = new QToolButton;
    boldBtn->setIcon(QIcon("./images/bold.png"));
    boldBtn->setCheckable(true);
    italicBtn = new QToolButton;
    italicBtn->setIcon(QIcon("./images/italic.png"));
    italicBtn->setCheckable(true);
    underlineBtn = new QToolButton;
    underlineBtn->setIcon(QIcon("./images/underline.png"));
    underlineBtn->setCheckable(true);
    colorBtn = new QToolButton;
    colorBtn->setIcon(QIcon("./images/color.png"));
    colorBtn->setCheckable(true);

     /*  排版功能 */
    listLabel = new QLabel(tr("排序"));
    listComboBox = new QComboBox;
    listComboBox->addItem("QTextListFormat::ListDisc");
    listComboBox->addItem("QTextListFormat::ListCircle");
    listComboBox->addItem("QTextListFormat::ListSquare");
    listComboBox->addItem("QTextListFormat::ListDecimal");
    listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    listComboBox->addItem("QTextListFormat::ListUpperAlpa");
    listComboBox->addItem("QTextListFormat::ListLowerRoman");
    listComboBox->addItem("QTextListFormat::ListUpperRoman");


    /*创建动作、菜单、工具栏的函数*/
    createActions();
    createMenus();
    createToolBars();
    if(img.load("image.png")){
        // 在imageLabel对象中放置图片
        showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    }

    //新建文件
    connect(NewfileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));
    connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));
    connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText()));
    connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage()));
    connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut()));
    connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn()));

    connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90()));
    connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180()));
    connect(rotate270Action,SIGNAL(triggered()),this,SLOT(ShowRotate270()));

    connect(mirrorHorizontalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorHorizontal()));
    connect(mirrorVerticalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorVertical()));

    /* 事件关联 */
    connect(fontComboBox, SIGNAL(activated(QString)),this,SLOT(ShowFontComboBox(QString)));
    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, SIGNAL(currentChartFormatChanged(QTextCharFormat&)),this,SLOT(ShowCurrentFormatChanged(QTextCharFormat&)));

    /*  排版功能 */
    connect(listComboBox, SIGNAL(activated(int)),this, SLOT(ShowList(int)));
    connect(showWidget->text->document(), SIGNAL(undoAvailable(bool)),redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text->document(), SIGNAL(redoAvailable(bool)),redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text, SIGNAL(cursorPositionChanged()),this, SLOT(ShowCursorPositionChanged()));
}

ImgProcessor::~ImgProcessor()
{
}

void ImgProcessor::createActions()
{
    //“打开” 动作
    openFileAction = new QAction(QIcon("./images/open.png"),tr("打开"),this);
    openFileAction->setShortcut(tr("Ctrl+O"));
    openFileAction->setStatusTip(tr("打开一个文件"));

    NewfileAction = new QAction(QIcon("./images/new.png"),tr("新建"),this);
    NewfileAction->setShortcut(tr("Ctrl+N"));
    NewfileAction->setStatusTip(tr("新建一个文件"));

    exitAction= new QAction(QIcon("./images/exit.png"),tr("退出"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("退出程序"));
    connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));

    copyAction = new QAction(QIcon("./images/copy.png"),tr("复制"),this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("复制文件"));
    connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy()));

    cutAction = new QAction(QIcon("./images/cut.png"),tr("剪切"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切文件"));
    connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut()));

    pasteAction = new QAction(QIcon("./images/paste.png"),tr("粘贴"),this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("粘贴文件"));
    connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste()));
    // ”“关于”“动作
    aboutAction = new QAction(QIcon("./images/about.png"),tr("关于"),this);
    connect(aboutAction, SIGNAL(triggered()),this, SLOT(QApplication::aboutQt()));

    //打印文件 动作
    PrintTextAction = new QAction(QIcon("./images/printText.png"),tr("打印文本"),this);
    PrintTextAction->setStatusTip(tr("打印一个文本"));

    //打印图片 动作
    PrintImageAction = new QAction(QIcon("./images/printImage.png"),tr("打印图片"),this);
    PrintImageAction->setStatusTip(tr("打印一幅图片"));

    zoomInAction = new QAction(QIcon("./images/zoomIn.png"),tr("放大"),this);
    zoomInAction->setStatusTip(tr("放大一幅图片"));

    zoomOutAction = new QAction(QIcon("./images/zoomOut.png"),tr("缩小"),this);
    zoomOutAction->setStatusTip(tr("缩小一幅图片"));

    rotate90Action = new QAction(QIcon("./images/rotate90.png"),tr("旋转90度"),this);
    rotate90Action->setStatusTip(tr("将一幅图片旋转90"));

    rotate180Action = new QAction(QIcon("./images/rotate180.png"),tr("旋转180度"),this);
    rotate180Action->setStatusTip(tr("将一幅图片旋转180"));

    rotate270Action = new QAction(QIcon("./images/rotate270.png"),tr("旋转270度"),this);
    rotate270Action->setStatusTip(tr("将一幅图片旋转270"));

    mirrorVerticalAction = new QAction(QIcon("./images/mirrorVertical.png"),tr("纵向镜像"),this);
    mirrorVerticalAction->setStatusTip(tr("对一幅图做纵向镜像"));

    mirrorHorizontalAction = new QAction(QIcon("./images/mirrorHorizontal.png"),tr("横向镜像"),this);
    mirrorHorizontalAction->setStatusTip(tr("对一幅图做横向镜像"));

    undoAction = new QAction(QIcon("./images/undo.png"),tr("撤销"),this);
    connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));

    redoAction = new QAction(QIcon("./images/redo.png"),tr("重做"),this);
    connect(redoAction,SIGNAL(triggered()),showWidget->text,SLOT(redo()));



    /*  排序: 左对齐、右对齐、居中和两端对齐 */
    actGrp = new QActionGroup(this);
    leftAction = new QAction(QIcon("./images/left.png"),"左对齐",actGrp);
    leftAction->setCheckable(true);
    rightAction = new QAction(QIcon("./images/right.png"),"右对齐",actGrp);
    rightAction->setCheckable(true);
    centerAction = new QAction(QIcon("./images/center.png"),"居中",actGrp);
    centerAction->setCheckable(true);
    justifyAction = new QAction(QIcon("./images/justify.png"),"两端对齐",actGrp);
    justifyAction->setCheckable(true);
    connect(actGrp,SIGNAL(triggered(QAction*)),this, SLOT(ShowAlignment(QAction*)));

}

void ImgProcessor::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(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("镜像");
    mirrorMenu->addAction(mirrorVerticalAction);
    mirrorMenu->addAction(mirrorHorizontalAction);

}

void ImgProcessor::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(pasteAction);
    zoomTool->addSeparator();

    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction);
    //旋转工具条
    rotateTool =  addToolBar("rotate");
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action);
    //撤销和重做工具条
    doToolBar = addToolBar("doEdit");
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction);

    //字体工具条
    fontToolBar = addToolBar("Font");
    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("list");
    listToolBar->addWidget(listLabel);
    listToolBar->addWidget(listComboBox);
    listToolBar->addSeparator();
    listToolBar->addActions(actGrp->actions());
}

void ImgProcessor::loadFile(QString filename)
{// 读取文件内容功能
    printf("file Name:%s\n", (char*)filename.data());
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QTextStream textStream(&file);
        while(!textStream.atEnd()){
            showWidget->text->append(textStream.readLine());
            printf("read line\n");
        }
        printf("end\n");
    }
}


void ImgProcessor::ShowNewFile()
{// 新建文件功能
    ImgProcessor *newImgProcessor = new ImgProcessor;
    newImgProcessor->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::ShowPrintText()
{// 实现打印文本功能
    QPrinter printer;
    QPrintDialog printDialog(&printer,this);
    if(printDialog.exec()){
        // 获得QTextEdit对象的文档
        QTextDocument *doc = showWidget->text->document();
        doc->print(&printer);
    }
}

void ImgProcessor::ShowPrintImage()
{// 实现打印图像功能
    QPrinter printer; //新建一个QPainter对象
    QPrintDialog printDialog(&printer, this);
    if(printDialog.exec()){
        QPainter painter(&printer);
        QRect rect = painter.viewport();// 获得QPainter对象的视图矩形区域
        QSize size =  img.size();
        /* 按照图形的比例大小重新设置视图矩形区域**/
        size.scale(rect.size(), Qt::KeepAspectRatio);
        painter.setWindow(img.rect()); // 设置QPainter窗口大小为图像的大小
        painter.drawImage(0,0,img);// 打印图像
    }
}

void ImgProcessor::ShowZoomIn()
{// 实现图形放大功能
    if(img.isNull()){
        return;
    }
    QMatrix matrix;
    matrix.scale(2,2); //按照2倍比例对水平和垂直方向进行放大
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowZoomOut()
{// 实现图形缩小功能
    if(img.isNull()){
        return;
    }
    QMatrix matrix;
    matrix.scale(0.5,0.5);//按照0.5倍比例对水平和垂直方向进行缩水
    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;
    img = img.mirrored(true,false);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowFontComboBox(QString comboStr)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(comboStr);
    mergeFormat(fmt);
}

void ImgProcessor::ShowSizeSpinBox(QString spinValue)
{
    QTextCharFormat fmt;
    fmt.setFontPointSize(spinValue.toFloat());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

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::red,this);
    if(color.isValid()){
        QTextCharFormat fmt;
        fmt.setForeground(color);
        showWidget->text->mergeCurrentCharFormat(fmt);
    }
}

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());
}

void ImgProcessor::ShowList(int index)
{
    // 排序
    QTextCursor cursor = showWidget->text->textCursor();
    if(index!=0){
        QTextListFormat::Style style = QTextListFormat::ListDisc;
        switch(index){
        case 1:
            style = QTextListFormat::ListDisc;
            break;
        case 2:
            style = QTextListFormat::ListCircle;
            break;
        case 3:
            style = QTextListFormat::ListSquare;
            break;
        case 4:
            style = QTextListFormat::ListDecimal;
            break;
        case 5:
            style = QTextListFormat::ListLowerAlpha;
            break;
        case 6:
            style = QTextListFormat::ListUpperAlpha;
            break;
        case 7:
            style = QTextListFormat::ListLowerRoman;
            break;
        case 8:
            style = QTextListFormat::ListUpperRoman;
            break;
        }
        /*  设置缩进值 */
        cursor.beginEditBlock();
        QTextBlockFormat blockFmt = cursor.blockFormat();
        QTextListFormat listFmt;
        if(cursor.currentList()){
            listFmt = cursor.currentList()->format();
        }
        else{
            listFmt.setIndent(blockFmt.indent()+1);
            blockFmt.setIndent(0);
            cursor.setBlockFormat(blockFmt);
        }
        listFmt.setStyle(style);
        cursor.createList(listFmt);
        cursor.endEditBlock();
    }
    else{
        QTextBlockFormat bfmt;
        bfmt.setObjectIndex(-1);
        cursor.mergeBlockFormat(bfmt);
    }
}

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)
        leftAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignRight)
        rightAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignCenter)
        centerAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignJustify)
        justifyAction->setChecked(true);
}

void ImgProcessor::mergeFormat(QTextCharFormat format)
{
    QTextCursor cursor = showWidget->text->textCursor();
    if(!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    showWidget->text->mergeCurrentCharFormat(format);
}

  

showwidget.h 代码:

#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

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

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

signals:
public slots:
};

#endif // SHOWWIDGET_H

  

showwidget.cpp代码

#include "showwidget.h"

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);
}

  在Qt5中,主窗体(MainWindow)是应用程序的核心组件,它通常承载着应用的主要功能和界面元素。在这个"文本编辑器"案例中,我们将深入探讨如何利用Qt5库来创建一个功能丰富的文本编辑器。Qt是一个跨平台的C++图形用户界面应用程序框架,提供了丰富的API来处理UI设计、事件处理以及与其他系统交互等功能。

标签:showWidget,文本编辑,QT,ImgProcessor,tr,void,QAction,简易,new
From: https://www.cnblogs.com/samrv/p/18534118

相关文章

  • 使用C++和QT开发应用程序入门以及开发实例分享
    目录1、搭建开发环境(VS2010和QT4.8.2)2、创建一个QT窗口3、在QT窗口中添加子窗口4、QT界面布局5、QT信号(SIGNAL)和槽(SLOT)6、最后C++软件异常排查从入门到精通系列教程(专栏文章列表,欢迎订阅,持续更新...)https://blog.csdn.net/chenlycly/article/details/125529931C/C++实......
  • 能粘贴WORD 内容(含图片)的文本编辑器
    编辑器:百度ueditor前端:vue2,vue3,vue-cli,html5需求:复制粘贴word内容图片,word图片转存交互,导入pdf,导入PowerPoint(PPT)要求:开源,免费,技术支持用户体验:Ctrl+V快捷键操作该说不说,最近这块应该也是挻火的,今天早上又有网友加我微信私聊,说是想了解一下这块的技术和方案。实......
  • 毕设拯救计划(二)基于QT的智能家居(Onenet云)
    文章目录前言一、效果展示二、设计思路2.1Mqtt的实现2.2音乐播放器的实现2.3虚拟键盘三、问题杂谈免责声明前言  前段时间,笔者觉得以前的STM32的智能家居太low了,于是想对其进行改进,目前的方案有以下两种:一、STM32和Linux开发板构成完整的智能车家系统,即通过MQ......
  • 基于QT的桌面软件,就是要比winform、wpf体验好。
    QT具有跨平台性强、可定制程度高等优点,能在多种操作系统上运行,并且对于开发者来说提供了丰富的功能库。然而,WinForm开发相对简单快捷,适合快速构建小型应用。WPF则在界面设计和动画效果方面表现出色,能创造出非常美观的用户界面。不同的开发场景和需求会决定哪种技术更合适......
  • QT creator 中c和c++混编问题
    今天在编译包含.c和.cpp的QT项目,在整合各种代码的时候,碰到一些问题,为了方便后查,初步总结如下:1.新版QT中一般使用g++编译cpp文件和c文件,可以在项目中同时使用c文件和cpp文件。后缀cpp文件g++自动识别为c++代码自动进行c++编译,后缀c文件自动识别为c代码进行c编译。这个时候必须注意......
  • 《古剑奇谭网络版》qt5widgets.dll文件丢失全方位解决方法大全
    《古剑奇谭网络版》是一款大型多人在线角色扮演游戏。如果在运行该游戏时遇到qt5widgets.dll文件丢失的错误提示,这通常意味着你的系统缺少了Qt框架中的一个必要组件。Qt是一个跨平台的应用程序开发框架,广泛用于图形用户界面(GUI)的创建。要解决qt5widgets.dll文件丢失的问题,你可......
  • Qt入门基础分享
    文章目录学习Qt语言之前的基本知识1.编程基础语法:面向对象编程(OOP):基本数据结构:了解数组、链表、栈、队列、树(如二叉树、平衡树)、图(如邻接矩阵、邻接表)等。算法:熟悉常见的排序算法(如快速排序、归并排序、冒泡排序)和查找算法(如线性查找、二分查找)。复杂度......
  • 简易CPU设计入门:译码模块(一)
    项目代码下载还是请大家首先准备好本项目所用的源代码。如果已经下载了,那就不用重复下载了。如果还没有下载,那么,请大家点击下方链接,来了解下载本项目的CPU源代码的方法。下载本项目代码准备好了项目源代码以后,我们接着去讲解。本节前言有一段时间没写本专栏的教程了。在之......
  • Qt多线程- QThread 创建多线程程序
    QThread创建多线程程序QThread类功能简介今天说一下Qt中的多线程。QThread类不依赖平台的管理线程的方法。一个QThread类的对象管理一个线程,一般从QThread继承一个自定义类,并重新定义虚函数run(),在run()函数里实现线程需要完成的任务。将应用程序的线程称为......
  • Qt Event事件系统小探1
    目录QtEventSystemFromqt.doc如何传递事件事件类型事件处理程序事件过滤器发送事件事件的产生和派发处理我们的事件来一段好玩的代码扩展:QWidget如何处理我们的事件?扩展2:实现一个变色的LabelQtEventSystemFromqt.doc在Qt中,事件是从抽象QEvent类派......