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 use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?

How to use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 7.9k 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.
  • JonBJ JonB

    @ailika
    In that case: either it's possible to have a delegate for normal, non-edit viewing which "looks like" a combobox --- I don't know if you can do that, as you say maybe QStyleOption something --- and then it has to change into a real combobox on click. Or, also as you say, go back to your preferred setCellWidget(). In which case I showed earlier how you can access the row/column, or the actual combobox, in the slot, which is what you originally asked for.

    A Offline
    A Offline
    ailika
    wrote on last edited by
    #19

    @JonB Thank you for your answer. Your answer is one way . I will start a new question if I cant do the QStyle function.

    1 Reply Last reply
    0
    • JonBJ JonB

      @ailika
      In that case: either it's possible to have a delegate for normal, non-edit viewing which "looks like" a combobox --- I don't know if you can do that, as you say maybe QStyleOption something --- and then it has to change into a real combobox on click. Or, also as you say, go back to your preferred setCellWidget(). In which case I showed earlier how you can access the row/column, or the actual combobox, in the slot, which is what you originally asked for.

      A Offline
      A Offline
      ailika
      wrote on last edited by
      #20

      @JonB But can I ask why setCellWidget is usually not good?

      JonBJ 1 Reply Last reply
      0
      • A ailika

        @JonB But can I ask why setCellWidget is usually not good?

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

        @ailika
        @VRonin is the one who guided you away from setCellWidget()/setIndexWidget(). Look at his signature! :)

        I backed him up, pointing out to you one disadvantage of memory/efficiency.

        He is generally correct. However, I am not necessarily on the crusade he is on! :) I answered your question initially for the setCellWidget() you asked about. For 250 comboboxes I personally would use his styled item delegate. But I am not going to die in a ditch if you are aware of this but still wish to use setCellWidget() in your own code. Just don't tell him I said this to you.... ;-)

        A 2 Replies Last reply
        0
        • JonBJ JonB

          @ailika
          @VRonin is the one who guided you away from setCellWidget()/setIndexWidget(). Look at his signature! :)

          I backed him up, pointing out to you one disadvantage of memory/efficiency.

          He is generally correct. However, I am not necessarily on the crusade he is on! :) I answered your question initially for the setCellWidget() you asked about. For 250 comboboxes I personally would use his styled item delegate. But I am not going to die in a ditch if you are aware of this but still wish to use setCellWidget() in your own code. Just don't tell him I said this to you.... ;-)

          A Offline
          A Offline
          ailika
          wrote on last edited by
          #22

          @JonB Thank you both! It seems this form is very good. There are people there answering my question. And it seems that qt is somehow well surpported.

          1 Reply Last reply
          0
          • JonBJ JonB

            @ailika
            @VRonin is the one who guided you away from setCellWidget()/setIndexWidget(). Look at his signature! :)

            I backed him up, pointing out to you one disadvantage of memory/efficiency.

            He is generally correct. However, I am not necessarily on the crusade he is on! :) I answered your question initially for the setCellWidget() you asked about. For 250 comboboxes I personally would use his styled item delegate. But I am not going to die in a ditch if you are aware of this but still wish to use setCellWidget() in your own code. Just don't tell him I said this to you.... ;-)

            A Offline
            A Offline
            ailika
            wrote on last edited by
            #23

            @JonB I have been studying the QStyle's help documentation theses days. It really makes me angry that I find the sentence "The list that pops up when the user clicks on the combo box is drawn by a delegate, which we do not cover in this overview. " How to display the important popup menu is not described!! And I have alreay displayed the others quite well.
            Do I need to write the hard code for the whole popup window all my self? I don't see it very necessary, since qt has alreay paint the combo box. I think it will be a repeated work if I write the paint code myself.

            mrjjM 1 Reply Last reply
            0
            • A ailika

              @JonB I have been studying the QStyle's help documentation theses days. It really makes me angry that I find the sentence "The list that pops up when the user clicks on the combo box is drawn by a delegate, which we do not cover in this overview. " How to display the important popup menu is not described!! And I have alreay displayed the others quite well.
              Do I need to write the hard code for the whole popup window all my self? I don't see it very necessary, since qt has alreay paint the combo box. I think it will be a repeated work if I write the paint code myself.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #24

              @ailika
              Hi
              Its important to read about the delegates and understand how they work.

              When not in edit mode the delegate draws the widget using pure QPainter/Qstyle calls. This is very fast so scrolling works good.
              Then when you trigger edit event for a cell the delegate createEditor
              creates a real Widget. in your case a combobox and handling the
              editing of the cell value.

              So the delegate get it speed improvements from not using a full widgets all the time.

              A 1 Reply Last reply
              1
              • mrjjM mrjj

                @ailika
                Hi
                Its important to read about the delegates and understand how they work.

                When not in edit mode the delegate draws the widget using pure QPainter/Qstyle calls. This is very fast so scrolling works good.
                Then when you trigger edit event for a cell the delegate createEditor
                creates a real Widget. in your case a combobox and handling the
                editing of the cell value.

                So the delegate get it speed improvements from not using a full widgets all the time.

                A Offline
                A Offline
                ailika
                wrote on last edited by
                #25

                @mrjj I'm reading 'Model/View programming' documentation. But first I don't quite under stand the edit role. Usually, when I double click the item, it will enter the "edit" state, isn't it? But in combox of spinbox, I don't use double click, I just click the spiral area of combox, and it will popup. Is this calling a "edit" state? If it is seen as a edit state, it is not a normal "edit", is it?
                Maybe after I go through the 'Model/View programming' documentation, I can understand this, since I have seen " To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized." Maybe the operation I said above is seen as a customized edit.

                mrjjM 1 Reply Last reply
                0
                • A ailika

                  @mrjj I'm reading 'Model/View programming' documentation. But first I don't quite under stand the edit role. Usually, when I double click the item, it will enter the "edit" state, isn't it? But in combox of spinbox, I don't use double click, I just click the spiral area of combox, and it will popup. Is this calling a "edit" state? If it is seen as a edit state, it is not a normal "edit", is it?
                  Maybe after I go through the 'Model/View programming' documentation, I can understand this, since I have seen " To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized." Maybe the operation I said above is seen as a customized edit.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #26

                  @ailika

                  Hi
                  The "edit state" talked about is in regards to the View. (like TableView/widget)
                  Normally one has to double click to make the view go into edit mode. However the
                  EditTriggers setting can alter this to allow other ways.

                  • Usually, when I double click the item, it will enter the "edit" state, isn't it?
                    yes.

                  In regards to your combobox. When using a delegate, when you trigger edit mode, the view tells the delegate and
                  it creates a real combobox. so when it's a real combo, it's also in editmode.

                  The delegate is then responsible to load and save data as the View dont know to handle say a combobox.

                  However, when you use setCellWidget, you just float a widget on top of the cells.
                  It does know to align the Widget with the cell but that is about it. There is no real concept of edit mode
                  here as the Widget (combo) will cover up the cell so you dont get to click it.

                  You must then use combobox signals etc to handle saving and loading data.

                  a basic delegate is not so bad.
                  https://wiki.qt.io/Combo_Boxes_in_Item_Views

                  A 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @ailika

                    Hi
                    The "edit state" talked about is in regards to the View. (like TableView/widget)
                    Normally one has to double click to make the view go into edit mode. However the
                    EditTriggers setting can alter this to allow other ways.

                    • Usually, when I double click the item, it will enter the "edit" state, isn't it?
                      yes.

                    In regards to your combobox. When using a delegate, when you trigger edit mode, the view tells the delegate and
                    it creates a real combobox. so when it's a real combo, it's also in editmode.

                    The delegate is then responsible to load and save data as the View dont know to handle say a combobox.

                    However, when you use setCellWidget, you just float a widget on top of the cells.
                    It does know to align the Widget with the cell but that is about it. There is no real concept of edit mode
                    here as the Widget (combo) will cover up the cell so you dont get to click it.

                    You must then use combobox signals etc to handle saving and loading data.

                    a basic delegate is not so bad.
                    https://wiki.qt.io/Combo_Boxes_in_Item_Views

                    A Offline
                    A Offline
                    ailika
                    wrote on last edited by
                    #27

                    @mrjj Hi, I've already used this mode, and I need to at least single click in order to enter edit state, which I don't like. I'd like to display the combox as soon as the table shows up, like the setCellWidget, And need to be able to popup when I click the arrow part of the combobox. So it's hard to acomplish this. I've already read half the documentation ,which seems there is no complex example of it. It seems I won't be able to find the solution directly.
                    Currently ,I have used the following code in the delegate's paint function, it can paint a combox and show, but the popup menu won't show up. There need more handling which I haven't find the way yet. I think I have to handle the key event in the delegate, which I think will be very complex.

                        QStyleOptionComboBox comboBoxOption;
                    
                        comboBoxOption.rect = option.rect;
                        comboBoxOption.state = option.state;
                        comboBoxOption.state |= QStyle::State_Enabled;
                        comboBoxOption.editable = false;
                        comboBoxOption.popupRect = option.rect;
                        comboBoxOption.popupRect.setHeight(option.rect.height());
                        comboBoxOption.currentText = "CCC"; // This doesn't show up.
                        QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
                        QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
                    
                    VRoninV 1 Reply Last reply
                    0
                    • A ailika

                      @mrjj Hi, I've already used this mode, and I need to at least single click in order to enter edit state, which I don't like. I'd like to display the combox as soon as the table shows up, like the setCellWidget, And need to be able to popup when I click the arrow part of the combobox. So it's hard to acomplish this. I've already read half the documentation ,which seems there is no complex example of it. It seems I won't be able to find the solution directly.
                      Currently ,I have used the following code in the delegate's paint function, it can paint a combox and show, but the popup menu won't show up. There need more handling which I haven't find the way yet. I think I have to handle the key event in the delegate, which I think will be very complex.

                          QStyleOptionComboBox comboBoxOption;
                      
                          comboBoxOption.rect = option.rect;
                          comboBoxOption.state = option.state;
                          comboBoxOption.state |= QStyle::State_Enabled;
                          comboBoxOption.editable = false;
                          comboBoxOption.popupRect = option.rect;
                          comboBoxOption.popupRect.setHeight(option.rect.height());
                          comboBoxOption.currentText = "CCC"; // This doesn't show up.
                          QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
                          QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
                      
                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #28

                      @ailika said in How to use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?:

                      So it's hard to acomplish this. I've already read half the documentation ,which seems there is no complex example of it. It seems I won't be able to find the solution directly.

                      It's not that hard, have a look at https://forum.qt.io/post/422423

                      need to be able to popup when I click the arrow part of the combobox

                      This is straightforward, at the end of ComboBoxDelegate::setEditorData call qobject_cast<QComboBox*>(editor)->showPopup() and make sure QAbstractItemView::CurrentChanged is set as an edit trigger

                      "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

                      A 2 Replies Last reply
                      2
                      • VRoninV VRonin

                        @ailika said in How to use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?:

                        So it's hard to acomplish this. I've already read half the documentation ,which seems there is no complex example of it. It seems I won't be able to find the solution directly.

                        It's not that hard, have a look at https://forum.qt.io/post/422423

                        need to be able to popup when I click the arrow part of the combobox

                        This is straightforward, at the end of ComboBoxDelegate::setEditorData call qobject_cast<QComboBox*>(editor)->showPopup() and make sure QAbstractItemView::CurrentChanged is set as an edit trigger

                        A Offline
                        A Offline
                        ailika
                        wrote on last edited by ailika
                        #29

                        @VRonin Thank you very much. A simple function 'openPersistentEditor' will do! (Because I have already generate the compbox/spinbox, the question is how to display it on creation of the table). But I just didn't know there was such a function!!! I thought it would be very non-sense to draw a combox/spinbox on my self since the system alreay knows how to draw it.

                        But as you said, it may still generate lot of combox object, which consumes lots of system resources. Your method still genterate QCombox when editing. Though I can set cbOption.currentText = "..." to display the initial value, it still has problem when I double click(it first become blank if I do not select anything). And the
                        'openPersistentEditor' method seems working well, but it need to generate all the combox at once.
                        I think there is some method that only use paint and mouse event and never generate combox at all. Maybe I need to look into the source code of combox. But currently I can't see the source code. And it is said the source code can't be seen after QT 5, do I need to install QT 4 instead? Can I install it without interfering the current installed QT 5?

                        1 Reply Last reply
                        0
                        • VRoninV VRonin

                          @ailika said in How to use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?:

                          So it's hard to acomplish this. I've already read half the documentation ,which seems there is no complex example of it. It seems I won't be able to find the solution directly.

                          It's not that hard, have a look at https://forum.qt.io/post/422423

                          need to be able to popup when I click the arrow part of the combobox

                          This is straightforward, at the end of ComboBoxDelegate::setEditorData call qobject_cast<QComboBox*>(editor)->showPopup() and make sure QAbstractItemView::CurrentChanged is set as an edit trigger

                          A Offline
                          A Offline
                          ailika
                          wrote on last edited by
                          #30

                          @VRonin I'm sorry, but when the combox is created in the createEditor function, the object is generated in the function which means that it will delete when the function end, am I right? I'm not very familiar with this new/delete thing, but I think it will delete an object automatically when the function is over.
                          If it is this way, there will not be too much consumption by creating combox.
                          If I use openPersistentEditor, the combox will always preserve, I presume?

                          C VRoninV 2 Replies Last reply
                          0
                          • A ailika

                            @VRonin I'm sorry, but when the combox is created in the createEditor function, the object is generated in the function which means that it will delete when the function end, am I right? I'm not very familiar with this new/delete thing, but I think it will delete an object automatically when the function is over.
                            If it is this way, there will not be too much consumption by creating combox.
                            If I use openPersistentEditor, the combox will always preserve, I presume?

                            C Offline
                            C Offline
                            CEO.
                            wrote on last edited by CEO.
                            #31
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • A ailika

                              @VRonin I'm sorry, but when the combox is created in the createEditor function, the object is generated in the function which means that it will delete when the function end, am I right? I'm not very familiar with this new/delete thing, but I think it will delete an object automatically when the function is over.
                              If it is this way, there will not be too much consumption by creating combox.
                              If I use openPersistentEditor, the combox will always preserve, I presume?

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #32

                              @ailika said in How to use QTableWidget::setCellWidget() to set a widget such as a QComboBox and do the further handling?:

                              the object is generated in the function which means that it will delete when the function end, am I right?

                              Nope, it's allocated on the heap so it will live untill someone calls delete on it

                              If it is this way, there will not be too much consumption by creating combox.

                              No, this is the method chosen by the Qt architects to save on resources

                              If I use openPersistentEditor, the combox will always preserve, I presume?

                              Yes but you will have the same performance issues you would have with setIndexWidget

                              "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

                              • Login

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