qt如何获取在textedit中的输入

2024-11-29 10:21:13
推荐回答(3个)
回答1:

  1. QString str = ui->textedit->toPlainText(); // 这是普通文本
    QString str = ui->textedit->toHtml(); // 这是富文本,即获取的是Html字符串

  2. 如果设置文本的话则对应的是:

    ui->textedit->setPlainText("123");

    ui->textedit->setHtml("123"); 

回答2:

连接document ()的contentsChange信号:

void QTextDocument::contentsChange ( int position, int charsRemoved, int charsAdded ) [signal]

This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied.

Information is provided about the position of the character in the document where the change occurred, the number of characters removed (charsRemoved), and the number of characters added (charsAdded).

The signal is emitted before the document's layout manager is notified about the change. This hook allows you to implement syntax highlighting for the document.

See also QAbstractTextDocumentLayout::documentChanged() and contentsChanged().

回答3:

ui->textEdit->toPlainText()