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. Style text in QTextEdit?
Qt 6.11 is out! See what's new in the release blog

Style text in QTextEdit?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 14.8k Views
  • 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.
  • Maaz MominM Offline
    Maaz MominM Offline
    Maaz Momin
    wrote on last edited by
    #8

    Also i can see that at the same time you are trying to change pushbutton's stylesheet. The only difference i see is "background-color: white/e6e6e6"

    You can achieve this similar to QTextEdit.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      legitnameyo
      wrote on last edited by
      #9

      Well, the styleVariant works but on the whole document. I am looking for something... more local. I want the text I write after pressing the bold button to become bold then when I press the bold button again the text goes back to normal. Adding unpolish() and polish() did the trick but again affected the whole document.

      Gojir4G 1 Reply Last reply
      0
      • L legitnameyo

        Well, the styleVariant works but on the whole document. I am looking for something... more local. I want the text I write after pressing the bold button to become bold then when I press the bold button again the text goes back to normal. Adding unpolish() and polish() did the trick but again affected the whole document.

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by Gojir4
        #10

        Hi @legitnameyo,

        I guess you are trying to reproduce some standard rich text editor features.
        You can get inspiration from the Text Edit Example.

        edit: The important part from this example are :

        void TextEdit::textBold()
        {
            QTextCharFormat fmt;
            fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
            mergeFormatOnWordOrSelection(fmt);
        }
        

        and

        void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
        {
            QTextCursor cursor = textEdit->textCursor();
            if (!cursor.hasSelection())
                cursor.select(QTextCursor::WordUnderCursor);
            cursor.mergeCharFormat(format);
            textEdit->mergeCurrentCharFormat(format);
        }
        

        I think this is all what you need to achieve the behavior you need.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          legitnameyo
          wrote on last edited by
          #11

          I have already looked at the Text Edit Example but there are two issue I have with it. 1. it's a bit too complicated at times for me and 2. I think it's using Qt4 since it references everything differently than I do in Qt5. Eg. it references textEdit->someThing() instead of ui->textEdit->something(), that along with the fact that it's Qt4 makes some code break in my environment. Because of this I can't copy, edit to my preference and learn from it. I tried using mergeFormatOnWordOrSelection but it gave me errors because it was the wrong Qt. I have Qt4 on my other computer and there it works fine but here on my mac with Qt5 it breaks. I'll try again though so as to get the error and maybe fix the issues!

          1 Reply Last reply
          0
          • L Offline
            L Offline
            legitnameyo
            wrote on last edited by legitnameyo
            #12

            the "mergeFormatOnWordOrSelection" gives me "error: out-of-line definition of 'mergeFormatOnWordOrSelection' does not match any declaration in 'MainWindow'". I've added the function to mainwindow.h and added every single header there is to add, just in case I'd miss one of those. Still, that is the error I get.

            void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
            {
                QTextCursor cursor = ui->textEdit->textCursor();
                if (!cursor.hasSelection())
                    cursor.select(QTextCursor::WordUnderCursor);
                cursor.mergeCharFormat(format);
                ui->textEdit->mergeCurrentCharFormat(format);
            }
            

            Edit: fixed it by adding "#include <QTextCharFormat>" at the top of my include list instead of the bottom, weird error though.

            Edit2:

            #include <QMainWindow>
            #include <QFile>
            #include <QTextCharFormat>
            #include <QFileDialog>
            #include <QTextStream>
            #include <QMessageBox>
            #include <QTabWidget>
            #include <QTextCursor>
            
            #include <QAction>
            #include <QApplication>
            #include <QClipboard>
            #include <QColorDialog>
            #include <QComboBox>
            #include <QFontComboBox>
            #include <QFile>
            #include <QFileDialog>
            #include <QFileInfo>
            #include <QFontDatabase>
            #include <QMenu>
            #include <QMenuBar>
            #include <QTextCodec>
            #include <QTextEdit>
            #include <QStatusBar>
            #include <QToolBar>
            #include <QTextCursor>
            #include <QTextDocumentWriter>
            #include <QTextList>
            #include <QtDebug>
            #include <QCloseEvent>
            #include <QMessageBox>
            #include <QMimeData>
            

            I added all of these headers to both mainwindow.cpp and mainwindow.h and it solved it. I know it isn't a good fix but right now I just want to get the project done.

            Gojir4G 1 Reply Last reply
            0
            • L legitnameyo

              the "mergeFormatOnWordOrSelection" gives me "error: out-of-line definition of 'mergeFormatOnWordOrSelection' does not match any declaration in 'MainWindow'". I've added the function to mainwindow.h and added every single header there is to add, just in case I'd miss one of those. Still, that is the error I get.

              void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
              {
                  QTextCursor cursor = ui->textEdit->textCursor();
                  if (!cursor.hasSelection())
                      cursor.select(QTextCursor::WordUnderCursor);
                  cursor.mergeCharFormat(format);
                  ui->textEdit->mergeCurrentCharFormat(format);
              }
              

              Edit: fixed it by adding "#include <QTextCharFormat>" at the top of my include list instead of the bottom, weird error though.

              Edit2:

              #include <QMainWindow>
              #include <QFile>
              #include <QTextCharFormat>
              #include <QFileDialog>
              #include <QTextStream>
              #include <QMessageBox>
              #include <QTabWidget>
              #include <QTextCursor>
              
              #include <QAction>
              #include <QApplication>
              #include <QClipboard>
              #include <QColorDialog>
              #include <QComboBox>
              #include <QFontComboBox>
              #include <QFile>
              #include <QFileDialog>
              #include <QFileInfo>
              #include <QFontDatabase>
              #include <QMenu>
              #include <QMenuBar>
              #include <QTextCodec>
              #include <QTextEdit>
              #include <QStatusBar>
              #include <QToolBar>
              #include <QTextCursor>
              #include <QTextDocumentWriter>
              #include <QTextList>
              #include <QtDebug>
              #include <QCloseEvent>
              #include <QMessageBox>
              #include <QMimeData>
              

              I added all of these headers to both mainwindow.cpp and mainwindow.h and it solved it. I know it isn't a good fix but right now I just want to get the project done.

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #13

              @legitnameyo Can we see the declaration in the header ?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                legitnameyo
                wrote on last edited by legitnameyo
                #14

                The main issue with

                void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                {
                   QTextCursor cursor = textEdit->textCursor();
                   if (!cursor.hasSelection())
                       cursor.select(QTextCursor::WordUnderCursor);
                   cursor.mergeCharFormat(format);
                   textEdit->mergeCurrentCharFormat(format);
                }
                

                is that it makes the whole previous word bold as well... is there a way to just make the letters written whilst the "bold" option is activated bold? instead of the previous word bold as well?

                Gojir4G 1 Reply Last reply
                0
                • L legitnameyo

                  The main issue with

                  void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                  {
                     QTextCursor cursor = textEdit->textCursor();
                     if (!cursor.hasSelection())
                         cursor.select(QTextCursor::WordUnderCursor);
                     cursor.mergeCharFormat(format);
                     textEdit->mergeCurrentCharFormat(format);
                  }
                  

                  is that it makes the whole previous word bold as well... is there a way to just make the letters written whilst the "bold" option is activated bold? instead of the previous word bold as well?

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #15

                  @legitnameyo That's because of cursor.select(QTextCursor::WordUnderCursor);

                  You can do something like :

                  void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                  {
                     QTextCursor cursor = textEdit->textCursor();
                     if (!cursor.hasSelection())
                         cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
                     cursor.mergeCharFormat(format);
                     textEdit->mergeCurrentCharFormat(format);
                  }
                  
                  
                  
                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    legitnameyo
                    wrote on last edited by
                    #16

                    Using this code is an improvement but still affects the letter behind the cursor

                    void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                    {
                       QTextCursor cursor = ui->textEdit->textCursor();
                       if (!cursor.hasSelection())
                           cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
                       cursor.mergeCharFormat(format);
                       ui->textEdit->mergeCurrentCharFormat(format);
                    }
                    

                    So close, but not what I was looking for... :)

                    Gojir4G 2 Replies Last reply
                    0
                    • L legitnameyo

                      Using this code is an improvement but still affects the letter behind the cursor

                      void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                      {
                         QTextCursor cursor = ui->textEdit->textCursor();
                         if (!cursor.hasSelection())
                             cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
                         cursor.mergeCharFormat(format);
                         ui->textEdit->mergeCurrentCharFormat(format);
                      }
                      

                      So close, but not what I was looking for... :)

                      Gojir4G Offline
                      Gojir4G Offline
                      Gojir4
                      wrote on last edited by
                      #17

                      @legitnameyo
                      And with QTextCursor::NextCharacter instead of QTextCursor::PreviousCharacter

                      1 Reply Last reply
                      2
                      • L legitnameyo

                        Using this code is an improvement but still affects the letter behind the cursor

                        void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
                        {
                           QTextCursor cursor = ui->textEdit->textCursor();
                           if (!cursor.hasSelection())
                               cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
                           cursor.mergeCharFormat(format);
                           ui->textEdit->mergeCurrentCharFormat(format);
                        }
                        

                        So close, but not what I was looking for... :)

                        Gojir4G Offline
                        Gojir4G Offline
                        Gojir4
                        wrote on last edited by
                        #18

                        @legitnameyo You can find all the possibilities in the documentation http://doc.qt.io/qt-5/qtextcursor.html#MoveOperation-enum

                        1 Reply Last reply
                        1
                        • Pl45m4P Pl45m4 referenced this topic on

                        • Login

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