Results 1 to 5 of 5

Thread: Something fishy about Model, data doesn't show up!

  1. #1
    Join Date
    Aug 2008
    Location
    Prague
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Something fishy about Model, data doesn't show up!

    Hi, I am using Qt Jambi. I was trying to subclass QAbstractListModel and use it with QListView. But no data appears at all. Here is code of my subclassed model:

    Qt Code:
    1. class SmenaDispeceriModel extends QAbstractListModel
    2. {
    3. private List dispeceri = new ArrayList(); // data
    4.  
    5. public int rowCount(QModelIndex parent)
    6. {
    7. return dispeceri.size();
    8. }
    9.  
    10. public Object data(QModelIndex index, int role)
    11. {
    12. if (index.row() < 0 || index.row() >= dispeceri.size())
    13. return new Object();
    14.  
    15. if (role == Qt.ItemDataRole.DisplayRole)
    16. return ((SmenaDispecer) dispeceri.get(index.row())).getDlouheJmeno();
    17. else
    18. return new Object();
    19. }
    20.  
    21. public void addDispecera(Long id, String jmeno, String prijmeni)
    22. {
    23. dispeceri.add(new SmenaDispecer(id, jmeno, prijmeni));
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Now in dialog I have following sample code:

    Qt Code:
    1. dispeceriModel = new SmenaDispeceriModel();
    2.  
    3. dispeceriModel.addDispecera(new Long(1), "Karel", "Novak");
    4. dispeceriModel.addDispecera(new Long(2), "Tomas", "Marny");
    5. ui.listDispeceri.setModel(dispeceriModel);
    To copy to clipboard, switch view to plain text mode 

    But the view is empty! When I tried following code, it works fine:
    Qt Code:
    1. model = new QStandardItemModel();
    2. model.setRowCount(2);
    3. model.setColumnCount(1);
    4. model.setData(model.index(0, 0), "Some text");
    5. model.setData(model.index(1, 0), "Other text");
    6. ui.listDispeceri.setModel(model);
    To copy to clipboard, switch view to plain text mode 

    So there have to be some problem with my model, but i dont know where. Please help.

    Thanks.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Something fishy about Model, data doesn't show up!

    Try implementing columnCount() as well.
    If that does not help: add debug-statements in data() to check if/for what indexes/roles it gets called.

    HTH

  3. #3
    Join Date
    Aug 2008
    Location
    Prague
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something fishy about Model, data doesn't show up!

    Hi,

    I cant implement columnCount, because I use QAbstractListModel, which has it already implemented.

    I addded debuging statement and I got following:
    Qt Code:
    1. public Object data(QModelIndex index, int role)
    2. {
    3. System.out.println(index);
    4. System.out.println(role);
    5. if (index.row() < 0 || index.row() >= dispeceri.size())
    6. return new Object();
    7.  
    8. if (role == Qt.ItemDataRole.DisplayRole)
    9. return ((SmenaDispecer) dispeceri.get(index.row())).getDlouheJmeno();
    10. else
    11. return new Object();
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Prague
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something fishy about Model, data doesn't show up!

    Hi,

    I cant implement columnCount, because I use QAbstractListModel, which has it already implemented.

    I addded debuging statement:
    Qt Code:
    1. public Object data(QModelIndex index, int role)
    2. {
    3. System.out.println(index);
    4. System.out.println(role);
    5. if (index.row() < 0 || index.row() >= dispeceri.size())
    6. return new Object();
    7.  
    8. if (role == Qt.ItemDataRole.DisplayRole)
    9. return ((SmenaDispecer) dispeceri.get(index.row())).getDlouheJmeno();
    10. else
    11. return new Object();
    12. }
    To copy to clipboard, switch view to plain text mode 

    and got following (running in ant):
    Qt Code:
    1. [java] QModelIndex(row=0,col=0,internal=0)
    2. [java] 13
    3. [java] QModelIndex(row=1,col=0,internal=0)
    4. [java] 13
    5. [java] QModelIndex(row=2,col=0,internal=0)
    6. [java] 13
    7. [java] QModelIndex(row=3,col=0,internal=0)
    8. [java] 13
    9. [java] QModelIndex(row=4,col=0,internal=0)
    10. [java] 13
    To copy to clipboard, switch view to plain text mode 

    Pretty weard is, that when I use QTableView instead of QListView, everything works fine!

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Something fishy about Model, data doesn't show up!

    I am not experienced with Jambi.
    And the code does look ok to me.

    Try calling the base class instead of 'only' returning "new Object()".

Similar Threads

  1. Clearing data model
    By db in forum Newbie
    Replies: 6
    Last Post: 16th February 2008, 13:10
  2. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  3. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30
  4. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 19:58
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59

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.