如何将小部件在 PyQt 网格布局中的一行中居中
我正在尝试用 pyqtgrid 将一行居中,但我无法弄清楚我尝试了拉伸和对齐,但它似乎不起作用,不确定我做错了什么。我正在寻找的是左对齐窗口“Class Subs”位于行的中间,这里是 MRE
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit,QGridLayout
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
class Subs(QTextEdit):
def __init__(self):
super().__init__()
font = QFont("Times New Roman")
font.setPointSize(30)
self.setFont(font)
self.setText("Center Placeholder")
self.setFixedWidth(500)
self.setAlignment(Qt.AlignCenter)
class SecondarySub(QTextEdit):
def __init__(self, parents):
super().__init__()
self.parents = parents
self.setText("placeholder")
font = QFont("Times New Roman")
font.setPointSize(30)
self.setFont(font)
self.setAlignment(Qt.AlignCenter)
self.setFixedWidth(1000)
self.setFixedHeight(100)
class Window(QWidget):
def __init__(self):
super().__init__()
grid_lay = QGridLayout()
self.setLayout(grid_lay)
self.widget_sub = Subs()
self.widget_sub.setFixedWidth(800)
self.widget_sub.setFixedHeight(50)
print(self.widget_sub)
# widgets
grid_lay.addWidget(SecondarySub(self.widget_sub))
grid_lay.addWidget(SecondarySub(self.widget_sub))
grid_lay.addWidget(self.widget_sub)
grid_lay.setAlignment(Qt.AlignCenter)
self.setLayout(grid_lay)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
I'm tryin to center a row with pyqtgrid and I cant figure it out I tried stretches and aligns but it doesn't seem to work not sure what I'm doing wrong. What im looking for is for the left aligned window "Class Subs" to be in the middle of the row that its in here is a MRE
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit,QGridLayout
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
class Subs(QTextEdit):
def __init__(self):
super().__init__()
font = QFont("Times New Roman")
font.setPointSize(30)
self.setFont(font)
self.setText("Center Placeholder")
self.setFixedWidth(500)
self.setAlignment(Qt.AlignCenter)
class SecondarySub(QTextEdit):
def __init__(self, parents):
super().__init__()
self.parents = parents
self.setText("placeholder")
font = QFont("Times New Roman")
font.setPointSize(30)
self.setFont(font)
self.setAlignment(Qt.AlignCenter)
self.setFixedWidth(1000)
self.setFixedHeight(100)
class Window(QWidget):
def __init__(self):
super().__init__()
grid_lay = QGridLayout()
self.setLayout(grid_lay)
self.widget_sub = Subs()
self.widget_sub.setFixedWidth(800)
self.widget_sub.setFixedHeight(50)
print(self.widget_sub)
# widgets
grid_lay.addWidget(SecondarySub(self.widget_sub))
grid_lay.addWidget(SecondarySub(self.widget_sub))
grid_lay.addWidget(self.widget_sub)
grid_lay.setAlignment(Qt.AlignCenter)
self.setLayout(grid_lay)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论