天天看點

Qt開發——文本編輯元件

作者:QT教程

1、Qt支援三種常用的文本編輯元件

(1)、QLineEdit:單行文本編輯元件

(2)、QTextEdit:多行富文本(有圖檔、視訊等)編輯元件

(3)、QPlainTextEdit:多行普通文本編輯元件

2、Qt中常用文本編輯元件的繼承層次圖

Qt開發——文本編輯元件

3、不同文本元件的特性比較

Qt開發——文本編輯元件

4、Qt中常用文本編輯元件内置功能

Qt開發——文本編輯元件

1、初探文本編輯元件

#include "MainWindow.h"

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

    lineEdit.move(20, 20);
    lineEdit.resize(560, 100);
    lineEdit.insert("QLineEdit");
    lineEdit.insert("\n");
    lineEdit.insert("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />");

    plainEdit.move(20, 130);
    plainEdit.resize(560, 130);
    plainEdit.insertPlainText("QPlainTextEdit");
    plainEdit.insertPlainText("\n");
    plainEdit.insertPlainText("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />");

    textEdit.move(20, 270);
    textEdit.resize(560, 130);
    textEdit.insertPlainText("QTextEdit");
    textEdit.insertPlainText("\n");
    textEdit.insertHtml("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />");
}

MainWindow::~MainWindow()
{
    
}           

2、NopePad:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QString>
#include <QtGui/QMainWindow>
#include <QToolBar>
#include <QIcon>
#include <QSize>
#include <QStatusBar>
#include <QLabel>
#include <QPlainTextEdit>
class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
        QPlainTextEdit mainEdit;
        QLabel statusLabel;
        MainWindow(QWidget *parent = 0);
        MainWindow(const MainWindow& obj);
        MainWindow* operator = (const MainWindow& obj);
        bool construct();

        bool initMenuBar();//菜單欄
        bool initToolBar();//工具欄
        bool initStatusBar();//狀态欄
        bool initinitMainEditor();//編輯視窗

        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 makeAction(QAction*& action, QString text, int ket);//菜單項
        bool makeAction(QAction*& action, QString tip, QString icon);
public:
     static MainWindow* NewInstance();
    ~MainWindow();
};

#endif // MAINWINDOW_H#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), statusLabel(this)
{
}

bool MainWindow::construct()
{
    bool ret = true;
    ret = ret && initMenuBar();
    ret = ret && initToolBar();
    ret = ret && initStatusBar();
    ret = ret && initinitMainEditor();
    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(),這是普通成員函數,不是構造函數

    ret = ret && initFileMenu(mb);//傳一個參數是為了在initFileMenu()函數将menu加入菜單欄
    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->setMovable(false);
    tb->setFloatable(false);
    tb->setIconSize(QSize(16,16));

    ret = ret && initFileToolItem(tb);

    return ret;
}

bool MainWindow::initStatusBar()//狀态欄
{
    bool ret = true;

    QStatusBar* sb = statusBar();

    QLabel* label = new QLabel("Made By LGC");

    if(label != NULL)
    {
        statusLabel.setMinimumWidth(200);
        statusLabel.setAlignment(Qt::AlignHCenter);
        statusLabel.setText("Ln:1    Col:1");


        label->setMinimumWidth(200);
        label->setAlignment(Qt::AlignHCenter);

        sb->addPermanentWidget(new QLabel());//單純加入分隔符
        sb->addPermanentWidget(&statusLabel);
        sb->addPermanentWidget(label);
    }
    else
    {
        ret = false;
    }
    return ret;
}
bool MainWindow::initinitMainEditor()//編輯視窗
{
    bool ret = true;

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

    return ret;
}

/************************************************檔案菜單********************************************************/
bool MainWindow::initFileMenu(QMenuBar* mb)
{
    bool ret = true;

    QMenu* menu = new QMenu("File(&F)");//建立檔案菜單,(&F)是為了可以Alt+F打開
    ret = (menu != NULL);
    if(ret)
    {
        QAction* action = NULL;

        //New
        ret = ret &&  makeAction(action, "New(&N)",Qt::CTRL + Qt::Key_N);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Open
        ret = ret &&  makeAction(action, "Open(&O)...",Qt::CTRL + Qt::Key_O);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Save
        ret = ret &&  makeAction(action, "Save(&S)",Qt::CTRL + Qt::Key_S);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Save As
        ret = ret &&  makeAction(action, "Save As(&A)...",0);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //print
        ret = ret &&  makeAction(action, "Print(&P)...",Qt::CTRL + Qt::Key_P);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Exit
        ret = ret &&  makeAction(action, "Exit(&X)",0);
        if(ret)
        {
           menu->addAction(action);//将菜單項加入到菜單
        }

    }
    if(ret)
    {
        mb->addMenu(menu);//将菜單加入到菜單欄
    }
    else
    {
        delete mb;
    }
    return ret;
}

/************************************************編輯菜單********************************************************/
bool MainWindow::initEditMenu(QMenuBar* mb)
{
    bool ret = true;

    QMenu* menu = new QMenu("Edit(&E)");
    ret = (menu != NULL);
    if(ret)
    {
        QAction* action = NULL;

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

        menu->addSeparator();

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

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

/************************************************格式菜單********************************************************/
bool MainWindow::initFormatMenu(QMenuBar* mb)
{
    bool ret = true;

    QMenu* menu = new QMenu("Format(&O)");
    ret = (menu != NULL);
    if(ret)
    {
        QAction* action = NULL;

        //Auto Wrap
        ret = ret &&  makeAction(action, "Auto Wrap(&W)",0);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Font
        ret = ret &&  makeAction(action, "Font(&F)...",0);
        if(ret)
        {
           menu->addAction(action);
        }

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

/************************************************視圖菜單********************************************************/
bool MainWindow::initViewMenu(QMenuBar* mb)
{
    bool ret = true;

    QMenu* menu = new QMenu("View(&V)");
    ret = (menu != NULL);
    if(ret)
    {
        QAction* action = NULL;

        //Tool Bar
        ret = ret &&  makeAction(action, "Tool Bar(&T)",0);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //Status Bar
        ret = ret &&  makeAction(action, "Status Bar(&S)",0);
        if(ret)
        {
           menu->addAction(action);
        }

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

/************************************************幫助菜單********************************************************/
bool MainWindow::initHelpMenu(QMenuBar* mb)
{
    bool ret = true;

    QMenu* menu = new QMenu("Help(&H)");
    ret = (menu != NULL);
    if(ret)
    {
        QAction* action = NULL;

        //User Manual
        ret = ret &&  makeAction(action, "User Manual",0);
        if(ret)
        {
           menu->addAction(action);
        }

        menu->addSeparator();

        //About NotePad
        ret = ret &&  makeAction(action, "About NotePad...",0);
        if(ret)
        {
           menu->addAction(action);
        }

    }
    if(ret)
    {
        mb->addMenu(menu);
    }
    else
    {
        delete mb;
    }
    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);
    }
    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);
    }

    return ret;

}

bool MainWindow::makeAction(QAction*& action, QString text, int key)//菜單項
{
    bool ret = true;
    action = new QAction(text, NULL);
    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("", NULL);
    if(action != NULL)
    {
        action->setToolTip(tip);
        action->setIcon(QIcon(icon));
    }
    else
    {
        ret = false;
    }
    return ret;
}
MainWindow::~MainWindow()
{
    
}#include <QtGui/QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow* w = MainWindow::NewInstance();
    int ret = -1;
    if(w != NULL)
    {
        w->show();
        ret = a.exec();
    }

    delete w;
    return ret;
}           

二、小結

(1)、Qt支援三種常用的文本編輯元件

(2)、Qt中的文本編輯元件封裝了常用的編輯功能

A、QLineEdit:單行文本編輯元件

B、QTextEdit:多行富文本編輯元件

C、QPlainTextEdit:多行普通文本編輯元件

【領QT開發教程學習資料,點選下方連結莬費領取↓↓,先碼住不迷路~】

「連結」

繼續閱讀