3

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?

4

0 回答 0