Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to insert italic text to QPlainTextEdit?
Forum Updated to NodeBB v4.3 + New Features

How to insert italic text to QPlainTextEdit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snorkelbuckle
    wrote on last edited by
    #1

    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));
            }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Snorkelbuckle said in How to insert italic text to QPlainTextEdit?:

      QPlainTextEdit

      If you want formatting, use QTextEdit. See https://doc.qt.io/qt-5/qplaintextedit.html#differences-to-qtextedit and https://doc.qt.io/qt-5/qplaintextdocumentlayout.html#details

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Snorkelbuckle
        wrote on last edited by Snorkelbuckle
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved