gpt4 book ai didi

python - 在QMainWindow中放置一个小部件,如何定位而不是setCentralWidget

转载 作者:行者123 更新时间:2023-12-01 08:52:19 25 4
gpt4 key购买 nike

我正在使用 QGridLayout,并通过使用 setCentralWidget 函数,行和列 (0,0) 从窗口的中心开始,从而留下大量空白空间。

如何让它居中,但从窗口顶部开始而不是从中间开始?

我对 Qt 还很陌生,想知道我的处理方式是否错误?我应该为 QWidget 创建一个新类吗?

class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Data visualizing'
self.left = 50
self.top = 50
self.width = 300
self.height = 100
self.initUI()

def initUI(self):
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)

# Create textbox
self.textbox = QLineEdit(self)
self.textbox.setReadOnly(True)

# Create textbox 2
self.textbox2 = QLineEdit(self)
self.textbox2.setReadOnly(True)

# Create button
self.button = QPushButton('Load file 1', self)
self.button.setToolTip('Click here to browse for the first data file')
self.button.clicked.connect(self.on_click)

# Create button 2
self.button2 = QPushButton('Load file 2', self)
self.button2.setToolTip('Click here to browse for the first data file')
self.button2.clicked.connect(self.on_click)

grid = QGridLayout()

grid.addWidget(self.textbox, 0, 0, 0, 3)
grid.addWidget(self.textbox2, 0, 3, 0, 3)
grid.addWidget(self.button, 1, 1, 1, 1)
grid.addWidget(self.button2, 1, 4, 1, 1)

self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.centralWidget().setLayout(grid)
self.show()

def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
"All Files (*);;Comma seperated files (*.csv)", options=options)
if fileName:
self.textbox.setText(fileName)
print(fileName)

@pyqtSlot()
def on_click(self):
self.openFileNameDialog()
print('PyQt5 button click')

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

最佳答案

您必须在第 2 行中建立一个 Stretch,但在此之前您必须更正以下位置的 rowSpan:

grid.addWidget(self.textbox, 0, 0, 0, 3)
grid.addWidget(self.textbox2, 0, 3, 0, 3)

为了理解,让我们回顾一下 the docs :

void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = ...)

This is an overloaded function.

This version adds the given widget to the cell grid, spanning multiple rows/columns. The cell will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns. The widget will have the given alignment.

If rowSpan and/or columnSpan is -1, then the widget will extend to the bottom and/or right edge, respectively.

也就是说,rowSpan 指示小部件将占用多少行,但您将其指示为 0,因此大小将不再由布局处理,而仅由位置处理,您必须将其更改为 1。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Data visualizing'
self.left, self.top, self.width, self.height = 50, 50, 300, 100
self.initUI()

def initUI(self):
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)

# Create textboxs
self.textbox = QtWidgets.QLineEdit(readOnly=True)
self.textbox2 = QtWidgets.QLineEdit(readOnly=True)

# Create buttons
self.button = QtWidgets.QPushButton('Load file 1',
toolTip = 'Click here to browse for the first data file')
self.button.clicked.connect(self.on_click)
self.button2 = QtWidgets.QPushButton('Load file 2',
toolTip = 'Click here to browse for the first data file')
self.button2.clicked.connect(self.on_click)

grid = QtWidgets.QGridLayout(self.centralWidget())

grid.addWidget(self.textbox, 0, 0, 1, 3)
grid.addWidget(self.textbox2, 0, 3, 1, 3)
grid.addWidget(self.button, 1, 1, 1, 1)
grid.addWidget(self.button2, 1, 4, 1, 1)
grid.setRowStretch(2, 1)

self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.show()

def openFileNameDialog(self):
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
"All Files (*);;Comma seperated files (*.csv)", options=options)
if fileName:
self.textbox.setText(fileName)
print(fileName)

@QtCore.pyqtSlot()
def on_click(self):
self.openFileNameDialog()
print('PyQt5 button click')

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

关于python - 在QMainWindow中放置一个小部件,如何定位而不是setCentralWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com