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. Keep focus on QTextEdit while clicking toggle button on multiple QTextEdit windows
Forum Update on Monday, May 27th 2025

Keep focus on QTextEdit while clicking toggle button on multiple QTextEdit windows

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.6k 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.
  • AsimovA Offline
    AsimovA Offline
    Asimov
    wrote on last edited by Asimov
    #1

    Ok I want to setup multiple QTextedt windows, and buttons like bold,italic etc.

    Now this is the problem I have cheated, but it works, but this won't work on multiple QTextedits.
    You see the problem is that when you click on the toggle button you use lose focus.
    Now I have got around this on one window by re-focusing on the QTextedit window.

    eg

    void MainWindow::on_pushButton_3_toggled(bool checked)
    {
        if(checked==true){
            ui->textEdit->setFontWeight(QFont::DemiBold);
        }else{
            ui->textEdit->setFontWeight(QFont::Normal);
        }
        ui->textEdit->setFocus();
    }
    

    But I want to use the same button on multiple QTextviews.
    The above code will only work on one single QTextedit called textEdit.

    I wonder if there is a way to get the last object that lost focus when you click on a toggle button.
    This way I can set the font to bold on multiple QTextedit windows, rather than hard coding in a single Qtextview like I have here.

    I don't really want to use multiple buttons, but have one button strip that can be use for more than one text view.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Maybe by setting the focus to NoFocus in Qt Designer or by code :

      toggleButton->setFocus(Qt::NoFocus);
      
      1 Reply Last reply
      2
      • AsimovA Offline
        AsimovA Offline
        Asimov
        wrote on last edited by Asimov
        #3

        @mpergand
        Great that has answered half my question. This really works cool

        Now all I have to do is to get the window that has focus. I think I found the command to do it, but don't think I am doing it right.

        QWidget * thing = qApp->focusWidget();
        

        Instead of

        ui->textEdit->setFontWeight(QFont::DemiBold);
        

        I thought I could do this, but it doesn't work

        thing->setFontWeight(QFont::DemiBold);
        

        So I thought I could do something like this

           QWidget * thing = qApp->focusWidget();
            if(checked==true){
               thing->setFontWeight(QFont::DemiBold);
            }else{
                thing->setFontWeight(QFont::Normal);
            }
        

        But thing should be the textEdit window, but why I can't I do thing->setFontWeight.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          you need qobject_cast
          Have a look at the doc.

          1 Reply Last reply
          1
          • AsimovA Offline
            AsimovA Offline
            Asimov
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • AsimovA Offline
              AsimovA Offline
              Asimov
              wrote on last edited by
              #6

              @mpergand
              Hey I got it to work thanks.
              It took some time because I always find that the docs tell you everything, but what you want to know LOL.
              Anyway here is my code now

              void MainWindow::on_pushButton_3_toggled(bool checked)
              {
                  QWidget * thing = qApp->focusWidget();
                  QTextEdit *test = qobject_cast<QTextEdit *>(thing);
                  if(checked==true){
                      test->setFontWeight(QFont::DemiBold);
                  }else{
                      test->setFontWeight(QFont::Normal);
                  }
              }
              

              I might set up QWidget and the QTextEdit variables in the header file later so they not constantly being recreated, but basically it works, and I might pick better variable names LOL.

              Thanks I can now make text bold in 2 QTextEdit windows.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                It's advisable to check the value of a dynamic cast/qobject cast because it can be null if the object cannot be converted to the specified type.

                1 Reply Last reply
                2
                • AsimovA Offline
                  AsimovA Offline
                  Asimov
                  wrote on last edited by
                  #8

                  @mpergand
                  I think this will do the trick, thanks;

                  QWidget * thing = qApp->focusWidget();
                      QTextEdit *test = qobject_cast<QTextEdit *>(thing);
                      if(test!=NULL){
                          if(checked==true){
                              test->setFontWeight(QFont::DemiBold);
                          }else{
                              test->setFontWeight(QFont::Normal);
                          }
                      }
                  
                  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