Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to insert italic text to QPlainTextEdit?



  • How do I insert italic text to QPlainTextEdit? I tried this below, but the text is not italicized.

    Thanks

    void MainWindow::parserReadyTextEvent(QByteArray buffer)
            {
            QTextCharFormat fmt;
            fmt.setFontItalic(true);
            ui->outputConsole->setCurrentCharFormat(fmt);
            ui->outputConsole->moveCursor(QTextCursor::End);
            ui->outputConsole->insertPlainText(QString(buffer));
            }
    

  • Qt Champions 2019



  • Hi @Christian-Ehrlicher ,

    Thank you for that, I replaced QPlainTextEdit with QTextEdit and I get partial success, mixed results. Basically, I set the font to italic, write my text to the QTextEdit and then set the font back to "non-italic" and any subsequent text entered is still italic. Strangely, if the moveCursor() line is not commented out, then no text is italicized, I'm not clear how this is happening or why. Do you have any advice? Basically, all I want to do is italicize or bold specific text that is inserted, then turn off the italicized or bold for any other text that is inserted, but it doesn't seem to work as expected. Here are the two functions I'm using:

    void MainWindow::commandInputEvent()
        {
        if (this->socket->state() == QTcpSocket::ConnectedState)
            this->socket->write(QByteArray(QString(ui->cmdLine->text() + "\n").toLatin1()).data());
    
        // Adding the text from input line to console window to "echo" what was entered
        ui->outputConsole->setFontItalic(true);
        
        // If the following code line is commented out, then even text inserted called from parserReadyTextEvent() is italicized
        // If the following code line is not commented out, then NO TEXT is italicized.
        // ui->outputConsole->moveCursor(QTextCursor::End);
        
        ui->outputConsole->insertPlainText(ui->cmdLine->text() + "\n");
        ui->outputConsole->setFontItalic(false);
        ui->cmdLine->setText("");
        }
    
    // This just adds normal non-decorated text to the console QTextEdit.  But it is italicized now because of the above
    void MainWindow::parserReadyTextEvent(QByteArray buffer)
        {
        ui->outputConsole->moveCursor(QTextCursor::End);
        ui->outputConsole->insertPlainText(QString(buffer));
        }
    

    Is there a different approach to handle this that would give the result I want?


Log in to reply