Results 1 to 4 of 4

Thread: getting data & QModelIndex

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default getting data & QModelIndex

    [PYTHON]

    I have a def

    Qt Code:
    1. def sizeHint(self, option, index)
    To copy to clipboard, switch view to plain text mode 

    witch receives 2 args, QStyleOptionViewItem and QModelIndex. (its a reimplementation of sizeHint, for drawing items in a qtreeview).
    As you can guess, I can't access the model where I have all the data because the def is located in another class. So, the question is, ¿how can I get the data of certain column, at the row from the "index" value, having the qmodelindex?

    I have been looking at the docs, and I can do a index.model() witch will return me a QAbstractItemModel, but I got stuck there.

    What should I do? Should I access the model from the other class? If there is a way of doing what I want to do without having to access the model from the another class it will be better.

    Thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: getting data & QModelIndex

    You can use the model->index function in a call to data. Something like:
    Qt Code:
    1. const QAbstractItemModel * model = index.model();
    2. QVariant vt = model->data(model->index(index.row(), column, index.parent()), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting data & QModelIndex

    That works great! =D Thanks!

    However, I have some little problem.
    I have a QStandardItem with a boolean value. (in column 4)

    Qt Code:
    1. model = index.model()
    2. vt = model.data(model.index(index.row(), 4, index.parent()), Qt.DisplayRole)
    3. if vt.toBool() == False:
    4. print "Nothing here"
    5. else:
    6. print "Hey! I found something!"
    To copy to clipboard, switch view to plain text mode 

    I guess that .toBool tries to convert a QStandardItem to a boolean value instead of convert the value contained in the qstandarditem, and that's why it always return false when I'm sure that there are "true" items.

    The .convert method from QVariant doesn't seem to be able to convert to qstandarditem, so, how could I do it? How could I convert the QVariant to QStandardItem?

  4. #4
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting data & QModelIndex

    Oh, I made it . It was the role... I forgot to chage it to "DecorationRole".

    Thanks for the help!

Similar Threads

  1. Replies: 6
    Last Post: 6th August 2009, 17:18
  2. beginInsertRows was not declared ???
    By travlr in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2009, 21:22
  3. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 17:31
  4. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14
  5. Replies: 6
    Last Post: 21st September 2007, 13:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.