Roson讲Qt #1 QTextEdit

不念不忘少年蓝@ 2022-09-06 01:23 319阅读 0赞

目录

1.设置文本

2.获取文本

3.文本修改事件

4.文本选择事件(光标移动时也会出发)

5.获取光标后的文本

6.获取光标前的文本

7.获取光标所在的行号

8.获取光标所在的列号

9.设置自动换行模式

10.设置字体颜色

方法1

方法2

方法3

方法4

11.设置背景色

方法1

方法2

方法3

12.设置字体

13.改变任意位置部分字符的颜色

14.选中任意位置任意长度的字符

15.设置html格式的文本


1.设置文本

  1. ui.textEdit->setText("Hello world");

2.获取文本

  1. qDebug()<<ui.textEdit->toPlainText();

3.文本修改事件

  1. public slots:
  2. void OnTextChanged();
  3. ...
  4. connect(ui.textEdit,SIGNAL(textChanged()),this,SLOT(OnTextChanged()));

4.文本选择事件(光标移动时也会出发)

  1. public slots:
  2. void OnSelectionChanged();
  3. ...
  4. connect(ui.textEdit, SIGNAL(selectionChanged()), this, SLOT(OnSelectionChanged()));

5.获取光标后的文本

  1. int charIndex = ui.textEdit->textCursor().position();
  2. QString strText = ui.textEdit->toPlainText().mid(charIndex);
  3. qDebug() << "Text After Cursor:" << strText << "\n";

6.获取光标前的文本

  1. int charIndex = ui.textEdit->textCursor().position();
  2. strText = ui.textEdit->toPlainText().left(charIndex);
  3. qDebug() << "Text before Cursor:" << strText << "\n";

7.获取光标所在的行号

  1. int rowNo = ui.textEdit->textCursor().blockNumber();

8.获取光标所在的列号

  1. int colNo = ui.textEdit->textCursor().columnNumber();

9.设置自动换行模式

  1. ui.textEdit->setWordWrapMode(QTextOption::NoWrap);

Qt提供了下面几种换行模式,根据需要选择





























QTextOption::NoWrap 0 文本完全没有换行。
QTextOption::WordWrap 1 文本在单词边界处换行。
QTextOption::ManualWrap 2 和NoWrap一样。
QTextOption::WrapAnywhere 3 文本可以在一行中的任何点换行,即使它出现在单词的中间。
QTextOption::WrapAtWordBoundaryOrAnywhere 4 如果可能,换行发生在单词边界;否则,它将出现在行中适当的点,甚至在单词的中间。

10.设置字体颜色

方法1

  1. QPalette palete;
  2. palete.setColor(QPalette::Text,Qt::red);
  3. ui.textEdit->setPalette(palete);

20210819193008169.png

方法2

  1. ui.textEdit->setTextColor(QColor("blue"));
  2. ui.textEdit->append("4564564646");

方法3

  1. ui.textEdit->setStyleSheet("color:rgb(80,240,120)");
  2. ui.textEdit->append("ppppad");

方法4

  1. QTextCharFormat format;
  2. format.setForeground(QBrush("red"));
  3. ui.textEdit->mergeCurrentCharFormat(format);
  4. ui.textEdit->append("abcdefgla");

其中方法2和方法4在编辑框无任何字符时使用,将不会生效。只有等编辑框上面有了字符之后,再用才会有效果。

11.设置背景色

方法1

  1. ui.textEdit->setStyleSheet("background-color:rgb(80,240,120)");
  2. ui.textEdit->append("ppppad");

方法2

  1. QTextCharFormat format;
  2. format.setBackground(QBrush("red"));
  3. ui.textEdit->mergeCurrentCharFormat(format);
  4. ui.textEdit->append("abcdefgla");

方法3

  1. QPalette palete;
  2. palete.setColor(QPalette::Base, Qt::red);
  3. ui.textEdit->setPalette(palete);
  4. ui.textEdit->append("ppppad");

12.设置字体

  1. ui.textEdit->setFont(QFont("Times", 20, QFont::Bold));

20210819192710961.png

13.改变任意位置部分字符的颜色

  1. QTextCharFormat format;
  2. format.setForeground(QBrush("red"));
  3. QTextCursor cursor = ui.textEdit->textCursor();
  4. cursor.setPosition(0, QTextCursor::MoveAnchor);
  5. cursor.setPosition(5, QTextCursor::KeepAnchor);
  6. cursor.mergeCharFormat(format);

20210819192636399.png

14.选中任意位置任意长度的字符

  1. QTextCursor cursor = ui.textEdit->textCursor();
  2. cursor.setPosition(5, QTextCursor::MoveAnchor);
  3. cursor.setPosition(10, QTextCursor::KeepAnchor);
  4. ui.textEdit->setTextCursor(cursor);

20210819192606510.png

15.设置html格式的文本

  1. ui.textEdit->setHtml("<i>This text is italic</i>");

20210819192531293.png

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RosonQt #3 QLineEdit

    1.什么是QLineEdit     QLineEdit小部件是一个单行文本编辑器。                     QLineEdit允许用户输入和编辑一行纯