我正在使用 QSqlQueryModel 从限制查询中选择进行简单分页
一切正常。但。现在我想反射(reflect) QTableView 的 Vertical headerData。
我正在实现 headerData,因为它是 const 函数,我无法在其中进行任何计算。所以我在计算垂直标题中的正确数字时遇到问题。例如,我正在获取从 20 到 30 的行。
我喜欢垂直标题显示数字 20 到 30。等等...
这就是我实现 headerData 的方式:
QVariant PlayListSqlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Vertical && role == Qt::DisplayRole)
{
return section;
}
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QString("name");
case 1:
return QString("From");
case 2:
return QString("Created Time");
case 3:
return QString("last name");
case 4:
}
}
}
return QVariant();
}
更新:
我什至尝试调用执行计算的 const 函数,但我仍然对新的 const 函数有编译错误:
error C2166: 左值指定 const 对象
int PlayListSqlModel::calculateVerticalHeader() const
{
int returnHeaderCount = m_iHeaderCount;
m_iHeaderCount++;
return returnHeaderCount;
}
QVariant PlayListSqlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Vertical && role == Qt::DisplayRole)
{
return calculateVerticalHeader();
}
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QString("Clip");
case 1:
return QString("From");
case 2:
return QString("Created Time");
case 3:
return QString("Rating");
case 4:
return QString("Feed");
case 5:
return QString("Double click to watch");
}
}
}
return QVariant();
}
最佳答案
只要您有一个包含当前页面的可访问成员变量,您就可以简单地将它乘以每页的行数 + 行数。
if(orientation == Qt::Vertical && role == Qt::DisplayRole)
{
return QString("%1").arg(m_currentPage*m_rowsPerPage+section);
}
但正如 Alxx 所说,您认为您无法进行任何计算可能是因为您未能调用返回我在上面示例中使用的变量的非 const 方法,对吧?
更新 你得到error C2166: l-value specified const object 的原因是你试图增加类变量:m_iHeaderCount++,这在 const 声明的方法中是不允许的。您可以在 headerData 方法中修改局部变量,但不能修改类变量。
关于c++ - 如何在进行分页时设置 Qt::Vertical headerData of QSqlQueryModel 增加数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260389/