模型中闪烁的背景单元格
我对自定义角色 (IsBlinkingRole
) 的数据方法中的 setData
感到困惑。 我尝试了不同的选项,但每个选项都失败了。
bool CustomSqlModel::setData( const QModelIndex& index, const QVariant& value, int role)
{
if (role == IsBlinkingRole && index.column() == 0 && index.isValid()) {
//What to put here ?
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant CustomSqlModel::data(const QModelIndex& index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if (role == Qt::BackgroundRole && index.column() == 0) {
QModelIndex testIndex = index.model()->index(index.row(), 0, index.parent());
if ( index.model()->data(testIndex, Qt::DisplayRole).toInt() == 5)
return QVariant(QColor(Qt::red));
else
return QVariant();
}
return value;
}
void CustomSqlModel::timerEvent(QTimerEvent *)
{
static bool blinkon = true;
blinkon = !blinkon;
int topRow = rowCount();
int bottomRow = -1;
for(int i = 0; i < rowCount(); i++) {
for(int j = 0; j < columnCount(); j++) {
if(data(index(i, j), IsBlinkingRole).toBool()){
if(blinkon)
setData(index(i, j), Qt::red, Qt::BackgroundRole);
else
setData(index(i, j), QVariant(), Qt::BackgroundRole);
if(i < topRow) topRow = i; if(i > bottomRow) bottomRow = i;
}
}
}
if(bottomRow >= 0)
emit dataChanged(index(topRow, 0), index(bottomRow, columnCount()-1));
}
setData
方法的问题在于我需要在此处放置什么样的代码。
我必须先创建一个容器来存储数据吗?
I am confused about the setData
in data methods for my custom role (IsBlinkingRole
). I've tried different options but every option failed.
bool CustomSqlModel::setData( const QModelIndex& index, const QVariant& value, int role)
{
if (role == IsBlinkingRole && index.column() == 0 && index.isValid()) {
//What to put here ?
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant CustomSqlModel::data(const QModelIndex& index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if (role == Qt::BackgroundRole && index.column() == 0) {
QModelIndex testIndex = index.model()->index(index.row(), 0, index.parent());
if ( index.model()->data(testIndex, Qt::DisplayRole).toInt() == 5)
return QVariant(QColor(Qt::red));
else
return QVariant();
}
return value;
}
void CustomSqlModel::timerEvent(QTimerEvent *)
{
static bool blinkon = true;
blinkon = !blinkon;
int topRow = rowCount();
int bottomRow = -1;
for(int i = 0; i < rowCount(); i++) {
for(int j = 0; j < columnCount(); j++) {
if(data(index(i, j), IsBlinkingRole).toBool()){
if(blinkon)
setData(index(i, j), Qt::red, Qt::BackgroundRole);
else
setData(index(i, j), QVariant(), Qt::BackgroundRole);
if(i < topRow) topRow = i; if(i > bottomRow) bottomRow = i;
}
}
}
if(bottomRow >= 0)
emit dataChanged(index(topRow, 0), index(bottomRow, columnCount()-1));
}
The problem I have with the setData
method is regarding what kind of code I need to put here.
Do I have to create a container first to store data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论