Matt
Matt

Reputation: 5567

Advice on how to use Qt Model View classes

So I have been writing a Sudoku game in C++. I have most of the game logic done and tested, but I wanted to use Qt on top of it for the GUI. I was trying to figure out the best way to work with the Qt classes for my needs.

As a test, I played around with QAbstractTableModel. I subclassed it and had it access my existing data model and my existing controllers. For now I am using QTableView to get basic rendering of the Sudoku board and basic "editing" (you can just change any value). It looks nothing like what I want, but the functionality is all there (or can be added).

I wanted to make a data model and controllers to modify it all in C++, without depending on a framework. Then I wanted to just have Qt sit on top. So I have this working, and here is a quick "diagram" of how these things communicate at a high level

QTableView?
    ^
    |
    v
PuzzleModel : QAbstractTableModel
             ^              |
             |              |_____________
             |                            v
Real data model classes <------------ Controllers

My question is, how can I modify QTableView or should I create my own view or QWidget in order to display the data the way I want?

Ideally, I'd like to display a fixed size table (no headers, no resizing), and disallow multi-selecting. There are some customizations on how I'd render various font styles/colors, but I think I can handle that pretty easily. I'd also like to render each cell as either a number, or like this for "marks":

*-------------*         *-------------*
|  1   2   3  |         |   ******    |
|  4       6  |         |        *    |
|      8   9  |         |        *    |
*-------------*         *-------------*

So clearly I can't continue using QTableView out of the box. Do I create my own QStyledItemDelegate and still use QTableView? Do I need to create a whole Widget? If I create

Just looking for some advice/direction from someone who knows the capabilities of the various Qt classes.

Upvotes: 3

Views: 1394

Answers (1)

Marc Mutz - mmutz
Marc Mutz - mmutz

Reputation: 25283

You have two options:

  1. Continue to use QTableView and your QAbstractTableModel, and subclass QStyledItemDelegate to render the cells exactly how you want them. You can't change the inter-cell painting that way, though.

    Note, however, that you can achieve a lot of what you want (fonts, colors) by reacting to more Qt::ItemDataRoles from your model's data() implementation.

  2. Write a custom widget and use a custom data provider interface. Let me stress that: don't continue to use QAbstractTableModel when you implement your own SudokuWidget. It's much simpler for everyone that way (QAbstractItemModel is both way too abstract and way too specialised for the case of item views to be useful as a general data provider interface).

My advice is to go with (2). Interview is overrated.

Upvotes: 4

Related Questions