Reputation: 4038
I'm trying to make a class that will generate all my widgets. Currently, I'm stuck because I want to make my line edit line up with the button (typical of what you see in file upload windows) but the button is in the next line. How do I line them up?
Class that generates the form widgets:
class CreateFormElements(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(CreateFormElements, self).__init__(*args, **kwargs)
def create_form_row(self, label, fieldType, *args, **kwargs):
layout = QFormLayout()
# this arguments will tell me if I want a button, and if the button should open a file window
connectType = kwargs.get('connectType', None)
_fieldType = kwargs.get('_fieldType', None)
# if this is a button that should open up a file window, call the create_file_window() function to make a
# file window. currently no button is visible and the file window is opening up directly.
if connectType == 'file' and _fieldType == 'button':
filePath = layout.addWidget(QLineEdit())
fieldType.setText("Browse")
fieldType.clicked.connect(create_file_window)
layout.addRow(QLabel(label), fieldType)
self.setLayout(layout)
Main window:
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.myWindow()
def myWindow(self):
self.setWindowTitle("My Awesome App")
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(-1)
print(" Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))
self.width = 600
self.height = 480
self.left = 0
self.top = 0
self.setGeometry(self.left, self.top, self.width, self.height)
layout = QVBoxLayout()
elm = CreateFormElements()
elm.create_form_row("Object Name: ", QPushButton(), connectType='file', _fieldType='button')
layout.addWidget(elm)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
Thanks in advance.
Upvotes: 3
Views: 951
Reputation: 24420
You could put the QLineEdit
and QButton
in a QHBoxLayout
, and add that to your form layout, e.g.
lineEdit = QLineEdit()
fieldType.setText("Browse")
hlayout = QHBoxLayout()
hlayout.addWidget(lineEdit)
hlayout.addWidget(fieldType)
layout.addRow(label, hlayout)
Upvotes: 4