设置文本排序及对齐
来源:互联网 发布:北京数据 编辑:程序博客网 时间: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
- 设置文本排序及对齐
- 对齐文本
- 文本对齐
- 文本对齐
- C语言字节对齐及设置编译对齐方式方法
- C语言字节对齐及设置编译对齐方式方法
- CSS中图片于文本的基线对齐设置
- 设置text-overflow文本溢出隐藏时的对齐问题
- 设置text-overflow: ellipsis后引起的文本对齐问题
- 如何在Beyond Compare合并文本时设置对齐方式
- CSS中图片于文本的基线对齐设置
- 在Word2013中设置文本格式与对齐方式
- 【CSS】文本对齐(左右均对整齐)及断行
- H5成长之路 元素及文本对齐方式
- Radan 自动排序文本的设置
- 【PyQt实例4】设置文体排序和对齐【转】
- LoadRunner12设置文本检查点及图片检查点
- MFC文本输出及文本的效果设置
- PAT 1051. Pop Sequence (25)
- github 修改tag
- 自定义控件 实现文本框输入模仿下拉框选择功能
- Android初学习 - 编译时不生成odex文件的方法
- 选择排序
- 设置文本排序及对齐
- 指针常量和常量指针
- 页面的跳转
- Ubuntu server 15.0.4安装Redis
- SPOJ FAVDICE
- Codeforces Round #256 (Div. 2) B. Suffix Structures
- SSH框架总结-框架分析
- 欢迎使用CSDN-markdown编辑器
- json 在线转换