Reputation: 491
I use pyqt5.
I use QTreeView with QDirModel to list files and folders.
self.dirModel = QtWidgets.QDirModel(self)
self.dirTreeView = QtWidgets.QTreeView()
self.dirTreeView.setModel(self.dirModel)
I want to add a button to change selected index, for example, when I press the button, I can select next file in current folder, the behavior is the same as press the "down" key.
What should I do?
Upvotes: 0
Views: 929
Reputation: 6666
You need to add a button to your GUI, and connect a slot to the clicked event.
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
// This layout will be your existing one
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
print ('Hello World')
Then when you have all that you need to write some code in the handleButton function which updates your index. QTreeView allows you to access the rows, so all you need is a row counter and every time increment that and ask for the next row in the tree:
indexItem = self.model.index(index.row(), 0, index.parent())
fileName = self.model.fileName(indexItem)
filePath = self.model.filePath(indexItem)
Upvotes: 1