Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTableWidget different font in one cell
Forum Updated to NodeBB v4.3 + New Features

QTableWidget different font in one cell

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 3 Posters 1.5k 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.
  • A Offline
    A Offline
    AlexSmart
    wrote on last edited by
    #1

    Is it possible to put text in QTableWidget in different font (different background)?

    jsulmJ 1 Reply Last reply
    0
    • A AlexSmart

      Is it possible to put text in QTableWidget in different font (different background)?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AlexSmart https://doc.qt.io/qt-5/qtablewidget.html#itemAt
      https://doc.qt.io/qt-5/qtablewidgetitem.html#setFont

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • jsulmJ jsulm

        @AlexSmart https://doc.qt.io/qt-5/qtablewidget.html#itemAt
        https://doc.qt.io/qt-5/qtablewidgetitem.html#setFont

        A Offline
        A Offline
        AlexSmart
        wrote on last edited by
        #3

        @jsulm works fine with whole cell, but is it possible to chamge font within cell?

        jsulmJ 1 Reply Last reply
        0
        • A AlexSmart

          @jsulm works fine with whole cell, but is it possible to chamge font within cell?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AlexSmart Can you please explain what you mean?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AlexSmart
            wrote on last edited by
            #5

            @jsulm Something like that
            413c5a51-8d6a-4a80-a0e6-ffd159a2d0fa-изображение.png

            JonBJ 1 Reply Last reply
            0
            • A AlexSmart

              @jsulm Something like that
              413c5a51-8d6a-4a80-a0e6-ffd159a2d0fa-изображение.png

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

              @AlexSmart
              Since a cell only displays a single text. (You could try putting in rich text/HTML, but I don't think it supports that?). Sounds to me like you would need a QStyledItemDelegate to achieve the changed font. Or if you are lazy, and @VRonin doesn't see this, setCellWidget(QLabel*) and do your formatting in the label, which does accept HTML.

              1 Reply Last reply
              2
              • A Offline
                A Offline
                AlexSmart
                wrote on last edited by
                #7

                @JonB said in QTableWidget different font in one cell:

                Or if you are lazy, and @VRonin doesn't see this, setCellWidget(QLabel*)

                I got the idea but this side of coding is new to me, what is the main downside of this approach?

                JonBJ 1 Reply Last reply
                0
                • A AlexSmart

                  @JonB said in QTableWidget different font in one cell:

                  Or if you are lazy, and @VRonin doesn't see this, setCellWidget(QLabel*)

                  I got the idea but this side of coding is new to me, what is the main downside of this approach?

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

                  @AlexSmart

                  • We have a regular expert contributor named @VRonin (though he doesn't seem to have been around lately). His signature includes "wanting to banish all uses of setCellWidget(). If you use it, he shouts at you ;-)

                  • Widgets use relatively quite a bit of resources (think of what they have to be able to do). It's all very well when you have a handful of widgets on a page. But when you put them into tables (QTableWidget has a "convenience" method setCellWidget()) you are in danger of creating hundreds+ of them. Yet most of the time they really sit there just showing some text/bitmap/whatever, so most of the resource usage (memory, processing) is "wasted". It can for example become "slow" to show them all. Instead a QAbstractItemView-derived object (includes QTableView/QTableWidget) can use setItemDelegate() on a QStyledItemDelegate you write to set the display/editing of the items without needing the overhead of a full QWidget. If you had hundreds/thousands in a table this would be faster and use less resources. I didn't look to see whether/how easy it might be to emulate the mixed-font display supported by a QLabel in a QStyledItemDelegate: have a look/give it a go if you can, if you can't/you're lazy/you just want to see quickly what might be doable use setCellWidget() on a QLabel you create.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AlexSmart
                    wrote on last edited by
                    #9

                    @JonB thank you for explanation!
                    I will try setCellWidget()

                    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