[PyQt] QTableWidget.cellClicked Does Not Work

Ricardo Araoz ricaraoz at gmail.com
Tue Apr 10 14:51:45 BST 2018


Maybe I don't really get it, I haven't looked at your code for real, but 
you are trying to navigate directories and files. If that is so, why 
don't you use a QFileSystemModel() (I think you'd have to use a 
QListView), it's easier.
Here's some code I had that might get you started (you go into a 
directory with a double click, a '..' entry symbolizes parent directory):
---------------------------------------------------------------------
from pathlib import Path

class FileSystemView(QListView):

     def __init__(self, iniDir=None, parent=None):
         super(FileSystemView, self).__init__(parent)

         if not iniDir:
             iniDir = Path.cwd()

         self.fileModel = QFileSystemModel()
         self.fileModel.setFilter(QDir.NoDot | QDir.Files | QDir.Dirs)
         rootIndex = self.fileModel.setRootPath(str(iniDir))

         self.setModel(self.fileModel)
         self.setRootIndex(rootIndex)
         self.curDir = str(iniDir)

         self.setSelectionMode(QAbstractItemView.ExtendedSelection)

         self.doubleClicked.connect(self.fileDblClicked)


     def fileDblClicked(self, index):
         fm = self.fileModel
         if fm.isDir(index):
             if Path(fm.filePath(index)).stem == '..':
                 dirPath = fm.filePath(fm.parent(fm.parent(index)))
                 rootIndex = fm.setRootPath(dirPath)
                 self.setRootIndex(rootIndex)
                 self.curDir = str(Path(fm.filePath(
self.currentIndex())).parent.parent)
             else:
                 rootIndex = fm.setRootPath(fm.filePath(index))
                 self.setRootIndex(rootIndex)
                 self.curDir = fm.filePath(self.currentIndex())
---------------------------------------------------------------------


On 09/04/18 23:01, Python3 Lover wrote:
> Hi,
>
> I am trying to make a simple files app (or, file explorer app) and I 
> am using the QTableWidget to Display the files and directories. When 
> the user clicks an directory, I want the program to jump to that 
> directory. I have used the QTableWidget.cellClicked signal, and it 
> does not currently work.
>
> The signal part:
> self.filesTable.connect(print)#self.updateUiCellClick)
> Added print instead of self.updateUiCellClick for debugging purposes.
>
> Code (probably you do not need this):
> #!/usr/bin/python3
>
> print('i Import Modules')
> print(' | Import sys')
> import sys
> print(' | Import PyQt5.QtCore')
> from PyQt5.QtCore import *
> print(' | Import PyQt5.QtGui')
> from PyQt5.QtGui import *
> print(' | Import PyQt5.QtWidgets')
> from PyQt5.QtWidgets import * # PyQt5 Support
> print(' | Import os')
> import os
> print(' | Import subprocess.Popen') # For backward-compatibility
> from subprocess import Popen, PIPE
> print(' | Done')
> print('i Define class Form')
>
> class root(QMainWindow):
>
>     def __init__(self, parent=None):
>         '''self.__init__ - Initializes QMainWindow'''
>         print('  self.__init__ - Initializes QMainWindow')
>         super(root, self).__init__(parent)
>
>         # Create Variables
>         self.currentPath = '/'
>         os.chdir(self.currentPath)
>         self.currentItems = os.listdir()
>         self.currentItemsLsProcess = Popen(['ls','-l'], stdout=PIPE, 
> stderr=PIPE)
> self.currentItemsLsProcessResult = 
> self.currentItemsLsProcess.communicate()
>         if self.currentItemsLsProcessResult[1].decode('utf-8'):
> QMessageBox.warning(self,'Files - ls -l Error','ls -l responded with 
> non-blank stderr.Error is shown 
> here:<br><code>{}</code><br><hr><br>Error LsStderr 
> (e-lsstderr)<br><hr><br>If you want to support the team, go to the <a 
> href="https://github.com/">GitHub 
> Repository</a>.'.format(self.currentItemsLsProcessResult[1].decode('utf-8')))
>         self.currentItemsLs = 
> self.currentItemsLsProcessResult[0].decode('utf-8').split('\n')[1:-1]
>
>         # Create Table Widget
>         self.filesTable = QTableWidget()
>
>         # Init Table Widget
>         self.filesTable.clear()
> self.filesTable.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
> self.filesTable.setRowCount(len(self.currentItems))
> self.filesTable.setColumnCount(4)
> self.filesTable.setHorizontalHeaderLabels(['Name','TimeStamp','Type','ls 
> -l'])
>         # self.filesTable.setReadOnly(1)
>
>         # Create & Add Items
>         self.itemWidgets = [[],[],[],[]]
>         for i in range(len(self.currentItems)):
> self.itemWidgets[0].append(QTableWidgetItem(self.currentItems[i]))
> self.filesTable.setItem(i,0,self.itemWidgets[0][-1])
> self.itemWidgets[3].append(QTableWidgetItem(self.currentItemsLs[i]))
> self.filesTable.setItem(i,3,self.itemWidgets[3][-1])
>
>         # Init Widgets
>
>         # Align Widgets to root
> self.setCentralWidget(self.filesTable)
>
>         # Signals-and-Slots
>
>         print('i Set self title')
> self.setWindowTitle('{}'.format(self.currentPath))
>
>     def updateUi(self):
>         '''self.updateUi - None'''
>         os.chdir(self.currentPath)
>         self.currentItems = os.listdir()
>         self.currentItemsLsProcess = Popen(['ls','-l'], stdout=PIPE, 
> stderr=PIPE)
> self.currentItemsLsProcessResult = 
> self.currentItemsLsProcess.communicate()
>         if self.currentItemsLsProcessResult[1].decode('utf-8'):
> QMessageBox.warning(self,'Files - ls -l Error','ls -l responded with 
> non-blank stderr.Error is shown 
> here:<br><code>{}</code><br><hr><br>Error LsStderr 
> (e-lsstderr)<br><hr><br>If you want to support the team, go to the <a 
> href="https://github.com/">GitHub 
> Repository</a>.'.format(self.currentItemsLsProcessResult[1].decode('utf-8')))
>         self.currentItemsLs = 
> self.currentItemsLsProcessResult[0].decode('utf-8').split('\n')[1:-1]
>         self.filesTable.clear()
> self.filesTable.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
> self.filesTable.setRowCount(len(self.currentItems))
> self.filesTable.setColumnCount(4)
> self.filesTable.setHorizontalHeaderLabels(['Name','TimeStamp','Type','ls 
> -l'])
>         self.itemWidgets = [[],[],[],[]]
>         for i in range(len(self.currentItems)):
> self.itemWidgets[0].append(QTableWidgetItem(self.currentItems[i]))
> self.filesTable.setItem(i,0,self.itemWidgets[0][-1])
> self.filesTable..connect(print)#self.updateUiCellClick)
> self.itemWidgets[3].append(QTableWidgetItem(self.currentItemsLs[i]))
> self.filesTable.setItem(i,3,self.itemWidgets[3][-1])
> self.filesTable.resizeColumnsToContents()
> self.setWindowTitle('{}'.format(self.currentPath))
>
>     def updateUiCellClick(self, row, column):
>         '''self.updateUiCellClick - None'''
> print('self.updateUiCellClick - None')
>         self.currentpath += self.itemWidgets[0][row].text+'/'
>         self.updateUi()
>
> print(' | Done')
>
> if __name__ == '__main__':
>     print('i Execute instance')
>     app = QApplication(sys.argv)
>     root = root()
>     root.show()
>     app.exec_()
>     print(' | Done')
>
> Any help would be appreciated,
> Ken
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180410/21b9359d/attachment.html>


More information about the PyQt mailing list