[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] QTableWidget.cellClicked Does Not Work
From:       Python3 Lover <kenxshiba () gmail ! com>
Date:       2018-04-10 22:46:35
Message-ID: CADJnhV6QPCG9Z+Jqfx8U2csmb7sLoEVxPpubFvDv6ccUZhU94Q () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thank you, It worked.
On Tue, Apr 10, 2018 at 5:44 AM Maurizio Berti <maurizio.berti@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@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@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
>

[Attachment #5 (text/html)]

Thank you, It worked. <br><div class="gmail_quote"><div dir="ltr">On Tue, Apr 10, \
2018 at 5:44 AM Maurizio Berti &lt;<a \
href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Well, it looks like \
there are some issues in your code.<div><br></div><div>First of all, you should avoid \
using ls and parsing its output to populate your model. You can use QDir or directly \
QFileSystemModel.<br>You are connecting to cellClicked within updateUi, which is \
called only by the updateUiCellClick (hence, the model is never updated, \
obviously).</div><div>And you are trying to connect it within a for cycle, which \
would eventually result in the updateUiCellClick being called once for every \
cycle.</div><div><br></div><div>Also, your syntax in the signal connection is \
wrong.</div><div>You should use this, and only once:</div><div><br></div><div><font \
face="monospace, monospace" \
size="1">self.filesTable.cellClicked.connect(self.updateUiCellClick)</font><br></div><div><br></div><div>Once \
that&#39;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).</div><div><br></div><div>Maurizio</div></div><div \
class="gmail_extra"><br><div class="gmail_quote"></div></div><div \
class="gmail_extra"><div class="gmail_quote">2018-04-10 4:01 GMT+02:00 Python3 Lover \
<span dir="ltr">&lt;<a href="mailto:kenxshiba@gmail.com" \
target="_blank">kenxshiba@gmail.com</a>&gt;</span>:<br></div></div><div \
class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div \
dir="ltr">Hi,<div><br></div><div>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.</div><div><br></div><div>The signal part:</div><div><font \
face="monospace">self.filesTable.connect(print)#self.updateUiCellClick)</font><br></div><div>Added \
<font face="monospace">print</font>  instead of <font \
face="monospace">self.updateUiCellClick</font>  for debugging \
purposes.</div><div><font face="monospace"><br></font></div><div>Code (probably you \
do not need this):</div><div><div><font \
face="monospace">#!/usr/bin/python3</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">print(&#39;i Import \
Modules&#39;)</font></div><div><font face="monospace">print(&#39; | Import \
sys&#39;)</font></div><div><font face="monospace">import sys</font></div><div><font \
face="monospace">print(&#39; | Import PyQt5.QtCore&#39;)</font></div><div><font \
face="monospace">from PyQt5.QtCore import *</font></div><div><font \
face="monospace">print(&#39; | Import PyQt5.QtGui&#39;)</font></div><div><font \
face="monospace">from PyQt5.QtGui import *</font></div><div><font \
face="monospace">print(&#39; | Import PyQt5.QtWidgets&#39;)</font></div><div><font \
face="monospace">from PyQt5.QtWidgets import * # PyQt5 Support</font></div><div><font \
face="monospace">print(&#39; | Import os&#39;)</font></div><div><font \
face="monospace">import os</font></div><div><font face="monospace">print(&#39; | \
Import subprocess.Popen&#39;) # For backward-compatibility</font></div><div><font \
face="monospace">from subprocess import Popen, PIPE</font></div><div><font \
face="monospace">print(&#39; | Done&#39;)</font></div><div><font \
face="monospace">print(&#39;i Define class Form&#39;)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">class \
root(QMainWindow):</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
__init__(self, parent=None):</font></div><div><font face="monospace">            \
&#39;&#39;&#39;self.__init__ - Initializes \
QMainWindow&#39;&#39;&#39;</font></div><div><font face="monospace">            \
print(&#39;   self.__init__ - Initializes QMainWindow&#39;)</font></div><div><font \
face="monospace">            super(root, \
self).__init__(parent)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            # Create \
Variables</font></div><div><font face="monospace">            self.currentPath = \
&#39;/&#39;</font></div><div><font face="monospace">            \
os.chdir(self.currentPath)</font></div><div><font face="monospace">            \
self.currentItems = os.listdir()</font></div><div><font face="monospace">            \
self.currentItemsLsProcess = Popen([&#39;ls&#39;,&#39;-l&#39;], stdout=PIPE, \
stderr=PIPE)</font></div><div><font face="monospace">            \
self.currentItemsLsProcessResult = \
self.currentItemsLsProcess.communicate()</font></div><div><font face="monospace">     \
if self.currentItemsLsProcessResult[1].decode(&#39;utf-8&#39;):</font></div><div><font \
face="monospace">                  QMessageBox.warning(self,&#39;Files - ls -l \
Error&#39;,&#39;ls -l responded with non-blank stderr.Error is shown \
here:&lt;br&gt;&lt;code&gt;{}&lt;/code&gt;&lt;br&gt;&lt;hr&gt;&lt;br&gt;Error \
LsStderr (e-lsstderr)&lt;br&gt;&lt;hr&gt;&lt;br&gt;If you want to support the team, \
go to the &lt;a href=&quot;<a href="https://github.com/" \
target="_blank">https://github.com/</a>&quot;&gt;GitHub \
Repository&lt;/a&gt;.&#39;.format(self.currentItemsLsProcessResult[1].decode(&#39;utf-8&#39;)))</font></div><div><font \
face="monospace">            self.currentItemsLs = \
self.currentItemsLsProcessResult[0].decode(&#39;utf-8&#39;).split(&#39;\n&#39;)[1:-1]</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            # Create \
Table Widget</font></div><div><font face="monospace">            self.filesTable = \
QTableWidget()</font></div><div><font face="monospace"><br></font></div><div><font \
face="monospace">            # Init Table Widget</font></div><div><font \
face="monospace">            self.filesTable.clear()</font></div><div><font \
face="monospace">            \
self.filesTable.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)</font></div><div><font \
face="monospace">            \
self.filesTable.setRowCount(len(self.currentItems))</font></div><div><font \
face="monospace">            self.filesTable.setColumnCount(4)</font></div><div><font \
face="monospace">            \
self.filesTable.setHorizontalHeaderLabels([&#39;Name&#39;,&#39;TimeStamp&#39;,&#39;Type&#39;,&#39;ls \
-l&#39;])</font></div><div><font face="monospace">            # \
self.filesTable.setReadOnly(1)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            # Create \
&amp; Add Items</font></div><div><font face="monospace">            self.itemWidgets \
= [[],[],[],[]]</font></div><div><font face="monospace">            for i in \
range(len(self.currentItems)):</font></div><div><font face="monospace">               \
self.itemWidgets[0].append(QTableWidgetItem(self.currentItems[i]))</font></div><div><font \
face="monospace">                  \
self.filesTable.setItem(i,0,self.itemWidgets[0][-1])</font></div><div><font \
face="monospace">                  \
self.itemWidgets[3].append(QTableWidgetItem(self.currentItemsLs[i]))</font></div><div><font \
face="monospace">                  \
self.filesTable.setItem(i,3,self.itemWidgets[3][-1])</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            # Init \
Widgets</font></div><div><font face="monospace"><br></font></div><div><font \
face="monospace">            # Align Widgets to root</font></div><div><font \
face="monospace">            \
self.setCentralWidget(self.filesTable)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            # \
Signals-and-Slots</font></div><div><font face="monospace"><br></font></div><div><font \
face="monospace">            print(&#39;i Set self title&#39;)</font></div><div><font \
face="monospace">            \
self.setWindowTitle(&#39;{}&#39;.format(self.currentPath))</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
updateUi(self):</font></div><div><font face="monospace">            \
&#39;&#39;&#39;self.updateUi - None&#39;&#39;&#39;</font></div><div><font \
face="monospace">            os.chdir(self.currentPath)</font></div><div><font \
face="monospace">            self.currentItems = os.listdir()</font></div><div><font \
face="monospace">            self.currentItemsLsProcess = \
Popen([&#39;ls&#39;,&#39;-l&#39;], stdout=PIPE, stderr=PIPE)</font></div><div><font \
face="monospace">            self.currentItemsLsProcessResult = \
self.currentItemsLsProcess.communicate()</font></div><div><font face="monospace">     \
if self.currentItemsLsProcessResult[1].decode(&#39;utf-8&#39;):</font></div><div><font \
face="monospace">                  QMessageBox.warning(self,&#39;Files - ls -l \
Error&#39;,&#39;ls -l responded with non-blank stderr.Error is shown \
here:&lt;br&gt;&lt;code&gt;{}&lt;/code&gt;&lt;br&gt;&lt;hr&gt;&lt;br&gt;Error \
LsStderr (e-lsstderr)&lt;br&gt;&lt;hr&gt;&lt;br&gt;If you want to support the team, \
go to the &lt;a href=&quot;<a href="https://github.com/" \
target="_blank">https://github.com/</a>&quot;&gt;GitHub \
Repository&lt;/a&gt;.&#39;.format(self.currentItemsLsProcessResult[1].decode(&#39;utf-8&#39;)))</font></div><div><font \
face="monospace">            self.currentItemsLs = \
self.currentItemsLsProcessResult[0].decode(&#39;utf-8&#39;).split(&#39;\n&#39;)[1:-1]</font></div><div><font \
face="monospace">            self.filesTable.clear()</font></div><div><font \
face="monospace">            \
self.filesTable.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)</font></div><div><font \
face="monospace">            \
self.filesTable.setRowCount(len(self.currentItems))</font></div><div><font \
face="monospace">            self.filesTable.setColumnCount(4)</font></div><div><font \
face="monospace">            \
self.filesTable.setHorizontalHeaderLabels([&#39;Name&#39;,&#39;TimeStamp&#39;,&#39;Type&#39;,&#39;ls \
-l&#39;])</font></div><div><font face="monospace">            self.itemWidgets = \
[[],[],[],[]]</font></div><div><font face="monospace">            for i in \
range(len(self.currentItems)):</font></div><div><font face="monospace">               \
self.itemWidgets[0].append(QTableWidgetItem(self.currentItems[i]))</font></div><div><font \
face="monospace">                  \
self.filesTable.setItem(i,0,self.itemWidgets[0][-1])</font></div><div><font \
face="monospace">                  \
self.filesTable..connect(print)#self.updateUiCellClick)</font></div><div><font \
face="monospace">                  \
self.itemWidgets[3].append(QTableWidgetItem(self.currentItemsLs[i]))</font></div><div><font \
face="monospace">                  \
self.filesTable.setItem(i,3,self.itemWidgets[3][-1])</font></div><div><font \
face="monospace">            \
self.filesTable.resizeColumnsToContents()</font></div><div><font face="monospace">    \
self.setWindowTitle(&#39;{}&#39;.format(self.currentPath))</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
updateUiCellClick(self, row, column):</font></div><div><font face="monospace">        \
&#39;&#39;&#39;self.updateUiCellClick - None&#39;&#39;&#39;</font></div><div><font \
face="monospace">            print(&#39;self.updateUiCellClick - \
None&#39;)</font></div><div><font face="monospace">            self.currentpath += \
self.itemWidgets[0][row].text+&#39;/&#39;</font></div><div><font face="monospace">    \
self.updateUi()</font></div><div><font face="monospace"><br></font></div><div><font \
face="monospace">print(&#39; | Done&#39;)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">if __name__ == \
&#39;__main__&#39;:</font></div><div><font face="monospace">      print(&#39;i \
Execute instance&#39;)</font></div><div><font face="monospace">      app = \
QApplication(sys.argv)</font></div><div><font face="monospace">      root = \
root()</font></div><div><font face="monospace">      \
root.show()</font></div><div><font face="monospace">      \
app.exec_()</font></div><div><font face="monospace">      print(&#39; | \
Done&#39;)</font></div></div><div><font face="monospace"><br></font></div><div>Any \
help would be appreciated,</div><div>Ken</div><div><br></div></div> \
<br></blockquote></div></div><div class="gmail_extra"><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc \
solid;padding-left:1ex">_______________________________________________<br> PyQt \
mailing list      <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br></blockquote></div></div><div \
class="gmail_extra"><br><br clear="all"><div><br></div>-- <br><div \
class="m_9205270809522047379gmail_signature" data-smartmail="gmail_signature">È \
difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - \
&quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div> </div></blockquote></div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic