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 check if the selected word is bold or not
Forum Updated to NodeBB v4.3 + New Features

How to check if the selected word is bold or not

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.1k 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.
  • D Offline
    D Offline
    ddacot
    wrote on last edited by
    #1

    Hello, basicaly i want to check if the selected word in textEdit is bold already or not, if it's bold, then to check the bold button, if not, don't do anything. this is the code, what's wrong?
    @QTextCursor tc = ui->textEdit->textCursor();
    tc.select(QTextCursor::WordUnderCursor);
    if(tc.charFormat().fontWeight() == QFont::Bold) {
    ui->actionBold->setChecked(true);
    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tzander
      wrote on last edited by
      #2

      Did you look at the text edit example, I think it has the proper code to do what you ask.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        ddacot
        wrote on last edited by
        #3

        yes, i did, but it doesnt have what i need.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rhayader
          wrote on last edited by
          #4

          First connect the QTextEdit::currentCharFormatChanged SIGNAL to a custom SLOT where you will update the actionBold state
          @connect(textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),
          SLOT(charFormatChanged(QTextCharFormat)));
          @

          where the custom slot is something like this (change MyTextEdit to your class name)
          @void MyTextEdit::charFormatChanged(QTextCharFormat cf)
          {
          ui->actionBold->setChecked(cf.fontWeight() == QFont::Bold )
          }@

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tzander
            wrote on last edited by
            #5

            Please notice that bold is not a boolean, its an int (weight). See QFont for more details.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tzander
              wrote on last edited by
              #6

              Oh, another important detail is that each block (aka paragraph) has a char format too which is applied as a default for the whole paragraph. See the cursor API for blockCharFOrmat

              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