Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to change the currentFont in a QTextEdit but leaving the formattation

    General and Desktop
    2
    11
    193
    Loading More Posts
    • 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.
    • H
      HenkCoder last edited by

      I'm currently developing a notepad and I've found a problem. Basically in a QTextEdit I have different text with different point sizes. So if I want to change the font of these ones their point size will reset to the one of the textbox.

      I will make an example with images.

      before:

      7c27b7f8-6347-44fb-9668-c39f67ecdab8-image.png enter image description here

      after:

      d30d4412-7c3b-49b7-97ef-e7d9845eedd1-image.png

      As you can see I changed the font to times new roman but the point size changed.

      This is the code:

      void MainWindow::on_fontComboBox_currentFontChanged(const QFont &f)
      {
          ui->fontComboBox->setFont(f);
          QTextEdit *edit = getTabTextEdit();
          if(ui->tabWidget->currentWidget() == ui->tab_1)
          {
              ui->edit->setCurrentFont(f);
          }
          else
          {
              edit->setCurrentFont(f);
          }
      }
      

      How can I solve this?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        How did you create the "f" QFont object ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply Reply Quote 0
        • H
          HenkCoder @SGaist last edited by HenkCoder

          @SGaist The QFont object is passed as a parameter in the on_fontComboBox_currentFontChanged() function.
          Basically I put a font in the fontComboBox and passees the parameter.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            That does not answer the question which is how is that font object created ?

            From the looks of it, you are not just changing the font name but you are nuking the current one.

            What is usually done is grabbing the current font of an object, modify whatever property you want and then apply that modified font on the widget.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            H 1 Reply Last reply Reply Quote 1
            • H
              HenkCoder @SGaist last edited by

              @SGaist Sorry I didn't understand what you said

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                QFont font = widget->font();
                font.setFamily("something");
                widget->setFont(font);
                

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                H 2 Replies Last reply Reply Quote 0
                • H
                  HenkCoder @SGaist last edited by

                  @SGaist alright thanks, I'll try it. I'll keep you updated.

                  1 Reply Last reply Reply Quote 0
                  • H
                    HenkCoder @SGaist last edited by

                    @SGaist
                    Hello, I tried the code you suggested. And it works yes, but an older problem appears again, this one

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      You should not apply fonts blindingly on QTextEdit if you want to have different fonts there you need to handle that à the document level.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply Reply Quote 0
                      • H
                        HenkCoder @SGaist last edited by

                        @SGaist Can you explain more? I don't understand much, sorry I'm a beginner in Qt

                        1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          You are using QTextEdit for custom editing, hence you want to only apply the font to the selected text and not the whole widget.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post