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

Qt-jambi-interest Archive, December 2007
Model/View Programming


Message 1 in thread

I am trying to follow the Model/View Programming Documentation by 
translating those C++ Examples
Step by Step to Java. Seems to be straigt forward, but some questions 
arrised:


1) I stuck with the QModelIndex() Function in the Chapter "Inserting and 
Removing Rows".


------------------------------------------------------------------------------------
bool StringListModel::insertRows(int position, int rows, const QModelIndex 
&parent)
{
    beginInsertRows(QModelIndex(), position, position+rows-1);

    for (int row = 0; row < rows; ++row) {
        stringList.insert(position, "");
    }

    endInsertRows();
    return true;
}
-------------------------------------------------------------------------------------

became

****************************************************************
@Override
public boolean insertRows(int position, int rows, QModelIndex parent){
    beginInsertRows(QModelIndex(), position, position+rows-1);

    for (int row = 0; row < rows; ++row){
        stringList.set(position, "");
    }
    endInsertRows();
    return true;
}
****************************************************************

but QModelIndex() seems to be missing! How do I reference to the top level 
item in a model?

2) Same Problem with index.isValid():

------------------------------------------------------------------------------------
bool StringListModel::setData(const QModelIndex &index,
                              const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {

        stringList.replace(index.row(), value.toString());
        emit dataChanged(index, index);
        return true;
    }
    return false;
}
------------------------------------------------------------------------------------

became

***************************************************************
@Override
public boolean setData(QModelIndex index, Object value, int role){
    if(role == Qt.ItemDataRole.EditRole){
        stringList.set(index.row(), value.toString());
        dataChanged.emit(index,index);
        return true;
    }
    return false;
}
***************************************************************

without making sure that the index is valid, because index.isValid() seems 
to be missing.


3) Am I righ?t:

------------------------------------------------------------------------------------
Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
------------------------------------------------------------------------------------

became

***************************************************************
@Override
public Qt.ItemFlags flags(QModelIndex index){
    Qt.ItemFlags myFlags = 
Qt.ItemFlag.createQFlags(Qt.ItemFlag.ItemIsEditable);
    myFlags.set(Qt.ItemFlag.ItemIsSelectable);
    myFlags.set(Qt.ItemFlag.ItemIsEnabled);
    return myFlags;
}
***************************************************************

seems to be working. Was it the right way?

4) Anyone interested in discussing the hole trail?

or is this totaly sensless because official documentation with java code is 
in sight?


Any help would be appreciated


Georg




Message 2 in thread

Georg Wolf wrote:
> 1) I stuck with the QModelIndex() Function in the Chapter "Inserting and 
> Removing Rows".
>   

This is actually the default constructor of the QModelIndex class in 
C++. It constructs an invalid model index.

In C++, calling a constructor without specifying the "new" operator 
means you'll allocate memory for it on the stack rather than the heap, 
and it will be memory managed accordingly.

In Qt Jambi the invalid model index is designated by a null reference 
instead. This is something we've chosen to do for certain types for the 
sake of efficiency (Java can't do allocations of complex types on stack, 
thus the overhead of constructing new objects is much greater.)


> ****************************************************************
> @Override
> public boolean insertRows(int position, int rows, QModelIndex parent){
>     beginInsertRows(QModelIndex(), position, position+rows-1);
>   

Try:

    beginInsertRows(null, position, position+rows-1);

>
> but QModelIndex() seems to be missing! How do I reference to the top level 
> item in a model?
>   

A model can have several top level items, and they can all be accessed 
by passing "null" as the parent, e.g. to the QAbstractItemModel.index() 
method:

    
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/core/QAbstractItemModel.html#index(int, 
int)


> 2) Same Problem with index.isValid():
>
> ------------------------------------------------------------------------------------
> bool StringListModel::setData(const QModelIndex &index,
>                               const QVariant &value, int role)
> {
>     if (index.isValid() && role == Qt::EditRole) {
>
>   
[...]
> became
>
> ***************************************************************
> @Override
> public boolean setData(QModelIndex index, Object value, int role){
>     if(role == Qt.ItemDataRole.EditRole){
>
>
>   


The "null equals invalid" goes here as well, so check the index against 
null before continuing:

    if (index != null && role == Qt.ItemDataRole.EditRole)


> 4) Anyone interested in discussing the hole trail?
>
> or is this totaly sensless because official documentation with java code is 
> in sight?
>
>   

We'd be interested in hearing about any porting difficulties you 
have/discoveries you make. Even with full Java documentation available, 
it will be valuable information, as it's not an unlikely use case.

-- Eskil


Message 3 in thread

Thank you for your quick reply! Very informative for me!


Georg