[Qt-jambi-interest] Problem for displaying header and data
Vincent Lebreil
vincent.lebreil at gmail.com
Mon Feb 2 10:23:25 CET 2009
Any idea ? ...
BR
Vincent
On Tue, Jan 27, 2009 at 4:04 PM, Vincent Lebreil
<vincent.lebreil at gmail.com>wrote:
> Hi,
>
> I don't understand why nothing is displaying (neither header and data) if
> I'm using a QTreeView and my own model which inherits QAbstractItemModel.
>
> Btw if I use a QStandardItemModel instead, it works fine. Don't understand
> why ??
>
> Best Regards,
>
> Vincent
>
> Here's my code:
>
> ------------- 8< ----------------------------------------------------
> package org.workshop;
>
> import com.trolltech.qt.core.QObject;
> import com.trolltech.qt.core.Qt.Orientation;
> import com.trolltech.qt.gui.QApplication;
> import com.trolltech.qt.gui.QStandardItemModel;
> import com.trolltech.qt.gui.QWidget;
>
> public class MyTreeWidget extends QWidget {
>
> Ui_MyTreeWidget ui = new Ui_MyTreeWidget();
>
> public static void main(String[] args) {
> QApplication.initialize(args);
>
> MyTreeWidget testMyTreeWidget = new MyTreeWidget();
> testMyTreeWidget.show();
>
> QApplication.exec();
> }
>
> public MyTreeWidget() {
> ui.setupUi(this);
> init();
> }
>
> public MyTreeWidget(QWidget parent) {
> super(parent);
> ui.setupUi(this);
> init();
> }
>
> private void init(){
> ui.treeView.setModel(createMyStandardItemModel(this));
> ui.treeView_2.setModel(createMyCustomItemModel(this));
> }
>
> private QStandardItemModel createMyStandardItemModel(QObject parent){
> QStandardItemModel model = new QStandardItemModel(3,4,parent);
> model.setHeaderData(0, Orientation.Horizontal, "A");
> model.setHeaderData(1, Orientation.Horizontal, "B");
> model.setHeaderData(2, Orientation.Horizontal, "C");
> model.setHeaderData(3, Orientation.Horizontal, "D");
> model.setData(0, 0, "A1");
> model.setData(0, 1, "B1");
> model.setData(0, 2, "C1");
> model.setData(0, 3, "D1");
> model.setData(1, 0, "A2");
> model.setData(1, 1, "B2");
> model.setData(1, 2, "C2");
> model.setData(1, 3, "D2");
> model.setData(2, 0, "A3");
> model.setData(2, 1, "B3");
> model.setData(2, 2, "C3");
> model.setData(2, 3, "D3");
> return model;
> }
>
> private MyCustomItemModel createMyCustomItemModel(QObject parent){
> MyCustomItemModel model = new MyCustomItemModel(parent);
>
> return model;
> }
> }
>
> ------------- 8< ----------------------------------------------------
>
>
> ------------- 8< ----------------------------------------------------
> package org.workshop;
>
> 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.QObject;
> import com.trolltech.qt.core.Qt.ItemFlag;
> import com.trolltech.qt.core.Qt.ItemFlags;
> import com.trolltech.qt.core.Qt.Orientation;
>
> public class MyCustomItemModel extends QAbstractItemModel {
>
> private List<String[]> datas = new ArrayList<String[]>();
>
> public MyCustomItemModel(QObject parent){
> super(parent);
> datas.add(new String[]{"A1", "B1", "C1", "D1"});
> datas.add(new String[]{"A2", "B2", "C2", "D2"});
> datas.add(new String[]{"A3", "B3", "C3", "D3"});
> }
>
> @Override
> public Object headerData(int section, Orientation orientation, int
> role) {
> String header = null;
> switch (section) {
> case 0: header = "A"; break;
> case 1: header = "B"; break;
> case 2: header = "C"; break;
> case 3: header = "D"; break;
> }
> System.out.println("header: " + header);
> return header;
> }
>
>
> @Override
> public int columnCount(QModelIndex parent) {
> return 4;
> }
>
> @Override
> public Object data(QModelIndex index, int role) {
> System.out.println("index: " + index);
> int row = index.row();
> int column = index.column();
> if (index != null && row >= 0 && row < datas.size()) {
> String[] rowData = datas.get(row);
> if (column >= 0 && column < columnCount()){
> System.out.println("data: " + rowData[column]);
> return rowData[column];
> }
> }
> return null;
> }
>
> @Override
> public QModelIndex index(int row, int column, QModelIndex parent) {
> if (parent == null){
> return createIndex(row, column, 1);
> }
> return null;
> }
>
> @Override
> public QModelIndex parent(QModelIndex child) {
> return null;
> }
>
> @Override
> public int rowCount(QModelIndex parent) {
> return datas.size();
> }
>
> @Override
> public ItemFlags flags(QModelIndex index) {
> if (index == null) {
> return new ItemFlags(ItemFlag.ItemIsEnabled);
> }
> ItemFlags flags = super.flags(index);
> flags.set(ItemFlag.ItemIsEditable);
> return flags;
> }
>
> }
> ------------- 8< ----------------------------------------------------
>
> ------------- 8< ----------------------------------------------------
>
> /********************************************************************************
> ** Form generated from reading ui file 'MyTreeWidget.jui'
> **
> ** Created: mar. 27. janv. 15:33:57 2009
> ** by: Qt User Interface Compiler version 4.4.2
> **
> ** WARNING! All changes made in this file will be lost when recompiling ui
> file!
>
> ********************************************************************************/
>
> package org.workshop;
>
> import com.trolltech.qt.core.*;
> import com.trolltech.qt.gui.*;
>
> public class Ui_MyTreeWidget
> {
> public QTreeView treeView;
> public QTreeView treeView_2;
>
> public Ui_MyTreeWidget() { super(); }
>
> public void setupUi(QWidget MyTreeWidget)
> {
> MyTreeWidget.setObjectName("MyTreeWidget");
> MyTreeWidget.resize(new QSize(724,
> 387).expandedTo(MyTreeWidget.minimumSizeHint()));
> treeView = new QTreeView(MyTreeWidget);
> treeView.setObjectName("treeView");
> treeView.setGeometry(new QRect(50, 40, 256, 192));
>
> treeView.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.WheelFocus);
> treeView_2 = new QTreeView(MyTreeWidget);
> treeView_2.setObjectName("treeView_2");
> treeView_2.setGeometry(new QRect(400, 40, 256, 192));
>
> treeView_2.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.WheelFocus);
> treeView_2.setRootIsDecorated(false);
> treeView_2.setItemsExpandable(false);
> retranslateUi(MyTreeWidget);
>
> MyTreeWidget.connectSlotsByName();
> } // setupUi
>
> void retranslateUi(QWidget MyTreeWidget)
> {
>
> MyTreeWidget.setWindowTitle(com.trolltech.qt.core.QCoreApplication.translate("MyTreeWidget",
> "Form"));
> } // retranslateUi
>
> }
> ------------- 8< ----------------------------------------------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-jambi-interest/attachments/20090202/e8cde852/attachment.html
More information about the Qt-jambi-interest
mailing list