[PyQt] QTableWidget.cellClicked Does Not Work

Python3 Lover kenxshiba at gmail.com
Tue Apr 10 23:46:35 BST 2018


Thank you, It worked.
On Tue, Apr 10, 2018 at 5:44 AM Maurizio Berti <maurizio.berti at gmail.com>
wrote:

> Well, it looks like there are some issues in your code.
>
> First of all, you should avoid using ls and parsing its output to populate
> your model. You can use QDir or directly QFileSystemModel.
> You are connecting to cellClicked within updateUi, which is called only by
> the updateUiCellClick (hence, the model is never updated, obviously).
> And you are trying to connect it within a for cycle, which would
> eventually result in the updateUiCellClick being called once for every
> cycle.
>
> Also, your syntax in the signal connection is wrong.
> You should use this, and only once:
>
> self.filesTable.cellClicked.connect(self.updateUiCellClick)
>
> Once that's set in the __init__, the signal is fired correctly (but the
> script will give an error, since the method is incomplete and has other
> issues).
>
> Maurizio
>
> 2018-04-10 4:01 GMT+02:00 Python3 Lover <kenxshiba at gmail.com>:
>
>> 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
>>
>
>
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180410/a9eb103b/attachment-0001.html>


More information about the PyQt mailing list