[Qt-jambi-interest] BackgroundRole
Vincent Lebreil
vincent.lebreil at gmail.com
Mon Feb 9 11:20:57 CET 2009
Hi Gunnar,
it works with your example, ie with a QTableView and a QAbstractTableModel,
but not with a QTreeView and a QAbstractItemModel.
Any idea ?
Here's the code:
------------------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<--------------------------
On Mon, Feb 9, 2009 at 11:02 AM, Gunnar Sletta <gunnar at trolltech.com> wrote:
> Vincent Lebreil wrote:
>
>> Hi,
>>
>> I'm trying to display a background color for all items of my model.
>> I've tried to return a QColor in my data method for BackgroundRole. But
>> this color is only displayed if the mouse is over one row of my view. This
>> color is not displayed for all the rows of my view.
>> I don't understand why, cause if I do the same thing but with
>> DecorationRole, it works great (ie all the rows of my view have this
>> decoration).
>>
>> Thanks for your help
>>
>
> Hi Vincent,
>
> In the following example, I set the displayrole to red and it consistently
> produces red cells. Are you doing anyting differently?
>
> import java.util.*;
> import com.trolltech.qt.gui.*;
> import com.trolltech.qt.core.*;
>
> public class TableModelMyFile extends QAbstractTableModel {
> ArrayList<String> myFiles;
>
> public TableModelMyFile(ArrayList<String> myFiles) {
> this.myFiles = myFiles;
> }
>
> public int columnCount(QModelIndex i) { return 5; }
> public int rowCount(QModelIndex i) { return myFiles.size(); }
>
> public Object data(final QModelIndex index, int role) {
> switch (role) {
> case Qt.ItemDataRole.DisplayRole:
> return myFiles.get(index.row()) + ":" + index.column();
> case Qt.ItemDataRole.BackgroundRole:
> return QColor.red;
> default:
> break;
> }
> return null;
> }
>
>
> public void doStuff() {
> beginInsertRows(null, 3, 3);
> myFiles.add("Four");
> endInsertRows();
> }
>
>
> public static void main(String args[]) {
> QApplication.initialize(args);
>
> QTableView view = new QTableView();
>
> ArrayList<String> list = new ArrayList<String>();
> list.add("One");
> list.add("Two");
> list.add("Three");
>
> final TableModelMyFile model = new TableModelMyFile(list);
> view.setModel(model);
>
> view.show();
>
> QApplication.invokeLater(3000, new Runnable() {
> public void run() {
> model.doStuff();
> }
> });
>
> QApplication.exec();
> }
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-jambi-interest/attachments/20090209/360000a6/attachment.html
More information about the Qt-jambi-interest
mailing list