关于如何使用 Qt 模型视图类的建议
所以我一直在用C++编写一个数独游戏。我已经完成并测试了大部分游戏逻辑,但我想在其之上使用 Qt 作为 GUI。我试图找出使用 Qt 类来满足我的需求的最佳方法。
作为测试,我使用了 QAbstractTableModel。我对它进行了子类化,并让它访问我现有的数据模型和现有的控制器。现在我使用 QTableView 来获得数独板的基本渲染和基本“编辑”(您可以更改任何值)。它看起来一点也不像我想要的,但功能已经全部有了(或者可以添加)。
我想制作一个数据模型和控制器来用 C++ 修改它,而不依赖于框架。然后我想让 Qt 坐在上面。所以我已经完成了这个工作,这里是这些东西如何在高层通信的快速“图表”
QTableView?
^
|
v
PuzzleModel : QAbstractTableModel
^ |
| |_____________
| v
Real data model classes <------------ Controllers
我的问题是,我如何修改 QTableView 或者我应该创建我自己的视图或 QWidget 以便以我的方式显示数据想?
理想情况下,我想显示一个固定大小的表格(没有标题,没有调整大小),并且不允许多重选择。关于如何呈现各种字体样式/颜色有一些自定义,但我认为我可以很容易地处理它。我还想将每个单元格渲染为数字,或者像这样的“标记”:
*-------------* *-------------*
| 1 2 3 | | ****** |
| 4 6 | | * |
| 8 9 | | * |
*-------------* *-------------*
很明显,我不能继续使用开箱即用的 QTableView。我是否创建自己的 QStyledItemDelegate 并仍然使用 QTableView?我需要创建一个完整的小部件吗?如果我创建
只是向了解各种 Qt 类功能的人寻求一些建议/方向。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两个选择:
继续使用
QTableView
和QAbstractTableModel
,并子类QStyledItemDelegate
来渲染单元格 正是您想要的。不过,您无法以这种方式更改单元格间的绘制。但是请注意,您可以通过对更多内容做出反应来实现很多您想要的东西(字体、颜色)
Qt::ItemDataRole
来自模型的data()
实现。编写自定义小部件并使用自定义数据提供程序接口。我要强调的是:当您实现自己的
SudokuWidget
时,不要继续使用QAbstractTableModel
。这样对每个人来说都简单得多(对于项目视图的情况来说,QAbstractItemModel
既太抽象又太专业,无法用作通用数据提供程序接口)。我的建议是选择(2)。 采访被高估了。
You have two options:
Continue to use
QTableView
and yourQAbstractTableModel
, and subclassQStyledItemDelegate
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::ItemDataRole
s from your model'sdata()
implementation.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 ownSudokuWidget
. 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.