Solved it!
Nice solution. I removed my delegate and reimplemented these functions in my model:
/* Reimplemented function: flags
* Set the att coulumn editable flag
*/
Qt
::ItemFlags EconomyHoursModel
::flags( const QModelIndex &index
) const{
if( index.column() == 5 )
flags |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
return flags;
}
/* Reimplemented function: data
* Have to reimplement this in order to show a checkbox when not editing the table
*/
{
if( role == Qt::CheckStateRole && index.column() == 5 )
{
ret = Qt::Unchecked;
else
ret = Qt::Checked;
}
else if( role == Qt::DisplayRole && index.column() == 5 ) //Don't show date as text
{
}
else
{
}
return ret;
}
/* Reimplemented function: setData
* Sets data for the att column
*/
{
bool ret = false;
if( role == Qt::CheckStateRole )
{
if( index.column() != 5 )
return false;
int id = data( idIndex ).toInt();
if( value.toInt() == Qt::Checked )
storeValue
= QDate::currentDate().
toString( "yyyy-MM-dd" );
clear();
query.prepare( "UPDATE hours SET att=? WHERE id=?" );
query.addBindValue( storeValue );
query.addBindValue( id );
ret = query.exec();
if( ret )
refresh();
}
else
{
}
return ret;
}
/* Reimplemented function: flags
* Set the att coulumn editable flag
*/
Qt::ItemFlags EconomyHoursModel::flags( const QModelIndex &index ) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags( index );
if( index.column() == 5 )
flags |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
return flags;
}
/* Reimplemented function: data
* Have to reimplement this in order to show a checkbox when not editing the table
*/
QVariant EconomyHoursModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if( role == Qt::CheckStateRole && index.column() == 5 )
{
if( QSqlQueryModel::data( index, Qt::DisplayRole ).toString() == "" )
ret = Qt::Unchecked;
else
ret = Qt::Checked;
}
else if( role == Qt::DisplayRole && index.column() == 5 ) //Don't show date as text
{
ret = QVariant();
}
else
{
ret = QSqlQueryModel::data( index, role );
}
return ret;
}
/* Reimplemented function: setData
* Sets data for the att column
*/
bool EconomyHoursModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
bool ret = false;
if( role == Qt::CheckStateRole )
{
if( index.column() != 5 )
return false;
QModelIndex idIndex = QSqlQueryModel::index( index.row(), 0 );
int id = data( idIndex ).toInt();
QString storeValue = "";
if( value.toInt() == Qt::Checked )
storeValue = QDate::currentDate().toString( "yyyy-MM-dd" );
clear();
QSqlQuery query;
query.prepare( "UPDATE hours SET att=? WHERE id=?" );
query.addBindValue( storeValue );
query.addBindValue( id );
ret = query.exec();
if( ret )
refresh();
}
else
{
ret = QSqlQueryModel::setData( index, value, role );
}
return ret;
}
To copy to clipboard, switch view to plain text mode
Thanks for pointing me in the right direction!!!
//John
Bookmarks