Reputation: 1129
I have all my widgets placed in center (except the one on the left side, 'cauz i want it to be there only), both in a grid and horizontal layout. Let me show you :
I Want them to be placed like this in the image below, and not all the widgets in the center like the image above :
As you can see, the top one's are in corners, and the one rectangle in the middle is at the center. I want them to be placed wherever i want, whether in the between or in the corner, but at the same time, not losing my grids and spacing added as when you stretch this horizontally/vertically it will add up more/less space between the widgets. As you can see yourself by stretching the app.
Here's my code :
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtGui import QCursor
from PySide2.QtWidgets import QFrame
from PySide2.QtCore import QRect
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(250, 80, 800, 600)
self.setWindowTitle("test app")
self.setStyleSheet("background-color: #ffffff; border")
label = QtWidgets.QLabel(self)
label.setFixedWidth(260)
label.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.994, y1:0.527, x2:0.999682, y2:0.005, stop:0 rgba(246, 246, 246, 255), stop:1 rgba(255, 248, 248, 255));")
lineedit = QtWidgets.QLineEdit()
lineedit.setFixedWidth(310)
lineedit.setFixedHeight(30)
lineedit.setStyleSheet("""
background-color: #ffffff;
border-width: 1px;
border-color: rgb(200, 200, 200);
border-style: solid;
border-radius: 4;
padding: 6px 12px;
color: rgb(50,50,50);
font-family: SourceSansPro-Regular;
font-size: 12px;
""")
f = lineedit.font()
f.setLetterSpacing(QtGui.QFont.PercentageSpacing, 100)
lineedit.setFont(f)
button = QtWidgets.QPushButton("New Product")
button.setFixedSize(110, 28)
button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
button1 = QtWidgets.QPushButton("Delete Product")
button1.setFixedSize(110, 28)
button1.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button1.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
button2 = QtWidgets.QPushButton("Next product")
button2.setFixedSize(110, 28)
button2.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button2.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
widget = QtWidgets.QLabel()
widget.setFixedSize(180, 180)
widget.setStyleSheet("""background-color: rgb(200, 255, 250)""")
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
right_container = QtWidgets.QWidget()
glay = QtWidgets.QGridLayout(right_container)
glay.addWidget(lineedit, 0, 0)
glay.addWidget(widget, 1, 0)
glay.addWidget(button, 2, 0)
glay.addWidget(button1, 2, 1)
glay.addWidget(button2, 0, 1)
hlay = QtWidgets.QHBoxLayout(central_widget)
hlay.setContentsMargins(0, 0, 0, 0)
hlay.addWidget(label)
hlay.addWidget(right_container)
def main():
app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I tried many things, but ended up with errors. I just ran out of logic, about how to achieve this.
Thanks in advance...
Edit 1: I tried both of these things that was suggested in comments :
1) Adding glay.addWidget(lineedit, 0, 0, alignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
. It's producing the same result even after applying the changes.
2) Using a QVBoxLayout isn't helpful though. There is something that is not allowing me to add the widgets at the rectangle i draw below. Because i can't add widgets at the rectangle side. (See the image below...)
Upvotes: 0
Views: 83
Reputation: 13691
void QGridLayout::setColumnStretch(int column, int stretch)
Sets the stretch factor of column column to stretch. The first column is number 0.
void QGridLayout::setRowStretch(int row, int stretch)
Sets the stretch factor of row row to stretch. The first row is number 0.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QFrame
from PyQt5.QtCore import QRect
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(250, 80, 800, 600)
self.setWindowTitle("test app")
self.setStyleSheet("background-color: #ffffff; border")
label = QtWidgets.QLabel(self)
label.setFixedWidth(260)
label.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.994, y1:0.527, x2:0.999682, y2:0.005, stop:0 rgba(246, 246, 246, 255), stop:1 rgba(255, 248, 248, 255));")
lineedit = QtWidgets.QLineEdit()
lineedit.setFixedWidth(310)
lineedit.setFixedHeight(30)
lineedit.setStyleSheet("""
background-color: #ffffff;
border-width: 1px;
border-color: rgb(200, 200, 200);
border-style: solid;
border-radius: 4;
padding: 6px 12px;
color: rgb(50,50,50);
font-family: SourceSansPro-Regular;
font-size: 12px;
""")
f = lineedit.font()
f.setLetterSpacing(QtGui.QFont.PercentageSpacing, 100)
lineedit.setFont(f)
button = QtWidgets.QPushButton("New Product")
button.setFixedSize(110, 28)
button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
button1 = QtWidgets.QPushButton("Delete Product")
button1.setFixedSize(110, 28)
button1.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button1.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
button2 = QtWidgets.QPushButton("Next product")
button2.setFixedSize(110, 28)
button2.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
button2.setStyleSheet("""
QPushButton {
background-color: #474767;
border: none;
color: white;
text-align: center;
font-size: 14px;
padding: 7px 10px;
border-radius: 3;
font-family: Source Sans Pro SemiBold;
}
QPushButton:hover{
background-color: #474757;
}
""")
widget = QtWidgets.QLabel()
# widget.setFixedSize(180, 180)
widget.setMinimumHeight(180) # +++
widget.setStyleSheet("""background-color: rgb(200, 255, 250)""")
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
right_container = QtWidgets.QWidget()
# ++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
glay = QtWidgets.QGridLayout(right_container)
glay.addWidget(lineedit, 0, 0)
glay.addWidget(button2, 0, 2)
glay.addWidget(widget, 2, 0, 1, 3) # 2
glay.addWidget(button, 4, 0) # 4
glay.addWidget(button1, 4, 2)
glay.setColumnStretch(1, 1) # setColumnStretch
glay.setRowStretch(1, 1) # setRowStretch
glay.setRowStretch(2, 2) # setRowStretch
glay.setRowStretch(3, 1) # setRowStretch
# ++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
hlay = QtWidgets.QHBoxLayout(central_widget)
hlay.setContentsMargins(0, 0, 0, 0)
hlay.addWidget(label)
hlay.addWidget(right_container)
def main():
app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Upvotes: 1