Python Forum
[PyQt] remove widgets of a layout
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] remove widgets of a layout
#1
i have some problem deleting widgets from the layout:
I used the removeItem() function but the widgets are still there:
def clearlayout(layout):
    for i in reversed(range(layout.count())):
        print(layout.itemAt(i))
        layout.itemAt(i).setParent(None)
        layout.removeItem(layout.itemAt(i))
        layout.itemAt(i).show()
the files are here:
https://1drv.ms/u/s!Ag-Rb-LqQZYuiB0SfkDA...s?e=1zKCKZ
(GUI.py is the executable file)

when you are running the 'GUI.py' you need to delete or change the cmd code part:
#Convert ui to py with PowerShell and reload##########################################
command1='cd \\\\filestore.soton.ac.uk\\users\\jy1u18\\mydesktop\\QT'
command2='pyuic5 try.ui -o first.py'
powershellcmd=subprocess.Popen(['powershell.exe',command1],stdout=sys.stdout)
powershellcmd.communicate()
powershellcmd=subprocess.Popen(['powershell.exe',command2],stdout=sys.stdout)
powershellcmd.communicate()
importlib.reload(first)
######################################################################################
the code is not complete yet.
I want to change the labels and lineedits shown in the layout when choosing different circuits from the menubar 'circuits'
the code is not complete yet. only 'C' and 'R' are available. 'C' only run the clearlayout function 'R' runs the function and add new widgets onto it.
Reply
#2
I think messing with layouts to change a view is a bad idea. If I want to change how parts of a window looks based on some selection or mode I would use a QStackedWidget.

I have a question about your code:
def clearlayout(layout):
    for i in reversed(range(layout.count())):
        print(layout.itemAt(i))
        layout.itemAt(i).setParent(None)  <- Why 1
        layout.removeItem(layout.itemAt(i))
        layout.itemAt(i).show()   <- Why 2
Why 1:
Why are you setting the parent to None? A widget with no parent becomes a top level window. When you run this don't you get a bunch of little windows popping up on the desktop?

Why 2: You just removed layoutItem[i]. It is gone.
Reply
#3
Sorry that was copied when I was trying another approach to delete them, the function in the file is :
def clearlayout(layout):
    for i in reversed(range(layout.count())):
        print(layout.itemAt(i))
        layout.removeItem(layout.itemAt(i))
I'm new to pyqt, thank you for you suggestion I think I need to learn that now lol
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Grid layout and QHBox hobbyist 1 449 Jan-26-2021, 12:35 AM
Last Post: deanhystad
  pyqt5 layout Nickd12 8 819 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  Delete Qt Layout manager deanhystad 2 491 Jan-10-2021, 08:33 PM
Last Post: deanhystad
  [Tkinter] Problems with frames layout Gupi 1 1,058 May-27-2019, 09:41 AM
Last Post: Yoriz
  Python GUI layout off between different OS shift838 5 1,604 Jan-02-2019, 02:53 AM
Last Post: shift838
  clear all widgets at same time (not delete/remove) shift838 0 1,143 Dec-17-2018, 11:55 PM
Last Post: shift838
  [PyQt] Layout unwanted space WatcherMagic 2 1,413 Dec-12-2018, 09:37 PM
Last Post: WatcherMagic
  [Tkinter] grid layout neech 8 12,909 Oct-14-2016, 07:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020