问题标签 [qabstracttablemodel]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - QAbstractTableModel 检索数据更改的自定义对象
我最近再次拿起了Qt,并开始刷新我的记忆。为表创建自定义数据模型非常简单。
现在我正在尝试检索选定的数据。请注意,我使用自定义数据对象。
我的自定义模型示例:
平台.h
用于测试目的的非常简单的数据结构。然后我实现了一个 QAbstractTableModel,Data() 方法如下所示:
平台模型.cpp
我从这段代码中了解到,对于可选择的项目,总是返回一个字符串,而不是一个平台对象。
为了显示,这很好用,我在视图中看到了实际的对象。现在我想从模型中选择实际的对象,而不仅仅是一个 QString。
所以方法体应该是这样的:
Ps 也许我试图搜索错误的东西,我可以找到使用自定义对象的示例,但没有人谈论检索所选项目。
必须有一种更好的方法,然后检索字符串,然后遍历平台列表并将名称与所选项目进行比较。如果我有一个大列表,则必须遍历每个项目并进行字符串比较不是很有效.
我希望我的问题足够清楚。如果缺少重要的东西,请告诉我,以便我可以编辑我的示例。
编辑
我试过 Q_DECLARE_METATYPE(Platform);
是的,它可以工作,它可以将它存储在 QVariant 中,问题是,因为为了显示,总是需要一个字符串,或者无论如何都要 9/10 次。到目前为止,似乎不可能同时显示文本并从选择模型中获取完整的平台对象(我可以单独做这两个.. 非常没用..)
qt - 如何在 QAbstractTableModel::data() 中正确访问 QMap
我得到了一个带有标识符和相应对象的 QMap。子类化 QAbstractTableModel::data() 时,您将分别获得带有行和列的 QModelIndex。每行应该代表一个对象(QAbstractTableModel::rowCount() 是 myMap->size())。
通过以下方式获取当前对象是否合法
由于(未排序的)地图的标识符被绕过了,这是否意味着(排序、插入)?我的意思是对于 QAbstractTableModel::setData() 我需要绕过相同的地图标识符?!谢谢。
qt - QDataWidgetMapper 和 QAbstractTableModel
我有一个QAbstractTableModel
由QTableView
. 该表有 2 列和几行。每个单元格是一个QLineEdit
. 我的问题是是否有一种方法可以QDataWidgetMapper::addMapping
与我的自定义模型(用于QLineEdit
单元格)一起使用。
c++ - QTableView sorting updates slowly
I have a QTableView
with a QAbstractTableModel
child instance set as the model. The child implements void sort(int columnt, Qt::SortOrder order)
. I set QTableView::sortEnabled(bool)
to true
so I can sort my table by clicking on the column header. When I click the header sorting finishes almost instantly but the table is only updated when I move the mouse to the table area. Do I need to emit some signal from sort()
? Or call a function to update the QTableView
?
c++ - Qt删除QTableView中的选定行
delete
单击按钮时,我想从表中删除选定的行。
但是我在 Qt 文档中找不到有关删除行的任何内容。有任何想法吗?
qt - 在 qt 的 QTableView 列中添加具有两种不同颜色的文本
我正在使用 QTableView 在我的应用程序中显示用户活动。
我的模型包含四列,每列显示不同类型的文本,例如:
- 第一列中的用户名
- 第二列中的用户活动
- 第三栏活动详情
我想在单列中显示第二列和第三列的文本,并用“-”分隔不同的文本颜色。
我的应用程序目前看起来像这样:
但是,我希望它看起来与此类似
有人可以帮我解决这个问题吗,在此先感谢
c++ - Use QAbstractTableModel implementation with a QList of custom classes
I have a general question about the implementation of the underlying data source of a QAbstractTableModel
.
The QAbstractTableModel::data()
function access the data sources content by an index consisting of a row and a column value.
If my underlying data source is a QList
of Person
classes, where each member represents a column, how do I access its members with a given column index?
The only approach I can think of is that I use a kind of mapping, that maps a column number to a member of the Person
class:
Is this the approach to tackle this problem or is there a better one? If my class has 50 members that would be a lot of work to do. The same thing must be done, when I write data to the source via QAbstractTableModel::setData()
.
Answers or links to material that would help me to understand this part of the model/view implementation in Qt are very much appriciated.
c++ - QTableView如何设置行高?
我有QTableView
和QAbstractTableModel
。我要求行的高度等于 24。我知道这样做的唯一方法是调用QTableView::setRowHeight
. 由于模型是动态的,它可能会添加新行,但我不想在setRowHeight
每次添加新行时都调用。
如何配置QTableView
使其对新添加的行使用相同的高度,或者可以向模型发送行的高度?
qt - 在 QAbstractTableModel 中对货币格式的数字进行排序
我有一个实现 data() 的 QAbstractTableModel 子类,其中一些列是整数和双精度。我正在使用 QLocale::toCurrencyString() 和 QLocale::toString() 根据语言环境转换这些数字,以添加诸如“$”和“,”之类的格式字符。由于结果是字符串,因此列现在以字符串形式而不是 int 或 double 形式对值进行排序。
我正在使用 QSortFilterProxyModel 根据列进行排序。但它只是以字符串形式获取数据,并以此为基础进行排序。也许我不应该在 data() 中格式化整数和双精度数?我找不到其他地方来格式化这些值。有谁知道如何解决这个问题?
qt - QTableView::edit( const QModelIndex &index ) 失败
我试图阻止用户将相同的数据输入到我的模型中,该模型是QAbstractTableModel的子类。
现在我应该捕捉到信号并将我的表格视图(类型为QTableView)转回编辑状态:
但是当我运行我的应用程序时,我在控制台中收到了消息,并且QTableView没有进入编辑状态:
我究竟做错了什么?非常感谢您提前。