好吧,我有一个文本编辑器,上面有一个按钮...我希望文本编辑器位于布局内部,以便它增加和减小QDialog
的大小,但我不想要使用按钮来做同样的事情,顺便说一下,按钮没有我想要的尺寸,也无法改变它。我该怎么办?
代码:
from PyQt4 import QtCore, QtGui
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TextEditorDlg(QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(500, 400)
self.Grid = QtGui.QGridLayout(self)
self.button = QtGui.QPushButton(self)
self.Grid.addWidget(self.button, 0, 0)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.Grid.addWidget(self.lineEdit, 1, 0)
self.Grid.addWidget(self.textEdit, 2, 0)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
现在查看问题了吗?
答案 0 :(得分:5)
首先,我不明白您使用QGridLayout
的原因。你是否意识到你根本没有使用网格?您应该只使用QVBoxLayout
。
无论如何,正如我在评论中所提到的,你可以获得你想要的"输出"使用QHBoxLayout
和addStretch
方法:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.button_layout = QtGui.QHBoxLayout()
self.button_layout.addStretch()
self.button_layout.addWidget(self.button)
self.button_layout.addStretch()
self.grid = QtGui.QGridLayout(self)
self.grid.addLayout(self.button_layout, 0, 0)
self.grid.addWidget(self.lineEdit, 1, 0)
self.grid.addWidget(self.textEdit, 2, 0)
# Alternative using QVBoxLayout:
#self.layout = QtGui.QVBoxLayout(self)
#self.layout.addLayout(self.button_layout)
#self.layout.addWidget(self.line_edit)
#self.layout.addWidget(self.text_edit)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
您可以将通过整数的拉伸量调整为addStretch
。
如果您想使用QGridLayout
将按钮放在特定位置,那么您应该这样做:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.grid = QtGui.QGridLayout(self)
# Use the row-span and column-span arguments!
self.grid.addWidget(self.button, 0, 0, 1, 1)
self.grid.addWidget(self.lineEdit, 1, 0, 1, 3)
self.grid.addWidget(self.textEdit, 2, 0, 1, 3)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
您可以使用正确的行和列索引以及行间距和列跨度值将按钮准确放置在所需位置。
修改:如果您想更改QPushButton
的尺寸,则必须更改按钮的QSizePolicy
和 QTextEdit
。例如:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.button.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.textEdit.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Minimum)
self.grid = QtGui.QGridLayout(self)
# Use the row-span and column-span arguments!
self.grid.addWidget(self.button, 0, 0, 3, 1)
self.grid.addWidget(self.lineEdit, 3, 0, 1, 3)
self.grid.addWidget(self.textEdit, 4, 0, 1, 3)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
您会看到按钮更大。您可以通过更改行跨度来控制有多大。
在self.grid.addWidget(self.button, 0, 0, 3, 1)
行增加/减少3
将增加/减少垂直大小(实际上是小部件占用的行数)和增加/减少1
将改变水平大小
请注意,如果您将小部件放置在0
行的3
行,那么您必须小心不要创建冲突,为布局添加新的小部件。