How to execute a function on click of empty area of QTreeWidget Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!How do I check if a list is empty?How to flush output of print function?How do I remove/delete a folder that is not empty with Python?How to return multiple values from a function?How to make a chain of function decorators?How do I get time of a Python program's execution?double click to edit a QtreeWidget columnHow to find the parent of item when clicked in empty space PyQt4 QTreeWidgetQTreeWidgetItem.addChild() doesn't work in some cases?PyQt5 to PySide2, loading UI-Files in different classes

How to align text above triangle figure

What causes the vertical darker bands in my photo?

Can any chord be converted to its roman numeral equivalent?

Is the Standard Deduction better than Itemized when both are the same amount?

Why did the Falcon Heavy center core fall off the ASDS OCISLY barge?

What exactly is a "Meth" in Altered Carbon?

What does the "x" in "x86" represent?

Identify plant with long narrow paired leaves and reddish stems

51k Euros annually for a family of 4 in Berlin: Is it enough?

Dating a Former Employee

What to do with chalk when deepwater soloing?

Can a non-EU citizen traveling with me come with me through the EU passport line?

A coin, having probability p of landing heads and probability of q=(1-p) of landing on heads.

Why did the rest of the Eastern Bloc not invade Yugoslavia?

Overriding an object in memory with placement new

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

What is a non-alternating simple group with big order, but relatively few conjugacy classes?

Using audio cues to encourage good posture

Why am I getting the error "non-boolean type specified in a context where a condition is expected" for this request?

3 doors, three guards, one stone

List *all* the tuples!

Output the ŋarâþ crîþ alphabet song without using (m)any letters

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

How do pianists reach extremely loud dynamics?



How to execute a function on click of empty area of QTreeWidget



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!How do I check if a list is empty?How to flush output of print function?How do I remove/delete a folder that is not empty with Python?How to return multiple values from a function?How to make a chain of function decorators?How do I get time of a Python program's execution?double click to edit a QtreeWidget columnHow to find the parent of item when clicked in empty space PyQt4 QTreeWidgetQTreeWidgetItem.addChild() doesn't work in some cases?PyQt5 to PySide2, loading UI-Files in different classes



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am using Python3.6 and PySide2



I have a treewidget-A populated with employee list.
on click on employee (treewidget-A item) a another treeWidget-B will populate.



Now if I click on empty area (deselect item) but treeWidget-B still showing last clicked employee item details.



This is to populate treewidget items



self.treeWidget_A.itemClicked.connect(self.populate_employee)
self.treeWidget_A.currentItemChanged.connect(self.populate_employee)


How to execute clear_employee_data() function on click on empty area of QtreeWidget ?



def clear_employee_data(self):
self.treeWidget_B.clear()









share|improve this question






























    1















    I am using Python3.6 and PySide2



    I have a treewidget-A populated with employee list.
    on click on employee (treewidget-A item) a another treeWidget-B will populate.



    Now if I click on empty area (deselect item) but treeWidget-B still showing last clicked employee item details.



    This is to populate treewidget items



    self.treeWidget_A.itemClicked.connect(self.populate_employee)
    self.treeWidget_A.currentItemChanged.connect(self.populate_employee)


    How to execute clear_employee_data() function on click on empty area of QtreeWidget ?



    def clear_employee_data(self):
    self.treeWidget_B.clear()









    share|improve this question


























      1












      1








      1








      I am using Python3.6 and PySide2



      I have a treewidget-A populated with employee list.
      on click on employee (treewidget-A item) a another treeWidget-B will populate.



      Now if I click on empty area (deselect item) but treeWidget-B still showing last clicked employee item details.



      This is to populate treewidget items



      self.treeWidget_A.itemClicked.connect(self.populate_employee)
      self.treeWidget_A.currentItemChanged.connect(self.populate_employee)


      How to execute clear_employee_data() function on click on empty area of QtreeWidget ?



      def clear_employee_data(self):
      self.treeWidget_B.clear()









      share|improve this question
















      I am using Python3.6 and PySide2



      I have a treewidget-A populated with employee list.
      on click on employee (treewidget-A item) a another treeWidget-B will populate.



      Now if I click on empty area (deselect item) but treeWidget-B still showing last clicked employee item details.



      This is to populate treewidget items



      self.treeWidget_A.itemClicked.connect(self.populate_employee)
      self.treeWidget_A.currentItemChanged.connect(self.populate_employee)


      How to execute clear_employee_data() function on click on empty area of QtreeWidget ?



      def clear_employee_data(self):
      self.treeWidget_B.clear()






      python pyside2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 8:58









      eyllanesc

      88.4k103564




      88.4k103564










      asked Mar 22 at 8:52









      Rajiv SharmaRajiv Sharma

      2,5612337




      2,5612337






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to detect the click and verify that there is not an item:



          from PySide2 import QtCore, QtWidgets

          class TreeWidget(QtWidgets.QTreeWidget):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(TreeWidget, self).__init__(parent)
          for i in range(2):
          it = QtWidgets.QTreeWidgetItem(self, ["item-".format(i)])
          for j in range(3):
          child_it = QtWidgets.QTreeWidgetItem(it, ["item--".format(i, j)])
          self.expandAll()

          def mousePressEvent(self, event):
          super(TreeWidget, self).mousePressEvent(event)
          if not self.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()

          @QtCore.Slot()
          def on_empty_clicked():
          print("clicked")

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = TreeWidget()
          w.emptyClicked.connect(on_empty_clicked)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())



          With Qt Designer use eventfilter:



          from PySide2 import QtCore, QtGui, QtWidgets

          from design import Ui_MainWindow

          class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.setupUi(self)
          self.treeWidget_A.viewport().installEventFilter(self)
          self.emptyClicked.connect(self.on_emptyClicked)

          def eventFilter(self, obj, event):
          if obj is self.treeWidget_A.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:
          if not self.treeWidget_A.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()
          return super(MainWindow, self).eventFilter(obj, event)

          @QtCore.Slot()
          def on_emptyClicked(self):
          print("empty")

          if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer

























          • I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

            – Rajiv Sharma
            Mar 22 at 9:26











          • @RajivSharma What template have you used: MainWindow, Dialog or Widget?

            – eyllanesc
            Mar 22 at 9:28











          • QtWidgets.QMainWindow

            – Rajiv Sharma
            Mar 22 at 9:32











          • @RajivSharma see my update

            – eyllanesc
            Mar 22 at 9:38











          • Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

            – Rajiv Sharma
            Mar 22 at 10:12











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295958%2fhow-to-execute-a-function-on-click-of-empty-area-of-qtreewidget%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to detect the click and verify that there is not an item:



          from PySide2 import QtCore, QtWidgets

          class TreeWidget(QtWidgets.QTreeWidget):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(TreeWidget, self).__init__(parent)
          for i in range(2):
          it = QtWidgets.QTreeWidgetItem(self, ["item-".format(i)])
          for j in range(3):
          child_it = QtWidgets.QTreeWidgetItem(it, ["item--".format(i, j)])
          self.expandAll()

          def mousePressEvent(self, event):
          super(TreeWidget, self).mousePressEvent(event)
          if not self.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()

          @QtCore.Slot()
          def on_empty_clicked():
          print("clicked")

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = TreeWidget()
          w.emptyClicked.connect(on_empty_clicked)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())



          With Qt Designer use eventfilter:



          from PySide2 import QtCore, QtGui, QtWidgets

          from design import Ui_MainWindow

          class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.setupUi(self)
          self.treeWidget_A.viewport().installEventFilter(self)
          self.emptyClicked.connect(self.on_emptyClicked)

          def eventFilter(self, obj, event):
          if obj is self.treeWidget_A.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:
          if not self.treeWidget_A.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()
          return super(MainWindow, self).eventFilter(obj, event)

          @QtCore.Slot()
          def on_emptyClicked(self):
          print("empty")

          if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer

























          • I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

            – Rajiv Sharma
            Mar 22 at 9:26











          • @RajivSharma What template have you used: MainWindow, Dialog or Widget?

            – eyllanesc
            Mar 22 at 9:28











          • QtWidgets.QMainWindow

            – Rajiv Sharma
            Mar 22 at 9:32











          • @RajivSharma see my update

            – eyllanesc
            Mar 22 at 9:38











          • Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

            – Rajiv Sharma
            Mar 22 at 10:12















          1














          You have to detect the click and verify that there is not an item:



          from PySide2 import QtCore, QtWidgets

          class TreeWidget(QtWidgets.QTreeWidget):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(TreeWidget, self).__init__(parent)
          for i in range(2):
          it = QtWidgets.QTreeWidgetItem(self, ["item-".format(i)])
          for j in range(3):
          child_it = QtWidgets.QTreeWidgetItem(it, ["item--".format(i, j)])
          self.expandAll()

          def mousePressEvent(self, event):
          super(TreeWidget, self).mousePressEvent(event)
          if not self.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()

          @QtCore.Slot()
          def on_empty_clicked():
          print("clicked")

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = TreeWidget()
          w.emptyClicked.connect(on_empty_clicked)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())



          With Qt Designer use eventfilter:



          from PySide2 import QtCore, QtGui, QtWidgets

          from design import Ui_MainWindow

          class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.setupUi(self)
          self.treeWidget_A.viewport().installEventFilter(self)
          self.emptyClicked.connect(self.on_emptyClicked)

          def eventFilter(self, obj, event):
          if obj is self.treeWidget_A.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:
          if not self.treeWidget_A.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()
          return super(MainWindow, self).eventFilter(obj, event)

          @QtCore.Slot()
          def on_emptyClicked(self):
          print("empty")

          if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer

























          • I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

            – Rajiv Sharma
            Mar 22 at 9:26











          • @RajivSharma What template have you used: MainWindow, Dialog or Widget?

            – eyllanesc
            Mar 22 at 9:28











          • QtWidgets.QMainWindow

            – Rajiv Sharma
            Mar 22 at 9:32











          • @RajivSharma see my update

            – eyllanesc
            Mar 22 at 9:38











          • Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

            – Rajiv Sharma
            Mar 22 at 10:12













          1












          1








          1







          You have to detect the click and verify that there is not an item:



          from PySide2 import QtCore, QtWidgets

          class TreeWidget(QtWidgets.QTreeWidget):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(TreeWidget, self).__init__(parent)
          for i in range(2):
          it = QtWidgets.QTreeWidgetItem(self, ["item-".format(i)])
          for j in range(3):
          child_it = QtWidgets.QTreeWidgetItem(it, ["item--".format(i, j)])
          self.expandAll()

          def mousePressEvent(self, event):
          super(TreeWidget, self).mousePressEvent(event)
          if not self.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()

          @QtCore.Slot()
          def on_empty_clicked():
          print("clicked")

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = TreeWidget()
          w.emptyClicked.connect(on_empty_clicked)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())



          With Qt Designer use eventfilter:



          from PySide2 import QtCore, QtGui, QtWidgets

          from design import Ui_MainWindow

          class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.setupUi(self)
          self.treeWidget_A.viewport().installEventFilter(self)
          self.emptyClicked.connect(self.on_emptyClicked)

          def eventFilter(self, obj, event):
          if obj is self.treeWidget_A.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:
          if not self.treeWidget_A.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()
          return super(MainWindow, self).eventFilter(obj, event)

          @QtCore.Slot()
          def on_emptyClicked(self):
          print("empty")

          if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())





          share|improve this answer















          You have to detect the click and verify that there is not an item:



          from PySide2 import QtCore, QtWidgets

          class TreeWidget(QtWidgets.QTreeWidget):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(TreeWidget, self).__init__(parent)
          for i in range(2):
          it = QtWidgets.QTreeWidgetItem(self, ["item-".format(i)])
          for j in range(3):
          child_it = QtWidgets.QTreeWidgetItem(it, ["item--".format(i, j)])
          self.expandAll()

          def mousePressEvent(self, event):
          super(TreeWidget, self).mousePressEvent(event)
          if not self.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()

          @QtCore.Slot()
          def on_empty_clicked():
          print("clicked")

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = TreeWidget()
          w.emptyClicked.connect(on_empty_clicked)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())



          With Qt Designer use eventfilter:



          from PySide2 import QtCore, QtGui, QtWidgets

          from design import Ui_MainWindow

          class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
          emptyClicked = QtCore.Signal()

          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.setupUi(self)
          self.treeWidget_A.viewport().installEventFilter(self)
          self.emptyClicked.connect(self.on_emptyClicked)

          def eventFilter(self, obj, event):
          if obj is self.treeWidget_A.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:
          if not self.treeWidget_A.indexAt(event.pos()).isValid():
          self.emptyClicked.emit()
          return super(MainWindow, self).eventFilter(obj, event)

          @QtCore.Slot()
          def on_emptyClicked(self):
          print("empty")

          if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 22 at 17:17

























          answered Mar 22 at 9:10









          eyllanesceyllanesc

          88.4k103564




          88.4k103564












          • I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

            – Rajiv Sharma
            Mar 22 at 9:26











          • @RajivSharma What template have you used: MainWindow, Dialog or Widget?

            – eyllanesc
            Mar 22 at 9:28











          • QtWidgets.QMainWindow

            – Rajiv Sharma
            Mar 22 at 9:32











          • @RajivSharma see my update

            – eyllanesc
            Mar 22 at 9:38











          • Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

            – Rajiv Sharma
            Mar 22 at 10:12

















          • I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

            – Rajiv Sharma
            Mar 22 at 9:26











          • @RajivSharma What template have you used: MainWindow, Dialog or Widget?

            – eyllanesc
            Mar 22 at 9:28











          • QtWidgets.QMainWindow

            – Rajiv Sharma
            Mar 22 at 9:32











          • @RajivSharma see my update

            – eyllanesc
            Mar 22 at 9:38











          • Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

            – Rajiv Sharma
            Mar 22 at 10:12
















          I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

          – Rajiv Sharma
          Mar 22 at 9:26





          I used Qt Designer to draw UI and later I convert UI to Py File using Pyuic. I did not created Treewidget class. How can I use this example ?

          – Rajiv Sharma
          Mar 22 at 9:26













          @RajivSharma What template have you used: MainWindow, Dialog or Widget?

          – eyllanesc
          Mar 22 at 9:28





          @RajivSharma What template have you used: MainWindow, Dialog or Widget?

          – eyllanesc
          Mar 22 at 9:28













          QtWidgets.QMainWindow

          – Rajiv Sharma
          Mar 22 at 9:32





          QtWidgets.QMainWindow

          – Rajiv Sharma
          Mar 22 at 9:32













          @RajivSharma see my update

          – eyllanesc
          Mar 22 at 9:38





          @RajivSharma see my update

          – eyllanesc
          Mar 22 at 9:38













          Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

          – Rajiv Sharma
          Mar 22 at 10:12





          Thanks a lot Its working now ! and Its good to learn installEventFilter Thanks again.

          – Rajiv Sharma
          Mar 22 at 10:12



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295958%2fhow-to-execute-a-function-on-click-of-empty-area-of-qtreewidget%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

          밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

          1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴