Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to scrollTo the desired item when the root of model changed
Forum Updated to NodeBB v4.3 + New Features

How to scrollTo the desired item when the root of model changed

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 245 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ridincal
    wrote on last edited by
    #1

    I've used QListView and QFileSystemModel to show local files. I expect to locate specific file by changing root directory and scrolling to show it, but scrollTo() does not work.

    def locateFile(self, fileDIr, filePath):
            self.listView.setRootIndex(self.listView.model().setRootPath(fileDir)
            posidx = self.listView.model().index(filePath)
            self.listView.setCurrentIndex(posidx)
            self.listView.scrollTo(posidx)
    

    How to resolve this issue?

    JonBJ 1 Reply Last reply
    0
    • R ridincal

      I've used QListView and QFileSystemModel to show local files. I expect to locate specific file by changing root directory and scrolling to show it, but scrollTo() does not work.

      def locateFile(self, fileDIr, filePath):
              self.listView.setRootIndex(self.listView.model().setRootPath(fileDir)
              posidx = self.listView.model().index(filePath)
              self.listView.setCurrentIndex(posidx)
              self.listView.scrollTo(posidx)
      

      How to resolve this issue?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ridincal
      Show the value of posidx at least. Also please post code that actually compiles/is your actual code, the above does/is not, and then all bets are off.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ridincal
        wrote on last edited by ridincal
        #3

        @JonB

        import sys
        from PyQt5 import QtCore, QtGui, QtWidgets
        
        class Ui_MainWindow(object):
            def setupUi(self, MainWindow):
                MainWindow.setObjectName("MainWindow")
                MainWindow.resize(800, 600)
                self.centralwidget = QtWidgets.QWidget(MainWindow)
                self.centralwidget.setObjectName("centralwidget")
                MainWindow.setCentralWidget(self.centralwidget)
                self.listView = QtWidgets.QListView(self.centralwidget)
                self.listView.setGeometry(QtCore.QRect(20, 10, 531, 521))
                self.listView.setObjectName("listView")
                self.model = QtWidgets.QFileSystemModel()
                self.listView.setModel(self.model)
                self.locateFile("D:\\=test=\\fonts", "D:\\=test=\\fonts\\HYYouYuanTiW.ttf")
                self.retranslateUi(MainWindow)
                QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
            def retranslateUi(self, MainWindow):
                _translate = QtCore.QCoreApplication.translate
                MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        
            def locateFile(self, fileDir, filePath):
                self.listView.setRootIndex(self.listView.model().setRootPath(fileDir))
                posidx = self.listView.model().index(filePath)
                self.listView.setCurrentIndex(posidx)
                self.listView.scrollTo(posidx)
        
        class AppMainWnd(QMainWindow , Ui_MainWindow):
            def __init__(self, parent=None):
                super(AppMainWnd, self).__init__(parent)
                self.setupUi(self)
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            AppWnd = AppMainWnd()
            sys.exit(app.exec_())
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved