Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, June 2007
QSqlQueryModel & QListView with Check boxes


Message 1 in thread

I have created a QListView that displays a single column from an
QSqlQueryModel's query.  I would like to have check boxes next to each
item in the list.  I would also like to be able to change the font
colors and styles for individual items in the list.  I am looking into
creating a custom delegate.  Am I on the right track?  Thanks a lot...
-Willy

--
 [ signature omitted ] 

Message 2 in thread

To be more specific, I'm looking at a bunch of examples where
delegates are used for editing purposes.  Eg: Spreadheet example.  I
do not need this.  I want checkboxes always showing next to text in a
QListView (I am actually not picky about which view I use.  If another
would be better I will use it.)  It doesn't strike me that checking a
box is really editing and I don't want it to disappear when it's been
checked or unchecked. It seems easy enough to do this using a
QListWidget but I'd rather use the the model view classes.

If anyone could just send me down the right path a bit I'm sure I'll
pick up the rest.  Thanks a lot...
-Willy



On 6/16/07, Willy P <willy.lists@xxxxxxxxx> wrote:
> I have created a QListView that displays a single column from an
> QSqlQueryModel's query.  I would like to have check boxes next to each
> item in the list.  I would also like to be able to change the font
> colors and styles for individual items in the list.  I am looking into
> creating a custom delegate.  Am I on the right track?  Thanks a lot...
> -Willy
>

--
 [ signature omitted ] 

Message 3 in thread

Hi Willy,

To make your data items checkable you need to override the flags function in 
your model and use it to return a set of flags that include the 
Qt::ItemIsUserCheckable for the indexes of the relevent items. Here is a 
sample from one of my models that sets the data items in the first column to 
be checkable:

Qt::ItemFlags MRStructureModel::flags( const QModelIndex& index ) const
{
    if ( !index.isValid() )
    {
        qDebug() << "Item can be dropped here";
        return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
    }

    Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    if ( index.column() == 0 )
        defaultFlags |= Qt::ItemIsUserCheckable;

    StructureModelNode* item = static_cast<StructureModelNode*>( 
index.internalPointer() );
    if ( item->itemType() == StructureModelNode::Group )
        defaultFlags |= Qt::ItemIsDropEnabled;

    return defaultFlags;
}


In addition to this your view needs ot be able to get and set the checkstate 
using the standard QAbstractItemModel interface. This means that you need to 
also override the data() and setData() functions in your model class (or just 
the data() function in the case of a read-only model). i.e.

QVariant MRStructureModel::data( const QModelIndex& index, int role ) const
{
    if ( !index.isValid() )
        return QVariant();
    ...<your other code>

    if ( index.column() == 0 && role == Qt::CheckStateRole )
    {
        // Change this to suit your particular model storage backend
        StructureModelNode* item = static_cast<StructureModelNode*>( 
index.internalPointer() );
        return item->checkState();
    }
}


bool MRStructureModel::setData( const QModelIndex& index, const QVariant& 
value, int role )
{
    if ( !index.isValid() )
        return false;
    ...<your other code>

    if ( role == Qt::CheckStateRole )
    {
         // Store the new checkstate to your backend here
    }
}

Hope this helps to get you started.

Kind regards,

Sean

--
 [ signature omitted ]