Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to clear QVBoxLayout by a Signal?

How to clear QVBoxLayout by a Signal?

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5
6 Posts 4 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    porsol
    wrote on 25 Mar 2018, 18:10 last edited by porsol
    #1

    I'm trying to clear QVBoxLayout with self.vbox.takeAt(0), but previous vboxes clog up the QWidget and would not go away. New appear on top of the old ones.

    #! /usr/bin/env python
    from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QHBoxLayout
    from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot
    import time, random
    
    class Worker(QObject):
    	CLEAR_VBOX = pyqtSignal()
    	intReady = pyqtSignal(int)
    	
    	@pyqtSlot()
    	def procCounter(self):
    		for i in range(1, 100):
    			self.CLEAR_VBOX.emit()
    			self.intReady.emit(i)
    			time.sleep(1)
    
    class Form(QWidget):
    	def __init__(self):
    		super().__init__()
    		
    		self.obj = Worker()
    		self.thread = QThread()
    		self.obj.intReady.connect(self.onIntReady)
    		self.obj.moveToThread(self.thread)
    
    		self.obj.CLEAR_VBOX.connect(self.clearvbox)
    		
    		self.thread.started.connect(self.obj.procCounter)
    		self.thread.start()
    		self.initUI()
    
    
    	def initUI(self):
    		self.vbox = QVBoxLayout()
    		self.setLayout(self.vbox)
    		
    		self.setGeometry(333, 333, 222, 222)
    		self.show()
    		
    	def clearvbox(self):
    		while self.vbox.count():
    			# ~ # break
    			self.vbox.takeAt(0)
    	
    	def onIntReady(self, i):
    		hbox = QHBoxLayout()
    						
    		for ii in range(i):
    			ll = QLabel(str(random.randint(1,9)))		
    			hbox.addWidget(ll)
    
    		self.vbox.addLayout(hbox)
    
    app = QApplication([])
    form = Form()
    app.exec_()
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Mar 2018, 21:13 last edited by
      #2

      Hi,

      Not a direct answer but since you want to show a random sized list of numbers why not use a QListView with a QStringListModel for that ? It would likely be simpler to manage.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 25 Mar 2018, 21:45
      1
      • S SGaist
        25 Mar 2018, 21:13

        Hi,

        Not a direct answer but since you want to show a random sized list of numbers why not use a QListView with a QStringListModel for that ? It would likely be simpler to manage.

        P Offline
        P Offline
        porsol
        wrote on 25 Mar 2018, 21:45 last edited by
        #3

        @SGaist I just simplified the code with Random numbers for demonstration.

        J 1 Reply Last reply 26 Mar 2018, 05:11
        0
        • P porsol
          25 Mar 2018, 21:45

          @SGaist I just simplified the code with Random numbers for demonstration.

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 26 Mar 2018, 05:11 last edited by
          #4

          @porsol I think you need to delete/hide widgets you're removing from the layout as removing them from the layout does not delete or hide them.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Mar 2018, 09:28 last edited by
            #5

            Hi
            takeAt only give you ownership back of the widget.

            However, in most cases where you want to reuse layouts,
            its far easier to use a place holder widget, that has the layout.
            To replace it, you simply delete the placeholder widget
            and allocate a new one (with empty layout) and insert into the layout.

            1 Reply Last reply
            1
            • P Offline
              P Offline
              porsol
              wrote on 27 Mar 2018, 01:56 last edited by
              #6
              def clearvbox(self, L = False):
              	if not L:
              		L = self.vbox
              	if L is not None:
              		while L.count():
              			item = L.takeAt(0)
              			
              			widget = item.widget()
              			
              			if widget is not None:
              				widget.deleteLater()
              			else:
              				self.clearvbox(item.layout())
              
              1 Reply Last reply
              0

              5/6

              26 Mar 2018, 09:28

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved