我正在使用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()
答案 0 :(得分:0)
您必须在第2行中建立一个Stretch,但是在此之前,您必须更正以下行的rowSpan:
grid.addWidget(self.textbox, 0, 0, 0, 3)
grid.addWidget(self.textbox2, 0, 3, 0, 3)
要了解,我们来回顾一下the docs:
无效QGridLayout :: addWidget(QWidget * widget,int fromRow,int fromColumn,int rowSpan,int columnSpan,Qt :: Alignment对齐= ...)
这是一个重载函数。
此版本将给定的小部件添加到单元格网格,跨多个 行/列。该单元格将从fromRow开始,fromColumn跨越 rowSpan行和columnSpan列。小部件将具有给定的 对齐。
如果rowSpan和/或columnSpan为-1,则小部件将分别扩展到底部和/或右侧。
也就是说,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()