Qt-jambi-interest Archive, February 2008
Testing the table model/view
Message 1 in thread
Hello!
I just tried to test the model/view programming in Jambi with very
simple default implementations of the QAbstractTableModel and QTableView
classes. However, the default behavior seem to be a checkbox displayed
inside every cell + the text beeing rendered in the upper left corner.
How can I easily remove this behavior?
Best regards,
Helge Fredriksen
Message 2 in thread
Helge Fredriksen wrote:
> Hello!
>
> I just tried to test the model/view programming in Jambi with very
> simple default implementations of the QAbstractTableModel and
> QTableView classes. However, the default behavior seem to be a
> checkbox displayed inside every cell + the text beeing rendered in the
> upper left corner.
>
> How can I easily remove this behavior?
Hi, Helge.
It is not the default to render checkboxes, your model is most likely
returning some invalid data for the decoration role of the cell. E.g if
you make an implementation of data() which returns a string for all
roles, this could cause the behavior you are seeing plus other arbitrary
reactions to the invalid data. If you have the data() method return null
for the decoration role, you will get the default behavior.
As for the alignment of the text, if you wish to e.g. center the text
completely, you should return a combination of the flags
Qt.AlignmentFlag.AlignVCenter and Qt.AlignmentFlag.AlignHCenter for the
role Qt.ItemDataRole.TextAlignmentRole. As a convenience, you can return
Qt.Alignment.AlignCenter which is a preset combination of center in both
horizontal and vertical direction.
Now, there's a bug in Qt Jambi 4.3 which means you will have to return
the bitwise combination of the flags as an *integer* in order for item
views to properly interpret them. In Qt Jambi 4.4, this will be fixed,
and you will be able to alternatively return the enum objects themselves
or an object of the QFlags type Qt.Alignment which represents a
combination of alignment flags. However, until Qt Jambi 4.4 has been
released, you will have to call the value() method on the enum or flags
in order to get its integer value.
Thus:
return Qt.AlignmentFlag.AlignCenter.value();
or:
return new Qt.Alignment(Qt.AlignmentFlag.AlignHCenter,
Qt.AlignmentFlag.AlignVCenter).value();
or:
return Qt.AlignmentFlag.AlignHCenter.value() |
Qt.AlignmentFlag.AlignVCenter.value();
All of these should work. I've attached an example of how this could be
done. Hope this helps!
-- Eskil
import com.trolltech.qt.core.*;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
public class TableView extends QAbstractTableModel {
@Override
public int rowCount(QModelIndex index) {
return 10;
}
@Override
public int columnCount(QModelIndex index) {
return 10;
}
@Override
public Object data(QModelIndex index, int role) {
if (role == Qt.ItemDataRole.DisplayRole) {
return "Hello world";
} else if (role == Qt.ItemDataRole.TextAlignmentRole) {
return Qt.AlignmentFlag.AlignCenter.value();
} else {
return null;
}
}
public static void main(String args[]) {
QApplication.initialize(args);
QTableView view = new QTableView();
view.setModel(new TableView());
view.show();
QApplication.exec();
}
}
Message 3 in thread
Eskil Abrahamsen Blomfeldt wrote:
> It is not the default to render checkboxes, your model is most likely
> returning some invalid data for the decoration role of the cell.
Sorry, I have to correct myself here. The problem is not returning the
wrong thing for the decoration role, but for the
Qt.ItemDataRole.CheckStateRole. If you return anything except null for
this role then the cell will get a check box (which is unchecked unless
the object returned is the boolean value true or a value which is
converted to "true", such as the integer value 1 or the string "1" etc.)
-- Eskil
Message 4 in thread
Ok, that's nice, I have a working model/view table that can
edit/remove/add rows. Easier to work with than the Swing equivalent, no
doubt!
However, upon removing rows, I have to make the cells selectable first.
overriding the model with
public Qt.ItemFlags flags(QModelIndex index) {
return Qt.ItemFlag.createQFlags(Qt.ItemFlag.ItemIsEditable,
Qt.ItemFlag.ItemIsEnabled, Qt.ItemFlag.ItemIsSelectable);
}
This makes it possible to select several cells at once, something that
is not desired since I then need to sort out which rows are multiple
selected via possible multiple column on the same row being selected.
How can I set the model to be single selection mode, which is possible
in Swing tables/lists?
Regards,
Helge Fredriksen
Eskil Abrahamsen Blomfeldt wrote:
> Eskil Abrahamsen Blomfeldt wrote:
>> It is not the default to render checkboxes, your model is most likely
>> returning some invalid data for the decoration role of the cell.
>
> Sorry, I have to correct myself here. The problem is not returning the
> wrong thing for the decoration role, but for the
> Qt.ItemDataRole.CheckStateRole. If you return anything except null for
> this role then the cell will get a check box (which is unchecked
> unless the object returned is the boolean value true or a value which
> is converted to "true", such as the integer value 1 or the string "1"
> etc.)
>
>
> -- Eskil
>
Message 5 in thread
Helge Fredriksen wrote:
> Ok, that's nice, I have a working model/view table that can
> edit/remove/add rows. Easier to work with than the Swing equivalent, no
> doubt!
>
> However, upon removing rows, I have to make the cells selectable first.
> overriding the model with
>
> public Qt.ItemFlags flags(QModelIndex index) {
> return Qt.ItemFlag.createQFlags(Qt.ItemFlag.ItemIsEditable,
> Qt.ItemFlag.ItemIsEnabled, Qt.ItemFlag.ItemIsSelectable);
> }
>
> This makes it possible to select several cells at once, something that
> is not desired since I then need to sort out which rows are multiple
> selected via possible multiple column on the same row being selected.
>
> How can I set the model to be single selection mode, which is possible
> in Swing tables/lists?
Hi Helge,
What you're looking for is selection behaviour and selection modes which
are properties of the view:
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/gui/QAbstractItemView.html#setSelectionBehavior(com.trolltech.qt.gui.QAbstractItemView.SelectionBehavior)
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/gui/QAbstractItemView.html#setSelectionMode(com.trolltech.qt.gui.QAbstractItemView.SelectionMode)
best regards,
Gunnar