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. each cell in QStandardItemModel has a unique delegate
QtWS25 Last Chance

each cell in QStandardItemModel has a unique delegate

Scheduled Pinned Locked Moved General and Desktop
qt4
9 Posts 4 Posters 3.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.
  • F Offline
    F Offline
    fisher103
    wrote on last edited by
    #1

    I am use QStandardItemModel to generate a M*N table, I know each row or column can share a delegate with setItemDelegateForColumn/Row but with same contents such as a Combox. the issues is each cell can have a unique combobox?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What do you mean by a unique combo box by cell ?

      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
      0
      • F Offline
        F Offline
        fisher103
        wrote on last edited by fisher103
        #3

        I want one column which has M cells in the M*N table to have a unique combo box to make choice. by default we can use setItemDelegateForColumn to set a combo box delegate for the column in the QStandardItemModel table, but all cell will use the same delegate with the same choices.
        for example, what i need is like this--

        • (i, j) has combo box with items ('A', 'B', 'C')

        • (i, j+1) has combo box with items ('A', 'B')

        • (i, j+1) has combo box with items('X','Y')

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok, then what defines the content of the combo box ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          F 1 Reply Last reply
          0
          • SGaistS SGaist

            Ok, then what defines the content of the combo box ?

            F Offline
            F Offline
            fisher103
            wrote on last edited by
            #5

            @SGaist
            content can be set static when initialized, can you give any suggestions or show me an example?
            the key problem of my issue are:

            • QStandardItemModel can make a M*N table with each cell presented by a QStandardItem

            • QStandardItem can edit or make a delegate with its parent QStandardItemModel

            • I want each cell have a unique Combo box with different choices, but delegate can only be set for a row of cells or a column of cells with all choices the same.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by alex_malyu
              #6

              You do not need to set delegate to every cell.
              One delegate set for whole table can create unique combobox for every cell.

              createEditor recieves index which also defines cell for which you need to create a widget (your combobox for example):
              QWidget * QAbstractItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const [virtual]

              F 1 Reply Last reply
              1
              • M Offline
                M Offline
                motormike
                wrote on last edited by motormike
                #7

                I agree with alex_malyu, I don't think you need multiple delegates.

                My initial approach would be to think about using the Roles to get the list of valid combo box values for each cell. So when you create the data use setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole). You might have to use a UserRole (>=32). Pass in the actual value with DisplayRole. Then pass in a QList of valid values stored in a QVariant. In the delegate editor function you can then request the actual value using DisplayRole and then the list of values by calling QModelIndex::data(int role = Qt::DisplayRole) const. You can then create your combo box from that information.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  +1 for @alex_malyu and @motormike

                  However, you should use Qt::UserRole + X in your enum to ensure you don't clash with roles reserved or already taken

                  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
                  0
                  • A alex_malyu

                    You do not need to set delegate to every cell.
                    One delegate set for whole table can create unique combobox for every cell.

                    createEditor recieves index which also defines cell for which you need to create a widget (your combobox for example):
                    QWidget * QAbstractItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const [virtual]

                    F Offline
                    F Offline
                    fisher103
                    wrote on last edited by
                    #9

                    @alex_malyu thanks for your suggestion to solve my issue.

                    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