[Qt-interest] checkbox in table view

linux newbie linux.newbie79 at gmail.com
Mon Sep 27 07:32:49 CEST 2010


Hi,

Thanks for your reply.

I tried the following code, but my check box was not editable. Let me
know, the mistake I made.

QVariant ClientModel::data ( const QModelIndex &index, int role ) const
{
    switch (role)
    {
    case Qt::DisplayRole:
         return list->value(index.row());
         break;

    case Qt::CheckStateRole:
         return QVariant(Qt::Unchecked);
        break;

    default:
        return QVariant();
    }
}

Qt::ItemFlags ClientModel::flags ( const QModelIndex &index ) const
{
    Qt::ItemFlags flags = Qt::ItemIsEnabled;

    if (index.isValid())
        flags = QAbstractTableModel::flags(index) |Qt::ItemIsUserCheckable;
    return  flags;
}



int main (int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStringList list;
    QTableView table;

    list << "row_1" << "row_2";
    ClientModel model(list);


    table.setModel(&model);
    table.show();
    return app.exec();
}
On Mon, Sep 27, 2010 at 3:26 PM, Andre Somers <andre at familiesomers.nl> wrote:
>  Op 27-9-2010 6:35, linux newbie schreef:
>> Hi,
>>
>> Can anyone let me know, how to create check box in table view?
>>
>> First Column has to contain this check box.
>>
>> I belive it can be done through delegate. If yes, how to change the
>> paint method?
> The easiest way by far, is to use/return the correct item flags for the
> item that needs to show the check box. See QAbstractItemModel::flags()
> and the Qt::CheckStateRole for the QAbstractItemModel::data() method.
>
> André
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>




More information about the Qt-interest-old mailing list