第30课 Qt中的文本编辑组件

1. 3种常用的文本编辑组件的比较

单行文本支持

多行文本支持

自定义格式支持

富文本支持

QLineEdit

(单行文本编辑组件)

Yes

No

No

No

QPlainTextEdit

(多行普通文本编辑组件)

Yes

Yes

No

No

QTextEdit

(多行富文本编辑组件)

Yes

Yes

Yes

Yes

2. Qt中常用文本编辑组件的继承层次图

第30课 Qt中的文本编辑组件

3. 常用文本编辑组件的内置功能

(1)右键弹出式菜单

(2)快捷键功能:复制、粘贴、剪切等

【编程实验】文本编辑组件的对比实验

第30课 Qt中的文本编辑组件

//main.cpp

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

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

    return a.exec();
}

//MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QTextEdit>

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QLineEdit lineEdit;   //单行文本
    QPlainTextEdit plainEdit;//多行普通文本
    QTextEdit    textEdit;   //多行富文本

public:
    MainWindow(QWidget *parent = );
    ~MainWindow();
};

#endif // MAINWINDOW_H

//MainWindow.cpp

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),
    lineEdit(this), plainEdit(this), textEdit(this)
{
    resize(, );

    //单行文本编辑组件
    lineEdit.move(, );
    lineEdit.resize(,);
    lineEdit.insert("QLineEdit");
    lineEdit.insert("\n");   //不支持换行
    lineEdit.insert("<img src=\"E:\\QT\\30-1\\D.T.png\" />");

    //多行普通文本编辑组件
    plainEdit.move(, );
    plainEdit.resize(, );
    plainEdit.insertPlainText("QPlainTextEdit");
    plainEdit.insertPlainText("\n");   //支持换行
    plainEdit.insertPlainText("<img src=\"E:\\QT\\30-1\\D.T.png\" />");

    //多行富文本编辑组件
    textEdit.move(, );
    textEdit.resize(, );
    textEdit.insertPlainText("QTextEdit");
    //textEdit.insertPlainText("\n");   //支持换行
    textEdit.insertHtml("<br>");      //支持富文本式的换行
    textEdit.insertHtml("<img src=\"E:\\QT\\30-1\\D.T.png\" />");
}

MainWindow::~MainWindow()
{
}

【编程实验】文本编辑组件实战

//Notepad.pro、main.cpp、res.qrc与第29课相同

//MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenuBar>
//#include <QKeySequence>
//#include <QAction>
#include <QPlainTextEdit>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT
private:

    QPlainTextEdit mainEditor;
    QLabel statusLbl;

    //将构造函数、复制构造、赋值函数等私有化
    MainWindow(QWidget *parent = );
    MainWindow(const MainWindow&);
    MainWindow& operator= (const MainWindow&);

    bool construct(); //二阶构造模式

    bool initMenuBar();   //初始化菜单栏
    bool initToolBar();   //初始化工具栏
    bool initStatusBar(); //初始化状态栏
    bool initMainEditor();//初始化文本编辑组件

    //菜单设置
    bool initFileMenu(QMenuBar* mb);    //“文件”菜单
    bool initEditMenu(QMenuBar* mb);    //“编辑”菜单
    bool initFormatMenu(QMenuBar* mb);  //“格式”菜单
    bool initViewMenu(QMenuBar* mb);    //“查看”菜单
    bool initHelpMenu(QMenuBar* mb);    //“帮助”菜单

    //工具栏设置
    bool initFileToolItem(QToolBar* tb);
    bool initEditToolItem(QToolBar* tb);
    bool initFormatToolItem(QToolBar* tb);
    bool initViewToolItem(QToolBar* tb);

    //生成菜单项
    bool makeAction(QAction*& action, QString text, int key);
    //生成工具栏中的各按钮
    bool makeAction(QAction*& action, QString tip, QString icon);

public:
    static MainWindow* NewInstance();

    ~MainWindow();
};

#endif // MAINWINDOW_H

//MainWindow.cpp

#include "MainWindow.h"
#include <QMenu>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}

//二阶构造中的第2阶构造
bool MainWindow::construct()
{
    bool ret = true;

    ret = ret && initMenuBar();
    ret = ret && initToolBar();
    ret = ret && initStatusBar();
    ret = ret && initMainEditor();

    return ret;
}

MainWindow* MainWindow::NewInstance()
{
    MainWindow* ret = new MainWindow();

    if((ret == NULL) || !ret->construct())
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

//初始化菜单栏
bool MainWindow::initMenuBar()
{
    bool ret = true;
    QMenuBar* mb = menuBar(); //menuBar为QMainWindow的成员变量

    ret = ret && initFileMenu(mb);
    ret = ret && initEditMenu(mb);
    ret = ret && initFormatMenu(mb);
    ret = ret && initViewMenu(mb);
    ret = ret && initHelpMenu(mb);

    return ret;
}

//初始化工具栏
bool MainWindow::initToolBar()
{
    bool ret = true;

    QToolBar* tb = addToolBar("Tool Bar");

    tb->setIconSize(QSize(, ));

    ret = ret && initFileToolItem(tb);
    tb->addSeparator();

    ret = ret && initEditToolItem(tb);
    tb->addSeparator();

    ret = ret && initFormatToolItem(tb);
    tb->addSeparator();

    ret = ret && initViewToolItem(tb);

    return ret;
}

//初始化状态栏
bool MainWindow::initStatusBar()
{
    bool ret = true;
    QStatusBar* sb = statusBar();
    QLabel* label = new QLabel("D.T.Software");

    if( label != NULL )
    {
        statusLbl.setMinimumWidth();
        statusLbl.setAlignment(Qt::AlignCenter);
        statusLbl.setText("Ln: 1    Col: 1");

        label->setMinimumWidth();
        label->setAlignment(Qt::AlignCenter);

        sb->addPermanentWidget(new QLabel()); //添加分隔线
        sb->addPermanentWidget(&statusLbl);
        sb->addPermanentWidget(label);
    }
    else
    {
        ret = false;
    }
    return ret;
}

//初始化主文本编辑组件
bool MainWindow::initMainEditor()
{
    bool ret = true;

    mainEditor.setParent(this);
    setCentralWidget(&mainEditor);

    return ret;
}

//文件(下拉)菜单
bool MainWindow::initFileMenu(QMenuBar* mb)
{
    QMenu* menu = new QMenu("File(&F)", mb);//指定parent为mb
    bool ret = (menu != NULL);

    if ( ret )
    {
        QAction* action = NULL;
        //"新建"菜单项
        ret = ret &&  makeAction(action, "New(&N)", Qt::CTRL + Qt::Key_N);
        if(ret)
        {
            menu->addAction(action); //add Action to Menu Item
        }

        //"打开"菜单项
        ret = ret &&  makeAction(action, "Open(&O)...", Qt::CTRL + Qt::Key_O);
        if(ret)
        {
            menu->addAction(action); //add Action to Menu Item
        }

        //"保存"菜单项
        ret = ret &&  makeAction(action, "Save(&S)", Qt::CTRL + Qt::Key_S);
        if(ret)
        {
            menu->addAction(action); //add Action to Menu Item
        }

        //"另存为"菜单项
        ret = ret &&  makeAction(action, );
        if(ret)
        {
            menu->addAction(action); //add Action to Menu Item
        }

        //水平分隔线
        menu->addSeparator();

        //"打印"菜单项
        ret = ret &&  makeAction(action, "Print(&P)...", Qt::CTRL + Qt::Key_P);
        if(ret)
        {
            menu->addAction(action); //add Action to Menu Item
        }

        menu->addSeparator();

        //"退出"菜单项
        ret = ret && makeAction(action, "Exit(&X)", Qt::CTRL + Qt::Key_Q);
        if(ret)
        {
            menu->addAction(action); //add Action Item to Menu
        }
    }

    if ( ret )
    {
        mb->addMenu(menu); //add Menu to Menu Bar
    }
    else
    {
        delete menu;
    }

    return ret;
}

//“编辑”菜单
bool MainWindow::initEditMenu(QMenuBar* mb)
{
    QMenu* menu = new QMenu("Edit(&E)", mb);
    bool ret = (menu != NULL);

    if( ret )
    {
        QAction* action = NULL;

        ret = ret && makeAction(action, "Undo(&U)", Qt::CTRL + Qt::Key_Z);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Redo(&R)...", Qt::CTRL + Qt::Key_Y);
        if ( ret )
        {
            menu->addAction(action);
        }

        menu->addSeparator();

        ret = ret && makeAction(action, "Cut(&T)", Qt::CTRL + Qt::Key_X);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Copy(&C)", Qt::CTRL + Qt::Key_C);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Paste(&P)", Qt::CTRL + Qt::Key_V);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Delete(&L)", Qt::Key_Delete);
        if ( ret )
        {
            menu->addAction(action);
        }

        menu->addSeparator();

        ret = ret && makeAction(action, "Find(&F)...", Qt::CTRL + Qt::Key_F);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Replace(&R)...", Qt::CTRL + Qt::Key_H);
        if ( ret )
        {
            menu->addAction(action);
        }

        ret = ret && makeAction(action, "Goto(&G)...", Qt::CTRL + Qt::Key_G);
        if ( ret )
        {
            menu->addAction(action);
        }

        menu->addSeparator();

        ret = ret && makeAction(action, "Select All(&A)...", Qt::CTRL + Qt::Key_A);
        if ( ret )
        {
            menu->addAction(action);
        }
    }

    if ( ret )
    {
        mb->addMenu(menu);
    }
    else
    {
        delete menu;
    }

    return ret;
}

//“格式”菜单
bool MainWindow::initFormatMenu(QMenuBar* mb)
{
    QMenu* menu = new QMenu("Format(&O)", mb);
    bool ret = ( menu != NULL);

    if (ret)
    {
        QAction* action = NULL;

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }
    }

    if ( ret )
    {
        mb->addMenu(menu);
    }
    else
    {
        delete menu;
    }

    return ret;
}

//“查看”菜单
bool MainWindow::initViewMenu(QMenuBar* mb)
{
    QMenu* menu = new QMenu("View(&V)", mb);
    bool ret = ( menu != NULL);

    if (ret)
    {
        QAction* action = NULL;

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }
    }

    if ( ret )
    {
        mb->addMenu(menu);
    }
    else
    {
        delete menu;
    }

    return ret;
}

//“帮助”菜单
bool MainWindow::initHelpMenu(QMenuBar* mb)
{
    QMenu* menu = new QMenu("Help(&H)", mb);
    bool ret = ( menu != NULL);

    if (ret)
    {
        QAction* action = NULL;

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }

        ret = ret && makeAction(action, );
        if ( ret )
        {
            menu ->addAction(action);
        }
    }

    if ( ret )
    {
        mb->addMenu(menu);
    }
    else
    {
        delete menu;
    }

    return ret;
}

//工具栏设置
bool MainWindow::initFileToolItem(QToolBar* tb)
{
    bool ret = true;
    QAction* action = NULL;

    ret = ret && makeAction(action, "New", ":/Res/pic/new.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Open", ":/Res/pic/open.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Save", ":/Res/pic/save.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Save As", ":/Res/pic/saveas.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Print", ":/Res/pic/print.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    return ret;
}

bool MainWindow::initEditToolItem(QToolBar* tb)
{
    bool ret = true;
    QAction* action = NULL;

    ret = ret && makeAction(action, "Undo", ":/Res/pic/undo.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Redo", ":/Res/pic/redo.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Cut", ":/Res/pic/cut.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Copy", ":/Res/pic/copy.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Paste", ":/Res/pic/paste.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Find", ":/Res/pic/find.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Replace", ":/Res/pic/replace.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Goto", ":/Res/pic/goto.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    return ret;
}

bool MainWindow::initFormatToolItem(QToolBar* tb)
{
    bool ret = true;
    QAction* action = NULL;

    ret = ret && makeAction(action, "Auto Wrap", ":/Res/pic/wrap.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Font", ":/Res/pic/font.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    return ret;
}

bool MainWindow::initViewToolItem(QToolBar* tb)
{
    bool ret = true;
    QAction* action = NULL;

    ret = ret && makeAction(action, "Tool Bar", ":/Res/pic/tool.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    ret = ret && makeAction(action, "Status Bar", ":/Res/pic/status.png");
    if ( ret )
    {
        tb->addAction(action);
    }

    return ret;
}

//生成菜单项(第1个参数是菜单项对象,第2个参数为显示文本,第3个参数为快捷键)
bool MainWindow::makeAction(QAction*& action, QString text, int key)
{
    bool ret = true;

    action = new QAction(text, this); //设置parent为this

    if(action != NULL)
    {
        action->setShortcut(QKeySequence(key)); //设置快捷键
    }
    else
    {
        ret = false;
    }

    return ret;
}

//生成工具栏中的各按钮
bool MainWindow::makeAction(QAction*& action, QString tip, QString icon)
{
    bool ret = true;
    action = new QAction("", this);

    if( action != NULL )
    {
        action ->setToolTip(tip);
        action->setIcon(QIcon(icon));
    }
    else
    {
       ret = false;
    }

    return ret;
}

MainWindow::~MainWindow()
{

}

4. 小结

(1)Qt中提供了3种不同类型的文本编辑组件

(2)Qt中的文本编辑组件封装了常用的编辑功能

  ①QLineEdit用于单行简单文本输入的情形

  ②QPlainTextEdit用于多行简单文本输入的情形

  ③QTextEdit用于多行富文本输入的情形

上一篇:【bozj2287】【[POJ Challenge]消失之物】维护多值递推


下一篇:Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全