Results 1 to 6 of 6

Thread: how do you reload a listView?

  1. #1
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how do you reload a listView?

    how do you reload a listView? When I add a object to LIBARY I want to reload the list so all items is showed. how do I do that?


    here is my code

    Qt Code:
    1. #------------------------------------------------------------
    2. #------------------------------------------------------------
    3.  
    4. # Hantera biblotek
    5. class manageLibary(QtGui.QDialog):
    6. def __init__(self, parent):
    7. QtGui.QDialog.__init__(self, parent)
    8. self.ui = ManageLibary()
    9. self.ui.setupUi(self)
    10. self.model = manageLibaryModel(self)
    11. self.ui.listView.setModel(self.model)
    12. self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.validateInput)
    13.  
    14.  
    15. def validateInput(self):
    16. if self.ui.lineEdit.text() != "":
    17. # make validator later here for
    18. LIBARY.append(bibliotek(self.ui.lineEdit.text()))
    19. print self.ui.lineEdit.text()
    20. print len(LIBARY)
    21. self.emit(QtCore.SIGNAL("dataChanged()"))
    22.  
    23.  
    24.  
    25. #------------------------------------------------------------
    26. # Manage Libary Model
    27. #------------------------------------------------------------
    28. #
    29. class manageLibaryModel(QtCore.QAbstractListModel):
    30. def __init__(self, parent):
    31. QtCore.QAbstractListModel.__init__(self, parent)
    32.  
    33.  
    34. def data(self, index, role):
    35. if role == QtCore.Qt.DisplayRole:
    36. return LIBARY[index.row()].name
    37.  
    38. elif role == QtCore.Qt.UserRole:
    39. return LIBARY[index.row()]
    40.  
    41. def rowCount(self, parent=QtCore.QModelIndex()):
    42. return len(LIBARY)
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: how do you reload a listView?

    Your model should emit proper signals when there is something added to it. You shouldn't access the underlying datastructure directly unless the model can detect that.
    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.


  3. #3
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how do you reload a listView?

    I got it to work. But i did not use signals. how would you do in my case?

    my new code:
    Qt Code:
    1. #------------------------------------------------------------
    2. #------------------------------------------------------------
    3.  
    4. # Hantera biblotek
    5. class manageLibary(QtGui.QDialog):
    6. def __init__(self, parent):
    7. QtGui.QDialog.__init__(self, parent)
    8. self.ui = ManageLibary()
    9. self.ui.setupUi(self)
    10. self.model = manageLibaryModel(self)
    11. self.ui.listView.setModel(self.model)
    12. self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.validateInput)
    13. #for i in dir(self.ui.listView):
    14. # print i
    15.  
    16. def validateInput(self):
    17. if self.ui.lineEdit.text() != "":
    18. # make validator later here for
    19. data = bibliotek(self.ui.lineEdit.text())
    20. print self.ui.lineEdit.text()
    21. print len(LIBARY)
    22. self.model.addItem(data)
    23.  
    24.  
    25.  
    26.  
    27. #------------------------------------------------------------
    28. # Manage Libary Model
    29. #------------------------------------------------------------
    30. #
    31. class manageLibaryModel(QtCore.QAbstractListModel):
    32. def __init__(self, parent):
    33. QtCore.QAbstractListModel.__init__(self, parent)
    34.  
    35.  
    36. def data(self, index, role):
    37. if role == QtCore.Qt.DisplayRole:
    38. return LIBARY[index.row()].name
    39.  
    40. elif role == QtCore.Qt.UserRole:
    41. return LIBARY[index.row()]
    42.  
    43. def rowCount(self, parent=QtCore.QModelIndex()):
    44. return len(LIBARY)
    45.  
    46. def addItem(self, item):
    47. self.beginInsertRows(QtCore.QModelIndex(), len(LIBARY), len(LIBARY))
    48. LIBARY.append(item)
    49. self.endInsertRows()
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: how do you reload a listView?

    You are using signals. beginInsertRows() and endInsertRows() emit proper signals.
    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.


  5. #5
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how do you reload a listView?

    oh, I see . btw can you manually reload a listView or do you have to use the framework ?

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how do you reload a listView?

    If you add items to the model, they will appear in the views almost instantly.

    If the model changes the data substantially, you can use the layoutAboutToBeChanged and layoutChanged functions to emit appropriate signals.

Similar Threads

  1. Reload widget contents
    By tiredtyrant in forum Qt Programming
    Replies: 13
    Last Post: 10th March 2010, 20:39
  2. Reload a Qt Plugin
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2009, 16:36
  3. QPlainTextEdit reload text
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2009, 15:24
  4. Reload a QTableWidget
    By SailinShoes in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 12:40
  5. How to reload widget plugins?
    By victorng in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2006, 00:27

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.