each cell in QStandardItemModel has a unique delegate



  • 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?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • 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')


  • Lifetime Qt Champion

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



  • @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.



  • 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]



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


  • Lifetime Qt Champion

    +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



  • @alex_malyu thanks for your suggestion to solve my issue.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.