Results 1 to 2 of 2

Thread: QListView only displaying last item

  1. #1
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4 Qt Jambi
    Platforms
    MacOS X

    Default QListView only displaying last item

    Hi everyone,

    I am totally new to QT(Jambi) and tried to create a simple application with a QListView
    that should contain some numbers. Unfortunately only the last number is displayed.
    What am I doing wrong?

    Here is the code of my two classes:

    Qt Code:
    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import com.trolltech.qt.gui.QApplication;
    4. import com.trolltech.qt.gui.QMainWindow;
    5. import com.trolltech.qt.gui.QWidget;
    6.  
    7. public class ListBox extends QMainWindow
    8. {
    9. Ui_MainWindow ui = new Ui_MainWindow();
    10.  
    11. public static void main(String[] args)
    12. {
    13. QApplication.initialize(args);
    14. ListBox gnah = new ListBox();
    15. gnah.show();
    16. QApplication.exec();
    17. }
    18.  
    19. public ListBox()
    20. {
    21. ui.setupUi(this);
    22. initialize();
    23. }
    24.  
    25. public ListBox(QWidget parent)
    26. {
    27. super(parent);
    28. ui.setupUi(this);
    29. initialize();
    30. }
    31.  
    32. private void initialize()
    33. {
    34. List<Integer> data = new ArrayList<Integer>();
    35. data.add(42);
    36. data.add(666);
    37. data.add(1337);
    38.  
    39. ListBoxModel model = new ListBoxModel(data);
    40.  
    41. this.ui.listView.setModel(model);
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import java.util.List;
    2. import com.trolltech.qt.core.QAbstractListModel;
    3. import com.trolltech.qt.core.QModelIndex;
    4. import com.trolltech.qt.core.Qt.ItemDataRole;
    5.  
    6. public class ListBoxModel extends QAbstractListModel
    7. {
    8. private List<Integer> data;
    9.  
    10. public ListBoxModel(List<Integer> data)
    11. {
    12. this.data = data;
    13. }
    14.  
    15. @Override
    16. public Object data(QModelIndex arg0, int arg1)
    17. {
    18. if(arg1 == ItemDataRole.DisplayRole)
    19. {
    20. return data.get(arg0.row());
    21. }
    22. else
    23. {
    24. return null;
    25. }
    26. }
    27.  
    28. @Override
    29. public int rowCount(QModelIndex arg0)
    30. {
    31. return data.size();
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListView only displaying last item

    I don't know how it looks in Java, but rowCount() and data() are const methods, maybe that's something you forgot about. Are your implementations being called at all?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to Modify QListView item height?
    By blackfox in forum Qt Programming
    Replies: 7
    Last Post: 16th May 2012, 12:38
  2. find item in QListView
    By AD in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 14:50
  3. QListView item alignment
    By innerhippy in forum Qt Programming
    Replies: 2
    Last Post: 11th November 2008, 10:32
  4. how to move item up and down in QListView
    By zhanglr in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2008, 15:39
  5. Displaying Multiple Columns in QListView
    By millard in forum Qt Programming
    Replies: 8
    Last Post: 5th December 2007, 13:03

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.