I have a program written in Python, using PyGTK+Glade. I use Glade to create the Layout, and internally, I create some other elements, including a list of labels. I have a VBox of size 3, each of these 3 elements containing an EventBox which contains an HBox.
Each HBox will contain a dinamically changing set of Labels. The problem is that after Adding elemens to the HBox, it doesn't show anything or doesn't get redraw.
As I said, there are some events/functions that change the HBoxes, however, something like this even doesn't work:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("maininterface.glade")
self.window = self.builder.get_object("mainWindow")
self.fila1 = self.builder.get_object("hbox1")
self.fila2 = self.builder.get_object("hbox2")
self.fila3 = self.builder.get_object("hbox3")
self.window.show_all()
lab0 = gtk.Label("XXXXXX")
self.fila1.add(lab0) #this label is not shown
#if I uncomment the next line, it works:
#self.window.show_all()
Obviously, I am missing something, I don't know what. I could make all the adds before show_all() but that just works for initialization, the program will remove/add elements on the fly.
PD: I used pack_end() instead of add() but the result is the same.
You will need to call gtk.Widget.show() on each label that you add to the HBox. Alternatively, you can call gtk.Widget.show_all() on the HBox after adding one or more labels.
Related
I can't seem to wrap my head around how they work. The the best for placing multiple widgets seems to be QGridLayout but when I add something into a specific row/column and later decide to add somthing into another row/column everything shifts and it's just really frustrating.
For example I would not even be able to do such a simple layout as the google mainpage. When I add a searchbar to the place I want it to be and then add an image/text above it everything moves into weird spots etc and I can't find proper explanations online on how to handle it. Thus I would be delighted if anyone could explain it to an absolute beginner, like me, in an understandable way.
So when I have the following code:
from PyQt6.QtWidgets import *
import sys
import numpy as np
import os
from PyQt6 import QtCore, QtGui
from PyQt6.QtCore import QEvent, Qt
from PyQt6.QtGui import QPalette, QColor
from pathlib import Path
app = QApplication(sys.argv)
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(1024, 768)
self.setWindowTitle("Tracker")
layout = QGridLayout()
self.setLayout(layout)
#layout.setRowMinimumHeight(0, 50)
#layout.setColumnMinimumWidth(0,50)
self.input = QLineEdit(self)
self.input.setPlaceholderText('Enter Username')
layout.addWidget(self.input,1,1)
self.input.setFixedSize(300,30)
self.darkmode_check = QCheckBox('Darkmode',self)
self.darkmode_check.toggled.connect(self.darkmode)
self.darkmode_check.setChecked(True)
self.darkmode_check.move(0,0)
def darkmode(self):
if self.darkmode_check.isChecked() == True:
app.setStyleSheet(Path('D:\CODE\League Code\darkorange.qss').read_text())
else:
app.setStyleSheet(Path('D:\CODE\League Code\classic_edit.qss').read_text())
window = MainWindow()
window.show()
sys.exit(app.exec())
I get this screen which is what I want.
When I only want to add a text above this search bar:
by adding
self.text = QLabel(self)
self.text.setText('Tracker')
layout.addWidget(self.text,0,1)
I get this:
which is all over the place.
Does anyone have good explanations on GridLayout or can recommend good websites for it? I found a lot about what the grid looks like etc but nothing helped (and also some posts giving me 3x3 grids, some 4x4 etc, I'm just confused at this point)
I basically just want to place a searchbar in the middle, then a text above that and keep on adding little things here and there.
Thank you
Qt basic layouts always try to evenly divide the available space in its "cells", and each widget will have that space reserved (even if it doesn't use all of it).
Note that different widget types have also different size policies that tell the layout how it should allocate the available space and eventually set the geometry of those widgets.
For instance, QLineEdit has a Fixed vertical policy, meaning that its size hint will always be considered as the only valid height (which is similar to calling setFixedHeight() or setFixedSize() as you did).
QLabel, instead, has a Preferred size policy, meaning that if there's space left in the layout, it can take advantage of it.
When you only have the line edit, the layout only has that widget, so it will place it in the center (because you didn't specify an alignment). But when you add the label, the layout finds that the line edit needs a very small space, so it will leave the remaining to the label, hence your result.
For a simple case like this, you can just specify a proper alignment when adding the widgets: when the alignment is provided, the item will not try to occupy the whole cell and the layout will align it to the available space of that layout cell.
layout.addWidget(self.text, 0, 0, alignment=Qt.AlignBottom)
layout.addWidget(self.input, 1, 0, alignment=Qt.AlignTop)
Note that I changed the column to 0, as there is no point in placing widgets in the second column if there's nothing in the first, unless you want to get advantage of setColumnStretch() or setColumnMinimumWidth().
Also consider that for this kind of "wide" layouts with lots of empty spaces it's usually better to use nested layouts, or use container widgets.
For instance:
layout = QGridLayout(self)
centerLayout = QVBoxLayout()
layout.addLayout(centerLayout, 0, 0, alignment=Qt.AlignCenter)
# ...
centerLayout.addWidget(self.text)
centerLayout.addWidget(self.input)
Or, alternatively:
layout = QGridLayout(self)
centerWidget = QWidget()
layout.addWidget(centerWidget, 0, 0, alignment=Qt.AlignCenter)
centerLayout = QVBoxLayout(centerWidget)
# ... as above
Try to remove the alignment argument in the two examples above and you'll see the difference.
I suggest you to do some experiments using layouts in Qt Designer, which makes it easier to immediately understand how layout work and behave with different widget types.
I am searching for a way to have a QTreeView that contains hierarchical items which themselfs have a layout that is propperly drawn.
I tried to inherit from both QStandartItem and QWidget (to have a layout) but the second i set the layout on the widget part of this class the programm is shutting down when it tries to render.
class modPackItem(qtg.QStandardItem,qtw.QWidget):
def __init__(self,txt:str='',image_path:str='./assets/defaultModPack.jpg'):
super().__init__()
fnt = qtg.QFont('Calibri',12)
fnt.setBold(True)
self.setEditable(False)
self.setForeground(qtg.QColor(0,0,0))
self.setFont(fnt)
self.setText(txt)
self.horLayout = qtw.QHBoxLayout()
self.horLayout.addWidget(qtw.QLabel("test"))
#self.setLayout(self.horLayout) #this breaks the rendering
modPack_image = qtg.QImage(image_path)
self.setData(modPack_image.scaled(64,64,qtc.Qt.AspectRatioMode.KeepAspectRatioByExpanding),qtc.Qt.ItemDataRole.DecorationRole)
Is there a possible way to have all items in the QTreeView contain layouts (For example with multiple texts[description,tag-words,etc]).
Note: I also considered switching to a simple List of widgets which have children containing the hierarchical items. But that would increase complexity of my app-structure a lot and therefore i would like to avoid that.
Edit: To clearify what i want to do:
I want to build a mod(pack) manager in the style of the technic-launcher for minecraft mods but instead for any kind of game in any kind of infrastructure(steam, local instal,etc). By clicking different buttons i add new "modpacks" or "mods" (optimally custom QStandartItem with Layout for all the data) in an hierarchical fashion (therefore treeview). Adding the items and the steam-subsrciption or filecopy logic is no problem but i would like to see all infos (Name,descritpion, custom tags) on the overview (like in the example pic). I know i could bind the QStandartItem selection method to a new popup showing all infos but that would be inconvinient.
Edit2: On terms of implementation i just add the QStandartItem-object as an additional row to the root-node before setting the model. I allready tested adding new objects to the rootnode by clicking on a button and that worked fine. Just setting the layout in the object crashes the application at start.
class SteamModManager_Dialog(qtw.QDialog):
window: Ui_SteamModManagerFrame
treeModel: qtg.QStandardItemModel
rootNode: qtg.QStandardItem
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.window = Ui_SteamModManagerFrame()
self.window.setupUi(self)
self.window.label_footer.setText("")
self.treeModel = qtg.QStandardItemModel()
self.rootNode = self.treeModel.invisibleRootItem()
modPack = modPackItem('Dont Starve Together')
testMod = modItem("TestMod")
modPack.appendRow(testMod)
self.rootNode.appendRow(modPack)
self.window.tView_modPacks.setModel(self.treeModel)
self.window.tView_modPacks.expandAll()
On the behalf of #musicamente here the solution that worked out for me:
I created a widget in the designer (as usual, not posting the full ui code here).
Then i implemented the following code into the Dialog:
self.treeModel = qtg.QStandardItemModel()
self.rootNode = self.treeModel.invisibleRootItem()
modPack = modPackItem('Dont Starve Together')
testMod = modItem("TestMod")
modPack.appendRow(testMod)
self.rootNode.appendRow(modPack)
self.window.tView_modPacks.setModel(self.treeModel)
self.window.tView_modPacks.expandAll()
modPackWidget = qtw.QWidget()
ui = Ui_modPackWidget()
ui.setupUi(modPackWidget)
self.window.tView_modPacks.setIndexWidget(self.treeModel.index(0,0),modPackWidget)
This code resulted setting the custom widget to the treeview item. Here the final look:
I ran into an issue tonight that involves resizing a QTextBox in a QGridLayout. The code is mostly self-explanatory. I set the QTextBox to span 10 cells down in my init_ui. I have a window size of 500x500.
def init_ui(self):
self.message_body = QTextEdit()
self.layout.addWidget(self.message_body, 2, 0, 9, 7)
self.show()
And then in a later method, changeSize, I am successfully able to change the size of QTextEdit. From this method I call the next method to load titles that will appear below the 445x280 QTextEdit. Loading these titles calls for new widgets to be added to the layout. Which is apparently a problem. I don't know if this is poor styling or not. Should all of the widgets be added in init? Either way when
self.layout.addWidget(lbl_title, 10, i)
runs. I lose my resized QTextEdit. Even if I follow it with another resize function.
Here are the two methods implicated
def changeSize(self):
self.message_body.resize(445,280)
self.loadTitles()
def loadTitles(self):
titles = Movie.title_library
for i, my_title in enumerate(titles):
lbl_title = QLabel(my_title)
## This is the line that refreshes the size to span 10x8 units
self.layout.addWidget(lbl_title, 10, i)
self.message_body.resize(445,280)
I figured out the solution. I needed to remove the message body widget before reinstating it. The code looked like
self.layout.removeWidget(self.message_body)
self.layout.addWidget(self.message_body,2,0,4,7)
It's my first time being here and I've been struggling with python coding dealing with figuring it out how to update stuff per action or mouse event.
Lately, whenever I tried to test out my script, I often see some of the buttons and layout panels in the Attribute Editor, when it's suppose to be in the window I've created. how can I make that stop?
I don't think I could post the code in here since it's about 1000 code long, but how can I find a way to prevent something like that? Is it because I used too much setParent('..') function?
If your buttons etc are appearing in the wrong layout, it is probably because you're calling UI commands after some other function has reset the existing parent.
If you want to be sure your controls are going to the right place you'll need to store the names of any windows/layouts/panels you've created and explicitly set them to be the parent before you start making widgets. Otherwise the parenting is basically 'whatever got created last'. You can verify that by something like this:
# make a button out of context
import maya.cmds as cmds
xxx = cmds.button('boo')
# ask the parent of what we just made....
print cmds.control(xxx, q=True, p=True)
## Result: u'MayaWindow|MainAttributeEditorLayout|formLayout2|AEmenuBarLayout|AErootLayout|AEselectAndCloseButtonLayout' #
Parentage will be switched if you create a top level container (a window or panel):
w = cmds.window()
c = cmds.columnLayout()
b = cmds.button("bar")
# ask b's parent....
print cmds.control(b, q=True, p=True)
## Result: window3|columnLayout49 #
You can also switch parents explicitly:
def make_a_layout(window_name):
w = cmds.window(window_name)
c = cmds.columnLayout()
return c
layout_a = make_a_layout('window_a')
# any future widgets go into this layout...
print cmds.button("layout 1 a")
# window_a|columnLayout55|layout_1_a
layout_b = make_a_layout('window_b')
# now this is the active layout
print cmds.button("layout 2 a ")
# window_b|columnLayout56|layout_2_a
# explicitly set the parent to the first layout
# now new widgets will be there
cmds.setParent(layout_a)
print cmds.button("layout 1 b")
# window_a|columnLayout56|layout_1_b
As you can see, current parent is set every time a new layout is created. You can pop up a level with setParent ('..') or set it to any layout explicitly with setParent('your_layout_here').
I'm trying to remove a Qt widget from a layout in a PySide application.
Here is a minimal example. It is a widget with 5 buttons in it, and the middle one is supposed to remove itself when clicked:
import sys
from PySide import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
layout = QtGui.QVBoxLayout()
buttons = [QtGui.QPushButton(str(x)) for x in xrange(5)]
def deleteButton():
b = layout.takeAt(2)
buttons.pop(2)
del b
buttons[2].clicked.connect(deleteButton)
map(layout.addWidget, buttons)
widget.setLayout(layout)
widget.show()
app.exec_()
What actually happens is this:
The button is unclickable and clearly isn't taken into consideration for the layout computations, but its image stays in place.
According to the Qt documentation, the correct way of deleting all objects from a layout is:
while ((child = layout->takeAt(0)) != 0) {
delete child;
}
Here I just want to delete the third button, so I just call takeAt(2), and then del b to call the destructor on that item. The button object is also .pop'd from the buttons list to make sure there is no leftover reference to the object. How does my code differ from the one in the Qt docs that would cause such a behavior?
Super simple fix:
def deleteButton():
b = layout.takeAt(2)
buttons.pop(2)
b.widget().deleteLater()
You first have to make sure you are addressing the actual button and not the QWidgetItem that is returned from the layout, and then call deleteLater() which will tell Qt to destroy the widget after this slot ends and control returns to the event loop.
Another example illustrates why the problem is occurring. Even though you take the layout item, the underlying widget is still parented to the original layouts widget.
def deleteButton():
b = layout.takeAt(2)
buttons.pop(2)
w = b.widget()
w.setParent(None)
This is not the preferred way, as it still leaves the cleanup of the object ambiguous. But it shows that clearing the parent allows it to leave the visual display. Use deleteLater() though. It properly cleans everything up.
The answer that 'jdi' provided is valid, although If anyone is interested, I tried implementing what is suggested in the Qt Documentation with the loop of every child Widget, and I got the following code working in Python PySide6:
def delete():
while ((child := layout.takeAt(0)) != None):
child.widget().deleteLater()