QTreeView incorrectly displays the SpinBox if item is checkable and when using QWindowsStyle

Posted by Sharraz on Stack Overflow See other posts from Stack Overflow or by Sharraz
Published on 2011-01-14T16:41:39Z Indexed on 2011/01/14 16:53 UTC
Read the original article Hit count: 173

Filed under:
|
|

Hello,

I'm having a problem with a QTreeView in my program: The SpinBox used to edit the double value of a checkable item is displayed incorrectly when using the Windows style. Only the up and down buttons of the SpinBox can be seen, but not any value. The following example code is able to reproduce the problem:

#include <QtGui>

class Model : public QAbstractItemModel
{
public:
    Model() : checked(false), number(0) {}

    Qt::ItemFlags flags(const QModelIndex & index) const
    {
        return Qt::ItemIsEnabled | Qt::ItemIsEditable |
               Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
    }

    QVariant data(const QModelIndex &index, int role) const
    {
        switch (role)
        {
        case Qt::DisplayRole:
        case Qt::EditRole:
            return QVariant(number);
        case Qt::CheckStateRole:
            return QVariant(checked ? Qt::Checked : Qt::Unchecked);
        }
        return QVariant();
    }

    QVariant headerData(int section, Qt::Orientation orientation, int role) const
    {
        return QVariant();
    }

    int rowCount(const QModelIndex &parent) const
    {
        return parent.isValid() ? 0 : 1;
    }

    int columnCount(const QModelIndex &parent) const
    {
        return parent.isValid() ? 0 : 1;
    }

    bool setData(const QModelIndex &index, const QVariant &value, int role)
    {
        switch (role)
        {
        case Qt::EditRole:
            number = value.toDouble();
            emit dataChanged(index, index);
            return true;
        case Qt::CheckStateRole:
            checked = value.toInt();
            emit dataChanged(index, index);
            return true;
        }
        return false;
    }

    QModelIndex index(int row, int column, const QModelIndex &parent) const
    {
        if (!row && !column && !parent.isValid())
            return createIndex(0, 0);

        return QModelIndex();
    }

    QModelIndex parent(const QModelIndex &child) const
    {
        return QModelIndex();
    }

private:
    bool checked;
    double number;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QApplication::setStyle(new QWindowsStyle());

    QTreeView tree;
    tree.setModel(new Model());
    tree.show();

    return app.exec();
}

The problems seems to have something to do with the checkbox. If Qt::ItemIsUserCheckable is removed, the SpinBox will be displayed correctly. If the number is replaced by a longer one like 0.01, it can be seen partially.

Any idea how this problem can be solved? Do I use the checkbox correctly?

Greets, Sharraz

© Stack Overflow or respective owner

Related posts about qt

Related posts about qtreeview