[PyQt] QTableWidget.cellClicked Does Not Work

Python3 Lover kenxshiba at gmail.com
Tue Apr 10 03:01:24 BST 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180410/f073d757/attachment.html>


More information about the PyQt mailing list