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. 2 Different fonts in a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

2 Different fonts in a QTextEdit

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

    I'm currently developing a notepad in Qt and I found a problem. I want to put two different fonts in a QTextEdit Before it's like this

    eca43b80-e17c-47c8-a626-965affdfde74-image.png

    and when I want to put another font it comes out like this:

    08665278-4be2-4712-a403-956b69ddb71e-image.png

    Is there a way to do that? Thanks.

    JonBJ 1 Reply Last reply
    0
    • H HenkCoder

      I'm currently developing a notepad in Qt and I found a problem. I want to put two different fonts in a QTextEdit Before it's like this

      eca43b80-e17c-47c8-a626-965affdfde74-image.png

      and when I want to put another font it comes out like this:

      08665278-4be2-4712-a403-956b69ddb71e-image.png

      Is there a way to do that? Thanks.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @HenkCoder
      Yes perfectly doable. You need to set the desired font on each block separately; I think you're changing it on the whole QTextEdit? Show your code.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HenkCoder
        wrote on last edited by HenkCoder
        #3

        @JonB
        Here it 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->setFont(f);
            }
            else
            {
                edit->setFont(f);
            }
        }
        
        
        JonBJ 1 Reply Last reply
        0
        • H HenkCoder

          @JonB
          Here it 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->setFont(f);
              }
              else
              {
                  edit->setFont(f);
              }
          }
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @HenkCoder
          You are indeed setting the font for the whole QTextEdit.

          Have you taken the time to read through https://doc.qt.io/qt-5/qtextedit.html#details? You can either use a text cursor and setCurrentCharFormat() etc., or append HTML text with the desired font in it. Google qtextedit multiple fonts , e.g. Using different fonts/attributes in QTextEdit.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            HenkCoder
            wrote on last edited by
            #5

            @JonB
            Thank you, I resolved this thing using setCurrentFont().

            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