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. Insert fill circle into cell of QTableWidget
QtWS25 Last Chance

Insert fill circle into cell of QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
88 Posts 5 Posters 39.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.
  • J Offline
    J Offline
    juaniyoalm
    wrote on 12 Nov 2018, 22:17 last edited by
    #1

    Hi,

    Is posible insert circle into cell of QTableWidget? I need the size of the circle can be changed dynamically.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Nov 2018, 22:21 last edited by
      #2

      Hi,

      Use a custom QStyledItemDelegate and do the drawing yourself in there.

      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
      5
      • J Offline
        J Offline
        juaniyoalm
        wrote on 12 Nov 2018, 22:33 last edited by juaniyoalm 11 Dec 2018, 22:34
        #3

        @SGaist Thank you for your answer.

        Sorry but I'm starting to use QT, could you give me an example? How would you insert it in the table?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Nov 2018, 23:06 last edited by
          #4

          See the Star Item Delegate Example.

          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
          5
          • J Offline
            J Offline
            juaniyoalm
            wrote on 13 Nov 2018, 19:04 last edited by
            #5

            There are too many classes to just paint a circle in a cell. There is no other way?

            M 1 Reply Last reply 13 Nov 2018, 19:09
            0
            • J juaniyoalm
              13 Nov 2018, 19:04

              There are too many classes to just paint a circle in a cell. There is no other way?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 13 Nov 2018, 19:09 last edited by mrjj
              #6

              @juaniyoalm
              Well you can also add the circle as an icon to the cell.
              If you add blank text, the effect is similar.

              alt text

              However, how big do u need the circle to be ?
              It cannot be bigger than the cell height in any case.

              Update: this was just to answer if there was alternatives.
              However, a delegate is far better for it.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Nov 2018, 19:31 last edited by
                #7

                You need one class: the delegate.

                In this example they have two if you take the custom editor that you likely don't need.

                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
                2
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 13 Nov 2018, 20:21 last edited by mrjj
                  #8

                  Hi
                  Adding to SGaist . Its not as bad as it looks.
                  For a paint circle only delegate, you need very little code.

                  #include <QStyledItemDelegate>
                  #include <QPainter>
                  
                  class CircleDelegate: public QStyledItemDelegate {
                   protected:
                    void paint(QPainter* painter, const QStyleOptionViewItem& opt, const QModelIndex& index) const {
                  
                      // set brush to green if selected else blue ( not needed just for fun)
                      if (opt.state & QStyle::State_Selected) {
                        painter->setBrush(Qt::green);
                      } else {
                        painter->setBrush(Qt::blue);
                      }
                  
                      // paint a circle
                      int CircleSize=10;
                      painter->drawEllipse(opt.rect.center(), CircleSize, CircleSize);
                    }
                  };
                  

                  and you set it to the table by
                  ui->tableWidget->setItemDelegateForColumn(1, new CircleDelegate());
                  the 1 is the col where u want it

                  alt text

                  You will need to read over
                  http://doc.qt.io/qt-5/model-view-programming.html
                  mostly section Item roles
                  as to make circle dynamic in size, you can use
                  a Qt::UserRole and setData.
                  so the size comes from the model.

                  1 Reply Last reply
                  5
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 14 Nov 2018, 08:33 last edited by
                    #9

                    Just a couple of small addition to @mrjj 's code:

                    • you'll probably also need to reimplement sizeHint() to return a size appropriate to contain the circle.
                    • blindly modifying the painter argument is risky. You should call painter->save(); before setting the brush and painter->restore(); after you finished the painting

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    2
                    • J Offline
                      J Offline
                      juaniyoalm
                      wrote on 27 Nov 2018, 18:44 last edited by
                      #10

                      Thank you, the solution is good for me!!! but I would like that the background of cells is fill too. This colour have to change too.
                      I thought about inserting a rectangle but I would need the circle to be located on top...

                      M 1 Reply Last reply 27 Nov 2018, 19:23
                      0
                      • J juaniyoalm
                        27 Nov 2018, 18:44

                        Thank you, the solution is good for me!!! but I would like that the background of cells is fill too. This colour have to change too.
                        I thought about inserting a rectangle but I would need the circle to be located on top...

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 27 Nov 2018, 19:23 last edited by
                        #11

                        @juaniyoalm
                        Hi
                        Just use other paint methods like
                        painter->drawRect(opt.rect);

                        J 1 Reply Last reply 27 Nov 2018, 19:28
                        1
                        • M mrjj
                          27 Nov 2018, 19:23

                          @juaniyoalm
                          Hi
                          Just use other paint methods like
                          painter->drawRect(opt.rect);

                          J Offline
                          J Offline
                          juaniyoalm
                          wrote on 27 Nov 2018, 19:28 last edited by
                          #12

                          @mrjj Thank you so much!!

                          Okay, I have that solved.

                          Another question is that the size of each circle depends on a value that my mushroom class has. In my class I created a paint method but I do not know how to link that method with the delegate or what is the way to write the method.

                          M 1 Reply Last reply 27 Nov 2018, 19:44
                          0
                          • J juaniyoalm
                            27 Nov 2018, 19:28

                            @mrjj Thank you so much!!

                            Okay, I have that solved.

                            Another question is that the size of each circle depends on a value that my mushroom class has. In my class I created a paint method but I do not know how to link that method with the delegate or what is the way to write the method.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 27 Nov 2018, 19:44 last edited by
                            #13

                            @juaniyoalm
                            Hi, is the value in the model ?
                            The delegate can use values from model.
                            so the color and size of circle could come from model.

                            J 1 Reply Last reply 27 Nov 2018, 19:54
                            1
                            • M mrjj
                              27 Nov 2018, 19:44

                              @juaniyoalm
                              Hi, is the value in the model ?
                              The delegate can use values from model.
                              so the color and size of circle could come from model.

                              J Offline
                              J Offline
                              juaniyoalm
                              wrote on 27 Nov 2018, 19:54 last edited by
                              #14

                              @mrjj

                              No, that value is in a c++class. I have to create a custom model... It's ok?? Or I can use a default model??

                              M 1 Reply Last reply 27 Nov 2018, 19:58
                              0
                              • J juaniyoalm
                                27 Nov 2018, 19:54

                                @mrjj

                                No, that value is in a c++class. I have to create a custom model... It's ok?? Or I can use a default model??

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 27 Nov 2018, 19:58 last edited by
                                #15

                                @juaniyoalm
                                You can use http://doc.qt.io/qt-5/qstandarditemmodel.html
                                if it is. dont have to be custom.
                                I though you already had a model ?
                                Also is this with a QTableWidget ?
                                (it uses a model already )

                                J 1 Reply Last reply 27 Nov 2018, 20:22
                                1
                                • M mrjj
                                  27 Nov 2018, 19:58

                                  @juaniyoalm
                                  You can use http://doc.qt.io/qt-5/qstandarditemmodel.html
                                  if it is. dont have to be custom.
                                  I though you already had a model ?
                                  Also is this with a QTableWidget ?
                                  (it uses a model already )

                                  J Offline
                                  J Offline
                                  juaniyoalm
                                  wrote on 27 Nov 2018, 20:22 last edited by
                                  #16

                                  @mrjj

                                  No, I use QTableView.

                                  I used a model to try but I don't know how to implement it

                                  M 1 Reply Last reply 27 Nov 2018, 20:28
                                  0
                                  • J juaniyoalm
                                    27 Nov 2018, 20:22

                                    @mrjj

                                    No, I use QTableView.

                                    I used a model to try but I don't know how to implement it

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 27 Nov 2018, 20:28 last edited by
                                    #17

                                    @juaniyoalm
                                    Super. With view its much easier.
                                    so you have to construct a model with the data
                                    that comes from the class.
                                    Did you make a qstandarditemmodel before ?

                                    J 1 Reply Last reply 27 Nov 2018, 20:38
                                    1
                                    • M mrjj
                                      27 Nov 2018, 20:28

                                      @juaniyoalm
                                      Super. With view its much easier.
                                      so you have to construct a model with the data
                                      that comes from the class.
                                      Did you make a qstandarditemmodel before ?

                                      J Offline
                                      J Offline
                                      juaniyoalm
                                      wrote on 27 Nov 2018, 20:38 last edited by
                                      #18

                                      @mrjj
                                      Yes, I did but without data class

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        juaniyoalm
                                        wrote on 28 Nov 2018, 10:29 last edited by
                                        #19

                                        @mrjj
                                        Do you show me how to make??

                                        M 1 Reply Last reply 28 Nov 2018, 10:48
                                        0
                                        • J juaniyoalm
                                          28 Nov 2018, 10:29

                                          @mrjj
                                          Do you show me how to make??

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 28 Nov 2018, 10:48 last edited by
                                          #20

                                          @juaniyoalm
                                          Hi
                                          Please read about userRoles and setData

                                          http://doc.qt.io/qt-5/model-view-programming.html
                                          section Item roles

                                          you can simply put the color / size in user role, or you can have
                                          it directly as an item in your model.
                                          Both ways are fine.
                                          Then delegate read the user role and use the data.

                                          1 Reply Last reply
                                          4

                                          • Login

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