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. QItemDelegate sizeHint() with QComboBox
QtWS25 Last Chance

QItemDelegate sizeHint() with QComboBox

Scheduled Pinned Locked Moved General and Desktop
qitemdelegate
7 Posts 3 Posters 5.2k 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
    Joel Bodenmann
    wrote on 2 Sept 2015, 12:24 last edited by
    #1

    I have a custom QItemDelegate class which uses a QComboBox as an editor. QComoboBox provides a method sizeHint() which returns the size hint to display all the items in the list so the layout won't change when showing the dropdown. Therefore, I would like to make my QItemDelegate::sizeHint() to return the QComboBox::sizeHint().
    However, outside of QItemDelegate::setEditorData() and QItemDelegate::setModelData() I cannot access the QComboBox as I don't get the QWidget* editor parameter.

    How can I fix this? I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column. Any hints? Is there no way to access the editor inside of QComboBox::sizeHint()?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on 2 Sept 2015, 22:57 last edited by
      #2

      You basically answered yourself.
      Normally editors are created and deleted of the fly. If you want to use editor's sizeHint() you should make it persistent and created before QItemDelegate::sizeHint() is called.

      I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column.

      And how this makes it bad ?

      J 1 Reply Last reply 3 Sept 2015, 06:53
      0
      • A alex_malyu
        2 Sept 2015, 22:57

        You basically answered yourself.
        Normally editors are created and deleted of the fly. If you want to use editor's sizeHint() you should make it persistent and created before QItemDelegate::sizeHint() is called.

        I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column.

        And how this makes it bad ?

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 3 Sept 2015, 06:53 last edited by Joel Bodenmann 9 Mar 2015, 07:48
        #3

        @alex_malyu So I am supposed to make the editor become a private member of my custom item delegate class?
        I assume(d) that there is a pretty good reason why I get a widget pointer for the editor in the setEditorData() and setModelData() methods.

        I though that making the combobox editor become a private member of the item delegate is a bad idea because then each row of the column would use the same combobox. As I understood the purpose of createEditor() is that there is one editor per row when I set the delegate for an entire column in my tableview.

        When I make the editor become a private member, will it still just show up when the user double clicked the item and vanish automatically after the edit has been completed?

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        A 1 Reply Last reply 3 Sept 2015, 23:58
        0
        • J Joel Bodenmann
          3 Sept 2015, 06:53

          @alex_malyu So I am supposed to make the editor become a private member of my custom item delegate class?
          I assume(d) that there is a pretty good reason why I get a widget pointer for the editor in the setEditorData() and setModelData() methods.

          I though that making the combobox editor become a private member of the item delegate is a bad idea because then each row of the column would use the same combobox. As I understood the purpose of createEditor() is that there is one editor per row when I set the delegate for an entire column in my tableview.

          When I make the editor become a private member, will it still just show up when the user double clicked the item and vanish automatically after the edit has been completed?

          A Offline
          A Offline
          alex_malyu
          wrote on 3 Sept 2015, 23:58 last edited by alex_malyu 9 Apr 2015, 00:06
          #4

          @Joel-Bodenmann

          As far as I understand, if you need a persistent editor you need to subclass an item view.
          Problem with delegate is - that you do not have any way to prevent editor to be deleted.
          If you make editor private member of itemview subclass and to override 2 functions below it should do it.

          bool QAbstractItemView::edit ( const QModelIndex & index, EditTrigger trigger, QEvent * event ) [virtual protected]
          void QAbstractItemView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint ) [virtual protected slot]

          You also might need to write additional resizing/positioning code so editor is drawn properly,
          I believe I've done something like this, but it was long time ago and may be even done with Qt 3 , so I can't be sure I can provide enough details.

          J 1 Reply Last reply 5 Sept 2015, 10:01
          0
          • A alex_malyu
            3 Sept 2015, 23:58

            @Joel-Bodenmann

            As far as I understand, if you need a persistent editor you need to subclass an item view.
            Problem with delegate is - that you do not have any way to prevent editor to be deleted.
            If you make editor private member of itemview subclass and to override 2 functions below it should do it.

            bool QAbstractItemView::edit ( const QModelIndex & index, EditTrigger trigger, QEvent * event ) [virtual protected]
            void QAbstractItemView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint ) [virtual protected slot]

            You also might need to write additional resizing/positioning code so editor is drawn properly,
            I believe I've done something like this, but it was long time ago and may be even done with Qt 3 , so I can't be sure I can provide enough details.

            J Offline
            J Offline
            Joel Bodenmann
            wrote on 5 Sept 2015, 10:01 last edited by
            #5

            @alex_malyu Thank you for your reply. I will give this a try in the following days.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Harb
              wrote on 5 Sept 2015, 20:55 last edited by Harb 9 May 2015, 20:57
              #6

              @Joel-Bodenmann said:

              How can I fix this? I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column. Any hints? Is there no way to access the editor inside of QComboBox::sizeHint()?

              Hi!
              Hm... the easiest solution which came to me is just create ComboBox instance inside delegate sizeHint function, call combobox size hint function and return it : ). ComboBox will be destroyed automatically at the end of delegate sizeHint function.

              But frankly, i didn't get your idea about ComboBox sizeHint. For example I also have comboboxes as editors for my delegates, I didn't implement sizeHint at all, and it still work quite well.

              J 1 Reply Last reply 6 Sept 2015, 14:06
              0
              • H Harb
                5 Sept 2015, 20:55

                @Joel-Bodenmann said:

                How can I fix this? I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column. Any hints? Is there no way to access the editor inside of QComboBox::sizeHint()?

                Hi!
                Hm... the easiest solution which came to me is just create ComboBox instance inside delegate sizeHint function, call combobox size hint function and return it : ). ComboBox will be destroyed automatically at the end of delegate sizeHint function.

                But frankly, i didn't get your idea about ComboBox sizeHint. For example I also have comboboxes as editors for my delegates, I didn't implement sizeHint at all, and it still work quite well.

                J Offline
                J Offline
                Joel Bodenmann
                wrote on 6 Sept 2015, 14:06 last edited by
                #7

                @Harb When I understood correctly a column of a QTableView which has a delegate assigned will automatically resize itself to the QItemDelegate::sizeHint() size. Therefore, I could return the required width of the column to display the full text in the cell, right?

                My custom delegate uses a QComboBox. The combobox is populated with items of different length. Now, let's assume there is just one row in the table and the item in the cell with the custom delegate is the item in the combobox with the shortest width. Therefore, the column will be just wide enough to display that text. When the user then clicks on the cell and the combobox dropdown shows up the user will not be able to see entries in the combobox which are longer. Items in the combobox which are longer than the width of the column will be shorted and three dots will be inserted.

                I took a screenshot so you can see what I mean: http://paste.ugfx.org/sores/7505d64a54e0/bcdde374e0fc.jpg
                As you can see there is one item in the dropdown which cannot be displayed in full length because the column width is too small.

                What I would like to do is using the QItemDelegate::sizeHint()to return the QComboBox::sizeHint() so the column will automatically be wide enough to display all items (because the QComboBox::sizeHint() returns the size so all items can be displayed.

                Industrial process automation software: https://simulton.com
                Embedded Graphics & GUI library: https://ugfx.io

                1 Reply Last reply
                0

                2/7

                2 Sept 2015, 22:57

                5 unread
                • Login

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