【发布时间】:2020-08-07 09:41:45
【问题描述】:
我试图让我的第一个 QML TableView 在 Qt 5.2 中工作(因为我们坚持
现在工作的版本)在后端使用QAbstractTableModel。
我的主要问题是,由于某种原因,itemDelegate 永远不会触发,所以
除了TableView 的轮廓外,我从未在视图中看到任何东西。
我还验证了theData_ 填充了二维数字
在构造函数的每一行/列中,我执行emit layoutChanged()
以及构造函数中的emit dataChanged()。
我意识到我在data() 调用中没有错误检查无效的QModelIndex
此时。
我也根本没有实现index()。
还有没有必要在这里使用ROLE?
我显示的数据是每个单元格的单个整数(QString),目前仅此而已。
感谢您的帮助
qml:
TableView {
width: 600
height: 600
model: myModel
visible: true
itemDelegate: Rectangle {
color: "lightgray"
width: 100
height: 20
Text {
text: styleData.value
color: "black"
}
}
}
来自子类 QAbstractTableModel 的相关代码:
int MyModel::rowCount(const QModelIndex&) const
{
return 10;
}
int MyModel::columnCount(const QModelIndex&) const
{
return 3;
}
QVariant MyModel::data(const QModelIndex& index, int role) const
{
const int row = index.row();
const int col = index.column();
return QString("%1").arg(this->theData_[col][row]);
}
【问题讨论】:
标签: qt qml tableview qitemdelegate