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

Qt-interest Archive, July 2007
AbstractTableModel subclass


Message 1 in thread

Hello i create my TableModel extending QAbstractTableModel

I observer a extrange behavior my view allways display a empty row at the end.

I other words when my rowCount returns 1  I view 2 rows in screen one
with the data in the model a one empty. Any ideas on how avoid this
empty row.

thanks in advance

-- 
 [ signature omitted ] 

Message 2 in thread

> Hello i create my TableModel extending QAbstractTableModel
>
> I observer a extrange behavior my view allways display a empty row at the
> end.
>
> I other words when my rowCount returns 1  I view 2 rows in screen one
> with the data in the model a one empty. Any ideas on how avoid this
> empty row.
>
>

You will have to post a minimal compilable example of this issue. Once we
have that I'm sure we will see an error in your code.

Cheers!
--Justin

--
 [ signature omitted ] 

Message 3 in thread

On Jul 26, 2007, at 8:13 PM, pepone.onrez wrote:

> Hello i create my TableModel extending QAbstractTableModel
>
> I observer a extrange behavior my view allways display a empty row  
> at the end.
>
> I other words when my rowCount returns 1  I view 2 rows in screen one
> with the data in the model a one empty. Any ideas on how avoid this
> empty row.
>
> thanks in advance

Try using http://labs.trolltech.com/page/Projects/Itemview/Modeltest

It's a ModelTest and it helps to catch most of common errors in  
models. When you get some assertion go to line it points, and in  
comments it will tell you how to fix it.

--
 [ signature omitted ] 

Message 4 in thread

I try modeltest and it not report any errors in my code.

Here is the code in my tableModel

QVariant Oz::Ui::TableModel::headerData(int section,Qt::Orientation
orientation,int role)const{
    if(_manager != 0 && role < _manager->headerData().size()){
        if(orientation == Qt::Horizontal)
            return _manager->headerData()[role];
        else
            return section;
    }
    return QVariant();
}

QVariant Oz::Ui::TableModel::data(const QModelIndex &index, int role) const{
    if (!index.isValid()){
        return QVariant();
    }if (role == Qt::DecorationRole){
        Oz::Ui::ModelMap::const_iterator it;
        it = _items.find(index.row());
        if(it != _items.end())
            return (*it).second->oz_icon();
        return QIcon(":/images/default_incon.png");
    }else if (role == Qt::DisplayRole){
        QString value = "";
        Oz::Ui::ModelMap::const_iterator it;
        it = _items.find(index.row());
        if(it != _items.end())
            value = (*it).second->oz_value(index.column());
        return value;
    }
    return QVariant();
}


Qt::ItemFlags Oz::Ui::TableModel::flags(const QModelIndex& index) const{
    if (index.isValid()){
        return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    }
    return Qt::ItemIsEnabled;
}

int Oz::Ui::TableModel::rowCount(const QModelIndex& parent) const{
    if(parent.isValid())
        return 0;
    return _items.size();
}

int Oz::Ui::TableModel::columnCount(const QModelIndex& parent) const{
    if (parent.isValid())
        return 0;
    if(_manager)
        return _manager->headerData().size();
    return 0;
}

//Code end

Thanks in advance


On 7/27/07, Kamil Klimek <naresh@xxxxxxx> wrote:
>
> On Jul 26, 2007, at 8:13 PM, pepone.onrez wrote:
>
> > Hello i create my TableModel extending QAbstractTableModel
> >
> > I observer a extrange behavior my view allways display a empty row
> > at the end.
> >
> > I other words when my rowCount returns 1  I view 2 rows in screen one
> > with the data in the model a one empty. Any ideas on how avoid this
> > empty row.
> >
> > thanks in advance
>
> Try using http://labs.trolltech.com/page/Projects/Itemview/Modeltest
>
> It's a ModelTest and it helps to catch most of common errors in
> models. When you get some assertion go to line it points, and in
> comments it will tell you how to fix it.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>


-- 
 [ signature omitted ] 

Message 5 in thread

On fredag den 27. Juli 2007, pepone.onrez wrote:
> I try modeltest and it not report any errors in my code.
>
> Here is the code in my tableModel
>
> QVariant Oz::Ui::TableModel::headerData(int section,Qt::Orientation
> orientation,int role)const{
>     if(_manager != 0 && role < _manager->headerData().size()){
>         if(orientation == Qt::Horizontal)
>             return _manager->headerData()[role];
>         else
>             return section;

You need to use "return _manager->headerData()[section]" and only when role is 
Qt::DisplayRole. Look at the Qt documentation for the role values.

>     }
>     return QVariant();
> }
>
> QVariant Oz::Ui::TableModel::data(const QModelIndex &index, int role)
> const{ if (!index.isValid()){
>         return QVariant();
>     }if (role == Qt::DecorationRole){
>         Oz::Ui::ModelMap::const_iterator it;
>         it = _items.find(index.row());
>         if(it != _items.end())
>             return (*it).second->oz_icon();
>         return QIcon(":/images/default_incon.png");
>     }else if (role == Qt::DisplayRole){
>         QString value = "";
>         Oz::Ui::ModelMap::const_iterator it;
>         it = _items.find(index.row());
>         if(it != _items.end())
>             value = (*it).second->oz_value(index.column());
>         return value;
>     }
>     return QVariant();
> }
>
>
> Qt::ItemFlags Oz::Ui::TableModel::flags(const QModelIndex& index) const{
>     if (index.isValid()){
>         return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
>     }
>     return Qt::ItemIsEnabled;
> }

index.isValid() only checks if the row or column number is < 0. You have to 
check yourself if it is withint the row and column count of your model. Also, 
you might want to use BaseClass::flags(index) instead of just 
Qt::ItemIsEnabled.

> int Oz::Ui::TableModel::rowCount(const QModelIndex& parent) const{
>     if(parent.isValid())
>         return 0;
>     return _items.size();
> }
>
> int Oz::Ui::TableModel::columnCount(const QModelIndex& parent) const{
>     if (parent.isValid())
>         return 0;
>     if(_manager)
>         return _manager->headerData().size();
>     return 0;
> }

When you do this, be sure to call reset() if you set _manager after the model 
is instantiated.

I hope this helps.

Bo.

> //Code end
>
> Thanks in advance
>
> On 7/27/07, Kamil Klimek <naresh@xxxxxxx> wrote:
> > On Jul 26, 2007, at 8:13 PM, pepone.onrez wrote:
> > > Hello i create my TableModel extending QAbstractTableModel
> > >
> > > I observer a extrange behavior my view allways display a empty row
> > > at the end.
> > >
> > > I other words when my rowCount returns 1  I view 2 rows in screen one
> > > with the data in the model a one empty. Any ideas on how avoid this
> > > empty row.
> > >
> > > thanks in advance
> >
> > Try using http://labs.trolltech.com/page/Projects/Itemview/Modeltest
> >
> > It's a ModelTest and it helps to catch most of common errors in
> > models. When you get some assertion go to line it points, and in
> > comments it will tell you how to fix it.
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body. List archive and information:
> > http://lists.trolltech.com/qt-interest/



-- 
 [ signature omitted ] 

Message 6 in thread

Hi  thanks for all the tips

The problem was that i made an incorrect call to beginInsertRows i
change _itemes.size() for _items.size()  - 1 and now all is working.

void Oz::Ui::TableModel::setItems(const Oz::Ui::ModelMap& items){
    if(_items.size() > 0 ){
        beginRemoveRows(QModelIndex(),0,_items.size() );
        _items.clear();
        endRemoveRows();
    }
    beginInsertRows(QModelIndex(),0,items.size() );
    _items = items;
    endInsertRows();
}

On 7/27/07, Bo Thorsen <bo@xxxxxxxxxxxxxxxxxxxxx> wrote:
> On fredag den 27. Juli 2007, pepone.onrez wrote:
> > I try modeltest and it not report any errors in my code.
> >
> > Here is the code in my tableModel
> >
> > QVariant Oz::Ui::TableModel::headerData(int section,Qt::Orientation
> > orientation,int role)const{
> >     if(_manager != 0 && role < _manager->headerData().size()){
> >         if(orientation == Qt::Horizontal)
> >             return _manager->headerData()[role];
> >         else
> >             return section;
>
> You need to use "return _manager->headerData()[section]" and only when role is
> Qt::DisplayRole. Look at the Qt documentation for the role values.
>
> >     }
> >     return QVariant();
> > }
> >
> > QVariant Oz::Ui::TableModel::data(const QModelIndex &index, int role)
> > const{ if (!index.isValid()){
> >         return QVariant();
> >     }if (role == Qt::DecorationRole){
> >         Oz::Ui::ModelMap::const_iterator it;
> >         it = _items.find(index.row());
> >         if(it != _items.end())
> >             return (*it).second->oz_icon();
> >         return QIcon(":/images/default_incon.png");
> >     }else if (role == Qt::DisplayRole){
> >         QString value = "";
> >         Oz::Ui::ModelMap::const_iterator it;
> >         it = _items.find(index.row());
> >         if(it != _items.end())
> >             value = (*it).second->oz_value(index.column());
> >         return value;
> >     }
> >     return QVariant();
> > }
> >
> >
> > Qt::ItemFlags Oz::Ui::TableModel::flags(const QModelIndex& index) const{
> >     if (index.isValid()){
> >         return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
> >     }
> >     return Qt::ItemIsEnabled;
> > }
>
> index.isValid() only checks if the row or column number is < 0. You have to
> check yourself if it is withint the row and column count of your model. Also,
> you might want to use BaseClass::flags(index) instead of just
> Qt::ItemIsEnabled.
>
> > int Oz::Ui::TableModel::rowCount(const QModelIndex& parent) const{
> >     if(parent.isValid())
> >         return 0;
> >     return _items.size();
> > }
> >
> > int Oz::Ui::TableModel::columnCount(const QModelIndex& parent) const{
> >     if (parent.isValid())
> >         return 0;
> >     if(_manager)
> >         return _manager->headerData().size();
> >     return 0;
> > }
>
> When you do this, be sure to call reset() if you set _manager after the model
> is instantiated.
>
> I hope this helps.
>
> Bo.
>
> > //Code end
> >
> > Thanks in advance
> >
> > On 7/27/07, Kamil Klimek <naresh@xxxxxxx> wrote:
> > > On Jul 26, 2007, at 8:13 PM, pepone.onrez wrote:
> > > > Hello i create my TableModel extending QAbstractTableModel
> > > >
> > > > I observer a extrange behavior my view allways display a empty row
> > > > at the end.
> > > >
> > > > I other words when my rowCount returns 1  I view 2 rows in screen one
> > > > with the data in the model a one empty. Any ideas on how avoid this
> > > > empty row.
> > > >
> > > > thanks in advance
> > >
> > > Try using http://labs.trolltech.com/page/Projects/Itemview/Modeltest
> > >
> > > It's a ModelTest and it helps to catch most of common errors in
> > > models. When you get some assertion go to line it points, and in
> > > comments it will tell you how to fix it.
> > >
> > > --
> > > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > > "unsubscribe" in the subject or the body. List archive and information:
> > > http://lists.trolltech.com/qt-interest/
>
>
>
> --
>
> Thorsen Consulting ApS - Qt consulting services
> http://www.thorsen-consulting.dk
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>


-- 
 [ signature omitted ]