设置文本排序及对齐

来源:互联网 发布:北京数据 编辑:程序博客网 时间:2024/06/09 21:46

设置文本排序及对齐

在编写包含格式设置的文本编辑程序时,经常用到的Qt类有:QTextEdit、QTextDocument、QTextCharFormat、QTextCursor、QTextBlock、QTextList、QTextFrame、QTextTable、QTextBlockFormat、QTextListFormat、QTextFrameFormat、QTextTableFormat等。

首先,任何一个文本编辑的程序都要用到QTextEdit作为输入文本的容器,在它里面输入的可编辑文本由QTextDocument作为载体,而QTextDocument的不同表现形式,可能为字符串、段落、列表、表格或是图片等。每种元素都有自己的格式,这些格式则用QTextCharFormat、QTextBlockFormat、QTextListFormat、QTextFrameFormat等类来描述与实现。

这里写图片描述


1、创建listalign.h

#ifndef LISTALIGN_H#define LISTALIGN_H#include <QtGui>class ListAligh : public QMainWindow{    Q_OBJECTpublic:    ListAligh(QWidget *parent = 0);public slots:    void slotAlignment(QAction *);    void slotList(int);    void slotCursorPositionChanged();private:    QTextEdit *text;    QLabel *label;    QComboBox *listBox;    QAction *leftAction;    QAction *rightAction;    QAction *centerAction;    QAction *justifyAction;    QAction *redoAction;    QAction *undoAction;};#endif // LISTALIGN_H

2、创建listalign.cpp文件

#include "listalign.h"ListAligh::ListAligh(QWidget *parent) : QMainWindow(parent){    setWindowTitle(tr("List&Alignment"));    QToolBar *toolBar = addToolBar("list");    label = new QLabel(tr("List: "));    listBox = new QComboBox(toolBar);    listBox->addItem(tr("Standard"));    listBox->addItem(tr("Bullet List (Disc)"));    listBox->addItem(tr("Bullet List (Circle)"));    listBox->addItem(tr("Bullet List (Square)"));    listBox->addItem(tr("Ordered List (Decimal)"));    listBox->addItem(tr("Ordered List (Alpha lower)"));    listBox->addItem(tr("Ordered List (Alpha upper)"));    toolBar->addWidget(label);    toolBar->addWidget(listBox);    toolBar->addSeparator();    QActionGroup *actGrp = new QActionGroup(this);    leftAction = new QAction(QIcon(":/images/left.png"),tr("left"),actGrp);    leftAction->setCheckable(true);    centerAction = new QAction(QIcon(":/images/center.png"),tr("center"),actGrp);    centerAction->setCheckable(true);    justifyAction = new QAction(QIcon(":/images/justify.png"),tr("justify"),actGrp);    justifyAction->setCheckable(true);    rightAction = new QAction(QIcon(":/images/right.png"),tr("right"),actGrp);    rightAction->setCheckable(true);    toolBar->addActions(actGrp->actions());    QToolBar *editBar = addToolBar("Edit");    undoAction = new QAction(QIcon(":/images/undo.png"),tr("undo"),this);    editBar->addAction(undoAction);    redoAction = new QAction(QIcon(":/images/redo.png"),tr("redo"),this);    editBar->addAction(redoAction);    text =  new QTextEdit(this);    text->setFocus();    setCentralWidget(text);    connect(listBox,SIGNAL(activated(int)),this,SLOT(slotList(int)));    connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(slotAlignment(QAction*)));    connect(redoAction,SIGNAL(triggered()),text,SLOT(redo()));    connect(undoAction,SIGNAL(triggered()),text,SLOT(undo()));    connect(text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));    connect(text->document(),SIGNAL(undoAvailable(bool)),undoAction,SLOT(setEnabled(bool)));    connect(text,SIGNAL(cursorPositionChanged()),this,SLOT(slotCursorPositionChanged()));}void ListAligh::slotAlignment(QAction *act){    if (act == leftAction)        text->setAlignment(Qt::AlignLeft);    if (act == centerAction)        text->setAlignment(Qt::AlignCenter);    if (act == justifyAction)        text->setAlignment(Qt::AlignJustify);    if (act == rightAction)        text->setAlignment(Qt::AlignRight);}void ListAligh::slotList(int index){    QTextCursor cursor = 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;             default:                    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();    }}void ListAligh::slotCursorPositionChanged(){    if (text->alignment() == Qt::AlignLeft)        leftAction->setChecked(true);    if (text->alignment() == Qt::AlignCenter)        centerAction->setChecked(true);    if (text->alignment() == Qt::AlignJustify)        justifyAction->setChecked(true);    if (text->alignment() == Qt::AlignRight)        rightAction->setChecked(true);}

3、创建main.cpp

#include <QApplication>#include "listalign.h"int main(int argc, char *argv[]){    QApplication app(argc,argv);    ListAligh *list = new ListAligh;    list->show();    return app.exec();}

4、创建资源文件,运行
这里写图片描述

5、资源文件

0 0
原创粉丝点击