[Qt-jambi-interest] BackgroundRole

Vincent Lebreil vincent.lebreil at gmail.com
Wed Feb 11 10:02:43 CET 2009


But with the code I sent you, ie with a QTreeView AND a QAbstractItemModel
(not a QAbstractTableModel) (see my code below), do you reproduce this
behaviour ?

I maybe miss something but I dont know what exactly...

Besides I really need to use QAbstractItemModel and QTreeView (I can't use
QAbstractTableModel cause it's too specific).

Here's the code I would like you to test:
------------------8<--------------------------

import java.util.ArrayList;
import java.util.List;

import com.trolltech.qt.core.QAbstractItemModel;
import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.ItemFlags;
import com.trolltech.qt.core.Qt.Orientation;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QTreeView;

public class MyModel extends QAbstractItemModel {

    private String[] headers = new String[]{"A", "B"};

    private List<String> myList = null;

    public MyModel(List<String> list) {
        this.myList = list;
    }

    @Override
    public Object headerData(int section, Orientation orientation, int role)
{
        Object data = null;
        if (role ==  Qt.ItemDataRole.DisplayRole){
            data = headers[section];
        }
        return data;
    }


    @Override
    public int columnCount(QModelIndex parent) {
        return headers.length;
    }

    @Override
    public Object data(QModelIndex index, int role) {
        switch (role) {
        case Qt.ItemDataRole.DisplayRole:
            return myList.get(index.row()) + ":" + index.column();
        case Qt.ItemDataRole.BackgroundRole:
            return QColor.red;
        default:
            break;
        }
        return null;
    }

    @Override
    public QModelIndex index(int row, int column, QModelIndex parent) {
        QModelIndex index = null;
        if (parent == null) {
            if (row >= 0 && row < myList.size()) {
                index = createIndex(row, column, 1);
            }
        }
        return index;

    }

    @Override
    public QModelIndex parent(QModelIndex child) {
        QModelIndex parent = null;
        return parent;
    }

    @Override
    public int rowCount(QModelIndex parent) {
        int rowCount = 0;
        if (parent == null) {
            rowCount = myList.size();
        }

        return rowCount;
    }

    @Override
    public ItemFlags flags(QModelIndex index) {
        return super.flags(index);
    }

    public static void main(String args[]) {
        QApplication.initialize(args);

        QTreeView view = new QTreeView();

        ArrayList<String> list = new ArrayList<String>();
        list.add("One");
        list.add("Two");
        list.add("Three");

        final MyModel model = new MyModel(list);
        view.setModel(model);

        view.show();

        QApplication.exec();
    }

}
------------------8<--------------------------


Best regards

Vincent

On Wed, Feb 11, 2009 at 8:13 AM, Gunnar Sletta <gunnar at trolltech.com> wrote:

> Vincent Lebreil wrote:
>
>> Hi !
>>
>> Do you have the same behaviour with my QTreeView/QAbstractItemModel ? and
>> if so, do you know why ?
>>
>
> I cannot reproduce this behaviour. If I change the QTableView in my code to
> a QTreeView it gets still has red backgrounds and with the DecorationRole, I
> get a blue icon next to all the items.
>
> -
> Gunnar
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-jambi-interest/attachments/20090211/4a830512/attachment.html 


More information about the Qt-jambi-interest mailing list