Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, November 2006
QAbstractTableModel and primitive types


Message 1 in thread

Dear all,

I actually using QAbstractTableModel, and when i return a primitive type at
the end of the data() method, sometimes the view doesn't show the value.
After some tests, it appears that char, byte, short and float types aren't
displayed by th viewer.

The following code, show this issue.

Laurent.

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QAbstractTableModel;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QScrollArea;
import com.trolltech.qt.gui.QTableView;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

public class TableModel extends QDialog {

    public TableModel(QWidget parent) {
        super(parent);
        try {
            QVBoxLayout layout = new QVBoxLayout();
            QScrollArea scroller = new QScrollArea();
            QTableView tableView = new QTableView();
            QAbstractTableModel model = new QAbstractTableModel() {
                public int columnCount(QModelIndex index) {
                    return 2;
                }

                public int rowCount(QModelIndex index) {
                    return 5;
                }

                public Object data(QModelIndex qModelIndex, int role) {
                    if (role == Qt.ItemDataRole.DisplayRole) {

                        int i = qModelIndex.row() * 2 + qModelIndex.column
();

                        switch (i) {
                        case 0:
                            return true;
                        case 1:
                            return 'C';
                        case 2:
                            return (byte) i;
                        case 3:
                            return (short) i;
                        case 4:
                            return (int) i;
                        case 5:
                            return (long) i;
                        case 6:
                            return (float) i;
                        case 7:
                            return (double) i;
                        case 8:
                            return "" + i;
                        default:
                            return "";
                        }
                    }
                    return null;
                }
            };
            tableView.setModel(model);
            tableView.setVisible(true);
            scroller.setWidget(tableView);

            QPushButton transmitButton = new QPushButton("Start transmit");

            layout.addWidget(scroller, 1);
            layout.addWidget(transmitButton);

            this.setLayout(layout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        QApplication.initialize(args);
        TableModel dialog = new TableModel(null);
        dialog.show();
        dialog.exec();
    }
}

Message 2 in thread

Hi, Laurent.

Laurent Jourdren wrote:

>
> I actually using QAbstractTableModel, and when i return a primitive 
> type at the end of the data() method, sometimes the view doesn't show 
> the value. After some tests, it appears that char, byte, short and 
> float types aren't displayed by th viewer.


In tech preview 3, there are unfortunately some problems with 
conversions where the original C++ API uses the QVariant-type. This has 
already been fixed for the next release.

Thank you for reporting!

-- Eskil