【qt代码段落注释】「QT界面编程实例」Qt之文本编辑 |
您所在的位置:网站首页 › qt编写界面样例 › 【qt代码段落注释】「QT界面编程实例」Qt之文本编辑 |
【实例】Qt之文本编辑:设置字体字号、文本排序对齐 一、设置字体、字号等格式属性。 【应用场景描述】在编辑框中输入一段文字,用鼠标选取文字,修改工具栏上的字体、字号大小、加粗、斜体等属性,选取的文字即发生相应的变化。 【基本概念】在编写包含格式设置的文本编辑程序时,经常用到的Qt类有:QTextEdit、QTextDocument、QTextCharFormat、QTextCursor、QTextBlock、QTextList、QTextFrame、QTextTable、QTextBlockFormat、QTextListFormat、QTextFrameFormat、QTextTableFormat等。刚看到如此多的相关类可能会感到有些混乱,但只要弄清了它们之间的关系,运用起来就会非常方便,Qt已经为用户完成了几乎所有与编辑有关的具体工作,我们所要做的就是运用合适的类,调用合适的函数接口。 首先,任何一个文本编辑器的程序都要用到QTextEdit作为输入文本的容器,在它里面输入的可编辑文本由QTextDocument作为载体,而QTextBlock、QTextList、QTextFrame等则用来表示QTextDocument的元素,也可理解为QTextDocument的不同表现形式,可能为字符串、段落、列表、表格或是图片等。每种元素都有自己的格式,这些格式则用QTextCharFormat、QTextBlockFormat、QTextListFormat、QTextFrameFormat等类来描述与实现。例如,QTextBlockFormat类对应于QTextBlock类,QTextBlock类用于表示一块文本,一般可以理解为一个段落,但并不只指段落,QTextBlockFormat类则用于表示这一块文本的格式,如缩进值、与四边的边距等。各类之间的划分与关系可用下图进行描述: 从上图可知,QTextCursor类是一个非常重要也经常会用到的类,它用于表示编辑文本中的光标。这个类提供了对QTextDocument文档的修改接口,所有对文档格式的修改,说到底都与光标有关,如改变字符的格式,指的是改变光标处字符的格式;改变段落的格式,指的是改变光标所在段落的格式,因此,QTextCursor类在文档编辑类程序中有着重要的作用,所有对QTextDocument的修改能够通过QTextCursor类实现。 【继承关系】 【实例】 代码: class FontSet : public QMainWindow { Q_OBJECT public: FontSet(QWidget *parent = 0, Qt::WFlags flags = 0); ~FontSet(); protected slots: void slotFont( QString f ); void slotSize( QString num ); void slotBold(); void slotItalic(); void slotUnder(); void slotColor(); void slotCurrentFormatChanged( const QTextCharFormat & fmt ); private: void mergeFormat( QTextCharFormat fmt ); private: QLabel * pLabel1; QLabel * pLabel2; QFontComboBox * pFontBox; QComboBox * pSizeBox; QToolButton * pBoldBtn; QToolButton * pItalicBtn; QToolButton * pUnderBtn; QToolButton * pColorBtn; QTextEdit * pText; }; 代码: FontSet::FontSet(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { setWindowTitle( tr( "Font" ) ); pText = new QTextEdit( tr( "ajglkajdkgnakjfdsklaj" ), this ); setCentralWidget( pText ); QToolBar * pToolBar = addToolBar( "Font" ); //字体 pLabel1 = new QLabel( tr( "ZiTi:" ) ); pFontBox = new QFontComboBox(); pFontBox->setFontFilters( QFontComboBox::ScalableFonts ); pToolBar->addWidget( pLabel1 ); pToolBar->addWidget( pFontBox ); //字号 pLabel2 = new QLabel( tr( "ZiHao:" ) ); pSizeBox = new QComboBox(); pToolBar->addWidget( pLabel2 ); pToolBar->addWidget( pSizeBox ); QFontDatabase db; foreach( int nSize, db.standardSizes() ) pSizeBox->addItem( QString::number( nSize ) ); pToolBar->addSeparator(); //加粗、斜体、下划线、颜色 pBoldBtn = new QToolButton(); pBoldBtn->setIcon( QIcon( ".//Resource; ) ); pBoldBtn->setCheckable( true ); pToolBar->addWidget( pBoldBtn ); pItalicBtn = new QToolButton(); pItalicBtn->setIcon( QIcon( ".//Resource; ) ); pItalicBtn->setCheckable( true ); pToolBar->addWidget( pItalicBtn ); pUnderBtn = new QToolButton(); pUnderBtn->setIcon( QIcon( ".//Resource; ) ); pUnderBtn->setCheckable( true ); pToolBar->addWidget( pUnderBtn ); pToolBar->addSeparator(); pColorBtn = new QToolButton(); pColorBtn->setIcon( QIcon( ".//Resource; ) ); pToolBar->addWidget( pColorBtn ); //连接信号与槽函数 connect( pFontBox, SIGNAL( activated( QString ) ), this, SLOT( slotFont( QString ) ) ); connect( pSizeBox, SIGNAL( activated( QString ) ), this, SLOT( slotSize( QString ) ) ); connect( pBoldBtn, SIGNAL( clicked() ), this, SLOT( slotBold() ) ); connect( pItalicBtn, SIGNAL( clicked() ), this, SLOT( slotItalic() ) ); connect( pUnderBtn, SIGNAL( clicked() ), this, SLOT( slotUnder() ) ); connect( pColorBtn, SIGNAL( clicked() ), this, SLOT( slotColor() ) ); connect( pText, SIGNAL( currentCharFormatChanged( const QTextCharFormat & ) ), this, SLOT( slotCurrentFormatChanged( const QTextCharFormat& ) ) ); } FontSet::~FontSet() { } void FontSet::slotFont( QString f ) { QTextCharFormat fmt; ( f ); mergeFormat( fmt ); } void FontSet::slotSize( QString num ) { QTextCharFormat fmt; ( num.toFloat() ); mergeFormat( fmt ); } void FontSet::slotBold() { QTextCharFormat fmt; ( pBoldBtn->isChecked() ? QFont::Bold : QFont::Normal ); mergeFormat( fmt ); //pText->mergeCurrentCharFormat( fmt ); } void FontSet::slotItalic() { QTextCharFormat fmt; ( pItalicBtn->isChecked() ); mergeFormat( fmt ); } void FontSet::slotUnder() { QTextCharFormat fmt; ( pUnderBtn->isChecked() ); mergeFormat( fmt ); } void FontSet::slotColor() { QColor color = QColorDialog::getColor( Qt::red, this ); if ( color.isValid() ) { QTextCharFormat fmt; ( color ); mergeFormat( fmt ); } } /*当光标所在处的字符格式发生变化时调用,函数根据新的字符格式把工具栏上的各个格式控件的显示更新*/ void FontSet::slotCurrentFormatChanged( const QTextCharFormat & fmt ) { pFontBox->setCurrentIndex( pFontBox->findText( () ) ); pSizeBox->setCurrentIndex( pSizeBox->findText( QString::number( () ) ) ); pBoldBtn->setChecked( ().bold() ); pItalicBtn->setChecked( Italic() ); pUnderBtn->setChecked( Underline() ); } /*设置光标的选区,使格式作用于选区内的字符,若没有选区则作用于光标所在处的字符*/ void FontSet::mergeFormat( QTextCharFormat fmt ) { QTextCursor cursor = pText->textCursor(); if ( !cur() ) cur( QTextCursor::WordUnderCursor ); cur( fmt ); pText->mergeCurrentCharFormat( fmt ); } 二、设置文本排序及对齐 QTextListFormat主要用于描述文本排序的格式,它主要包括两个基本属性,一个为QTextListFormat::style,表示文本采用哪种排序方式;另一种为QTextListFormat::indent,表示排序后的缩进值。因此,要实现文本排序的功能只需设置好QTextListFormat的这两个属性,并把整个格式通过QTextCursor类应用到文本中即可。 【实例】 li代码: class listAlign : public QMainWindow { Q_OBJECT public: listAlign(QWidget *parent = 0, Qt::WFlags flags = 0); ~listAlign(); protected slots: void slotAlignment( QAction * ); void slotList( int ); void slotCursorPositionChanged(); private: QTextEdit * pText; QLabel * pLabel; QComboBox * pListBox; QAction * pLeftAction; QAction * pRightAction; QAction * pCenterAction; QAction * pJustifyAction; QAction * pRedoAction; QAction * pUndoAction; }; li代码: listAlign::listAlign(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { QFont f( "ZYSong18030", 12 ); setFont( f ); setWindowTitle( tr( "List&Alignment" ) ); QToolBar * pToolBar = addToolBar( "List" ); pLabel = new QLabel( tr( "List:" ) ); pListBox = new QComboBox( pToolBar ); pListBox->addItem( tr( "Standard" ) ); pListBox->addItem( tr( "Bullet List (Disc)" ) ); pListBox->addItem( tr( "Bullet List (Circle)" ) ); pListBox->addItem( tr( "Bullet List (Square)" ) ); pListBox->addItem( tr( "Ordered List (Decimal)" ) ); pListBox->addItem( tr( "Ordered List (Alpha lower)" ) ); pListBox->addItem( tr( "Ordered List (Alpha upper)" ) ); pToolBar->addWidget( pLabel ); pToolBar->addWidget( pListBox ); pToolBar->addSeparator(); QActionGroup * pActGrp = new QActionGroup( this ); pLeftAction = new QAction( QIcon( ".//Resource; ), tr( "left" ), pActGrp ); pLeftAction->setCheckable( true ); pCenterAction = new QAction( QIcon( ".//Resource; ), tr( "center" ), pActGrp ); pCenterAction->setCheckable( true ); pJustifyAction = new QAction( QIcon( ".//Resource; ), tr( "justify" ), pActGrp ); pJustifyAction->setCheckable( true ); pRightAction = new QAction( QIcon( ".//Resource; ), tr( "right" ), pActGrp ); pRightAction->setCheckable( true ); pToolBar->addActions( pActGrp->actions() ); QToolBar * pEditBar = addToolBar( "Edit" ); pUndoAction = new QAction( QIcon( ".//Resource; ), tr( "undo" ), this ); pEditBar->addAction( pUndoAction ); pRedoAction = new QAction( QIcon( ".//Resource; ), tr( "redo" ), this ); pEditBar->addAction( pRedoAction ); pText = new QTextEdit( this ); pText->setFocus(); setCentralWidget( pText ); connect( pListBox, SIGNAL( activated( int) ), this, SLOT( slotList( int ) ) ); connect( pActGrp, SIGNAL( triggered( QAction * ) ), this, SLOT( slotAlignment( QAction * ) ) ); connect( pRedoAction, SIGNAL( triggered() ), pText, SLOT( redo() ) ); connect( pUndoAction, SIGNAL( triggered() ), pText, SLOT( undo() ) ); connect( pText->document(), SIGNAL( redoAvailable( bool ) ), pRedoAction, SLOT( setEnabled( bool ) ) ); connect( pText->document(), SIGNAL( undoAvailable( bool ) ), pUndoAction, SLOT( setEnabled( bool ) ) ); connect( pText, SIGNAL( cursorPositionChanged() ), this, SLOT( slotCursorPositionChanged() ) ); } listAlign::~listAlign() { } void listAlign::slotAlignment( QAction * act ) { if ( act == pLeftAction ) pText->setAlignment( Qt::AlignLeft ); if ( act == pCenterAction ) pText->setAlignment( Qt::AlignCenter ); if ( act == pJustifyAction ) pText->setAlignment( Qt::AlignJustify ); if ( act == pRightAction ) pText->setAlignment( Qt::AlignRight ); } void listAlign::slotList( int index ) { QTextCursor cursor = pText->textCursor(); if ( index != 0 ) { QTextListFormat::Style style = QTextListFormat::ListDisc; switch ( index ) { default: 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; } cur(); QTextBlockFormat blockFmt = cur(); QTextListFormat listFmt; if ( cur() ) { listFmt = cur()->format(); } else { li( blockFmt.indent() + 1 ); blockFmt.setIndent( 0 ); cur( blockFmt ); } li( style ); cur( listFmt ); cur(); } else { QTextBlockFormat bfmt; b( -1 ); cur( bfmt ); } } void listAlign::slotCursorPositionChanged() { if ( pText->alignment() == Qt::AlignLeft) pLeftAction->setChecked(true); if (pText->alignment() == Qt::AlignCenter) pCenterAction->setChecked(true); if (pText->alignment() == Qt::AlignJustify) pJustifyAction->setChecked(true); if (pText->alignment() == Qt::AlignRight) pRightAction->setChecked(true); } 通过分享实用的计算机编程语言干货,推动中国编程到2025年基本实现普及化,使编程变得全民皆知,最终实现中国编程之崛起,这里是中国编程2025,感谢大家的支持。 原文链接: 1.文章《【qt代码段落注释】「QT界面编程实例」Qt之文本编辑》援引自互联网,为网友投稿收集整理,仅供学习和研究使用,内容仅代表作者本人观点,与本网站无关,侵删请点击此处。 2.文章《【qt代码段落注释】「QT界面编程实例」Qt之文本编辑》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。 3.文章转载时请保留本站内容来源地址,https://www.bluester.cn/p/379271.html qt代码段落注释 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |