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.
  • A ailika

    I'm sorry I'm not familiar with advanced usage of c++, such as lamba. So I don't understand the the code.
    Do you mean when I use connect, I can set a fixed inforation and when I receive the signal in the slot function I can get the information? If it is so, it is a better method than subclassing the combo box. But currently it seems I can just use subclassing method. Learning lamba is not a prority.
    In fact, I'm a new QT learner also. I have just read a book and haven't done much practice.

    I'm very confused with my data presentation. It seems there are several ways, it seems hard to choose.
    If I use standard QTableWidget, I think I need to process the data in the parent widget, don't I? So I need to know the row and column badly. This is the simple way, but it seems not so "tidy".
    If I use the model/view structure, I can process the date in the self-defined model, maybe I don't need this information that much. But the model/view structure is very complicated.
    So what's the better way to present the data?

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

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

    I'm sorry I'm not familiar with advanced usage of c++, such as lamba. So I don't understand the the code.

    I am sorry of you don't understand. All you should have to do is copy that code, provided it's correct, which I think it is. And read up on C++ lambdas if you want to understand it.

    So I need to know the row and column badly.

    If this is indeed true then you should be using the lambda as the only way to pass extra parameters to the slot I showed you. That's what's required to send the extra row/column information.

    Otherwise you're on your own. If you want to do it with your sub-classing that's up to you.

    A 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #8

      Don't reinvent the wheel, setCellWidget is the wrong tool for the job (as it is in 99.9% of the cases). You should use a delegate, have a look at https://wiki.qt.io/Combo_Boxes_in_Item_Views

      "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 1 Reply Last reply
      4
      • JonBJ JonB

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

        I'm sorry I'm not familiar with advanced usage of c++, such as lamba. So I don't understand the the code.

        I am sorry of you don't understand. All you should have to do is copy that code, provided it's correct, which I think it is. And read up on C++ lambdas if you want to understand it.

        So I need to know the row and column badly.

        If this is indeed true then you should be using the lambda as the only way to pass extra parameters to the slot I showed you. That's what's required to send the extra row/column information.

        Otherwise you're on your own. If you want to do it with your sub-classing that's up to you.

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

        @JonB Thank you very much. I think I will learn lamba later. I just want to focus on the table widget now

        1 Reply Last reply
        0
        • VRoninV VRonin

          Don't reinvent the wheel, setCellWidget is the wrong tool for the job (as it is in 99.9% of the cases). You should use a delegate, have a look at https://wiki.qt.io/Combo_Boxes_in_Item_Views

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

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

          Views
          I m leaning the table widget. Firstly , I use the sellWidget to try it out, since it's very simple. After this, I will try to use the delegate model view method, and see which one suits most. I think its the most complicate part in QT.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ailika
            wrote on last edited by
            #11

            I tried this method, it works. But there is a problem: When the table shows up, the combox doesn't show, I have to double click the item then the combox shows. I have tried spinbox, it does not show up automatically too. How to fixe this problem?

            JonBJ 1 Reply Last reply
            0
            • A ailika

              I tried this method, it works. But there is a problem: When the table shows up, the combox doesn't show, I have to double click the item then the combox shows. I have tried spinbox, it does not show up automatically too. How to fixe this problem?

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

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

              I have to double click the item then the combox shows.

              Have a look at your setting for editTriggers. If your behaviour is QAbstractItemView::DoubleClicked only you can change that.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                ailika
                wrote on last edited by
                #13

                But even if I use QAbstractItemView::AllEditTriggers, I need to single-click to make the item show.

                JonBJ 1 Reply Last reply
                0
                • A ailika

                  But even if I use QAbstractItemView::AllEditTriggers, I need to single-click to make the item show.

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

                  @ailika
                  That is correct. The code you copied which @VRonin referred you to produces a combobox only when the user wishes to edit a cell. Otherwise the cell just shows the current value. Little point in showing a combobox when you're not editing something. When you have 10,000 rows in your table view @VRonin is trying to save you from having 10,000 comboboxes present all the time, which is "expensive/inefficient".

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    ailika
                    wrote on last edited by
                    #15

                    I haven't think of this problem. If I have 10000 combox, I will have to create 10000 objects which will "swallow" the RAM. This indeed is a problem. So this is his really purpose, not a defect?

                    In fact, I don't use the table to do some data process which may contain thousands of data. I need only 250 rows and need it to be displayed.
                    Currently, I think the createEdit method can't work. I think I should use 'QStyleOptionComboBox' instead, am I right? But this really difficult, I haven't seen any complete example in the network. Currently I can only display a text in the combox item, but it can't popup. It seems that I should study QStyle function first and see whether I can solve my problem.

                    JonBJ 1 Reply Last reply
                    0
                    • A ailika

                      I haven't think of this problem. If I have 10000 combox, I will have to create 10000 objects which will "swallow" the RAM. This indeed is a problem. So this is his really purpose, not a defect?

                      In fact, I don't use the table to do some data process which may contain thousands of data. I need only 250 rows and need it to be displayed.
                      Currently, I think the createEdit method can't work. I think I should use 'QStyleOptionComboBox' instead, am I right? But this really difficult, I haven't seen any complete example in the network. Currently I can only display a text in the combox item, but it can't popup. It seems that I should study QStyle function first and see whether I can solve my problem.

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

                      @ailika
                      Even 250 comboboxes is quite a lot.

                      Anyway, I don't know what doesn't work for you. If you follow the code in @VRonin's Combo Boxes in Item Views you should find:

                      1. Initially all items are displayed with their current values, no comboboxes.
                      2. Then you click or double-click in a cell and it turns into a combobox.
                      3. Then you click on the combobox to get the popup and do the editing.
                      4. Then you click away or whatever and it reverts to the text value.
                      A 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @ailika
                        Even 250 comboboxes is quite a lot.

                        Anyway, I don't know what doesn't work for you. If you follow the code in @VRonin's Combo Boxes in Item Views you should find:

                        1. Initially all items are displayed with their current values, no comboboxes.
                        2. Then you click or double-click in a cell and it turns into a combobox.
                        3. Then you click on the combobox to get the popup and do the editing.
                        4. Then you click away or whatever and it reverts to the text value.
                        A Offline
                        A Offline
                        ailika
                        wrote on last edited by
                        #17

                        @JonB
                        250is quite alot? I have seen other similar application who has 250 combox. But they ard made by MFC and c#. so it's not unreasonable quantity.
                        I have used the code and it works like you said.
                        But what I need is to to display them initially not display on edit. The display on edit way just don't fit into the application. If I cant solve it even by QStyle, I will use the setCellWidget way or just do not use the combobox. Just use a dialog to edit the date and use the table to dislpay text(the dialog is destined to be used what ever method I use.)

                        JonBJ 1 Reply Last reply
                        0
                        • A ailika

                          @JonB
                          250is quite alot? I have seen other similar application who has 250 combox. But they ard made by MFC and c#. so it's not unreasonable quantity.
                          I have used the code and it works like you said.
                          But what I need is to to display them initially not display on edit. The display on edit way just don't fit into the application. If I cant solve it even by QStyle, I will use the setCellWidget way or just do not use the combobox. Just use a dialog to edit the date and use the table to dislpay text(the dialog is destined to be used what ever method I use.)

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

                          @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 2 Replies 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
                            #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

                                          • Login

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