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 change the currentFont in a QTextEdit but leaving the formattation
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 756 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • SGaistS SGaist

        Hi,

        How did you create the "f" QFont object ?

        H Offline
        H Offline
        HenkCoder
        wrote on last edited by HenkCoder
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          1
          • SGaistS SGaist

            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.

            H Offline
            H Offline
            HenkCoder
            wrote on last edited by
            #5

            @SGaist Sorry I didn't understand what you said

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6
              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
              0
              • SGaistS SGaist
                QFont font = widget->font();
                font.setFamily("something");
                widget->setFont(font);
                
                H Offline
                H Offline
                HenkCoder
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • SGaistS SGaist
                  QFont font = widget->font();
                  font.setFamily("something");
                  widget->setFont(font);
                  
                  H Offline
                  H Offline
                  HenkCoder
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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
                    0
                    • SGaistS SGaist

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

                      H Offline
                      H Offline
                      HenkCoder
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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
                        0

                        • Login

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