Important: Please read the Qt Code of Conduct -

Boolean Display in QTableView [Solved]

  • I'm using a QTableView to display data in a QSqlRelationalTableModel from two related database tables. One of the tables contains a boolean field. I'd like this to display the word "True" or "False" rather than 1 or 0. The RDBMS is MySQL which stores booleans as tiny integers.

    I know I could get around this by altering the database to use a string field rather than a boolean field and just store "true" and "false" but that seems wasteful.

    How would I go about this?

  • I'm not sure what the recommended way would be, but I can think of two straight-forward ways off the top of my head.

    Create an inherited model and override the QAbstractItemModel::data() function. Have it return "True" or "False" for the Qt::DisplayRole of the column in question.

    Create a custom QItemDelegate for that column/table which renders the boolean field to "True" or "False". You can assign it with QAbstractItemView::setItemDelegateForColumn() or QAbstractItemView::setItemDelegate().

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    A third way would be to use a "QIdentityProxyModel": in a similar fashion as 1. suggested by dkruger

    Hope it helps

  • I went the QItemDelegate solution. Mostly because I could wrap my head around it the best. Thanks for the suggestions.

  • Lifetime Qt Champion

    You're welcome !

    Don't forget to update the thread's title to solved so other forum users will know that it's all good :)

Log in to reply