简体   繁体   中英

How can I index a QTableWidget by field name?

I'm bug fixing someone else's Qt code, and this involves adding some extra columns to a QTableWidget. The current code indexes columns by their integer index. I would like to change this code so that it indexes by the header name. The code at the moment looks a bit like this:

// column 1 => Semantics
// column 2 => algorithm
// colunm 3 => ...
// ...

tableWidget->setItemDelegateForColumn(0, semanticsDelegate);
tableWidget->setItemDelegateForColumn(1, algorithmsDelegate);
tableWidget->setItemDelegateForColumn(2, specDelegate);

I would like to change it to something like this:

tableWidget->setItemDelegateForColumn(foo("Semantics"), semanticsDelegate);
tableWidget->setItemDelegateForColumn(foo("Algorithm"), algorithmsDelegate);
tableWidget->setItemDelegateForColumn(foo("..."), specDelegate);

Is this possible in Qt? (The QTableWidget columns are titled "Semantics", "Algorithm", etc) Or will I have to make an enum of table fields to handle this?

Item delegates are indexed by an int so, if you want to proceede in that way, you must to provide a function to map the column header to an unique integer. How to implement that foo() function is up to you - an enum and a QList< QPair<QString,enum_item> > should be good for you - . If your app will be translated, don't forget to use Qt's i18n facilities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM