Issue
I'm creating a simple dialog box for my PySide application. Within this dialog, there are going to be multiple inputs that the user will have to fill out. Associated with those inputs are labels that go alongside the left of the labels. Right now I create the label, input pair using a separate class:
class inputLayout(PyGui.QHBoxLayout):
def __init__(self, Label, parent):
super(inputLayout, self).__init__()
label = PyGui.QLabel()
label.setText(Label)
self.addWidget(label)
self.__input = PyGui.QTextEdit()
self.addWidget(self.__input)
parent.addLayout(self)
and then add it to the master layout like so:
layout = PyGui.QVBoxLayout()
self.amp = inputLayout('Amplitude', layout)
self.test = inputLayout('test', layout)
self.test2 = inputLayout('test2', layout)
The problem is that when PySide does its automagic, it get something like the following:
Like my image suggests, I'd rather have the Label take up 1/3 (or some other proportional rate of my choice) to make it look more unified. How do I size the layout using this proportionality, or ratio?
I am aware of this question, however I'm not looking to statically set the size of the label, but rather do it dynamically using a ratio.
Solution
Here's a small example that should solve your problem. The key is in two parts:
Use a
QGridLayout
, which sets the column width to the width of the widest widget in its column (unless defined otherwise). This ensures that everything is aligned nicely along the vertical axis.Set a stretch factor. This determines how an element should resize when their parent is resized. By default it's 0, so you don't have to set it, I just added it for illustration purposes. By setting the text_edit's column to 1, it will start stretching. By playing with the factors, you can make one column grow faster than the other.
from PySide import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.input_widget = InputWidget(self)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.input_widget)
self.setLayout(self.layout)
self.setCentralWidget(self.input_widget)
class InputWidget(QtGui.QWidget):
def __init__(self, parent):
super(InputWidget, self).__init__(parent)
self.grid_layout = QtGui.QGridLayout()
self.labels = ["amp", "more text", "blabla"]
self.text_edits = []
self.qlabels = []
for row, label in enumerate(self.labels):
label = QtGui.QLabel(label)
self.qlabels.append(label)
self.grid_layout.addWidget(label, row, 0)
text_edit = QtGui.QTextEdit()
self.text_edits.append(text_edit)
self.grid_layout.addWidget(text_edit, row, 1)
self.grid_layout.setColumnStretch(0, 0)
self.grid_layout.setColumnStretch(1, 2)
self.setLayout(self.grid_layout)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
If you want to access the text from the text_edit
, you could search for the label's index in self.labels
and then use that label in self.text_edits
to retrieve the corresponding text_edit
. Alternatively, once you close the dialog, you could loop through both self.labels
and self.text_edits
and create a dictionary that maps the label to the text from the text_edit
.
results = {}
for label, text_edit in zip(self.labels, self.text_edits):
results[label] = text_edit.text()
Answered By - Ivo Flipse
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.