QPushButton 和 QLineEdit 的高度在 OS-X 下不同
Heights of QPushButton and QLineEdit differ under OS-X
我想将 QPushButton 和 QLineEdit 放在一个 QTreeView 单元格中,方法是将它们放在具有 QHBoxLayout 的容器小部件中。然而,它看起来不太好,按钮比行编辑器高,您可以在下面的屏幕截图中看到。当单元格只包含一个按钮或编辑器时,它会填满整个单元格,这就是我想要的。
问题只发生在 OS-X(我使用的是 10.6.8),在 Windows 和 Linux 下它看起来符合预期。
我已经将布局的 contentsMargin 和间距设置为 0。使用样式表将小部件的填充和边距设置为 0 也没有帮助。我该如何解决这个问题?
我的示例是在 PyQt 中,但我还添加了 Qt 和 PySide 标签,因为我认为这不是 Python 问题。
import sys
if True:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt
else:
from PySide import QtCore, QtGui
from PySide.QtCore import Qt
def setSizePolicies(widget,
horPolicy=QtGui.QSizePolicy.MinimumExpanding,
verPolicy=QtGui.QSizePolicy.MinimumExpanding):
sizePolicy = widget.sizePolicy()
sizePolicy.setHorizontalPolicy(horPolicy)
sizePolicy.setVerticalPolicy(verPolicy)
widget.setSizePolicy(sizePolicy)
def createContainer():
container = QtGui.QFrame()
if False: # setting this to True doesn't help
container.setStyleSheet("""
QWidget {
margin: 0px;
padding: 0px;
border: 1px solid blue;
border-radius: 0px;
background-color: #CCCCCC;
}
QLineEdit { background-color: #FFFF00; }
QPushButton { background-color: #FF00FF; }
QPushButton:pressed { background-color: #AA00AA; }
""")
hLayout = QtGui.QHBoxLayout()
hLayout.setSpacing(0)
hLayout.setContentsMargins(0, 0, 0, 0)
container.setLayout(hLayout)
return container, hLayout
class MyTableView(QtGui.QTreeView):
def __init__(self):
super(MyTableView, self).__init__()
model = QtGui.QStandardItemModel(3, 2)
self.setModel(model)
self.header().resizeSection(0, 200)
self.header().resizeSection(1, 300)
self.resize(550, 400)
self.setUniformRowHeights(True)
self.setAlternatingRowColors(True)
# Create a single QLineEdit inside a container widgets that has a
# QHBoxLayout. The edior fills the entire table cell.
container0, hlayout0 = createContainer()
lineEdit0 = QtGui.QLineEdit("Fills entire cell")
setSizePolicies(lineEdit0)
hlayout0.addWidget(lineEdit0)
model.setData(model.index(0, 0), "Only a line editor")
self.setIndexWidget(model.index(0, 1), container0)
# Create a single QPushButton inside a container widgets that has a
# QHBoxLayout. The button fills the entire table cell.
container1, hlayout1 = createContainer()
button1 = QtGui.QPushButton("Fills entire cell")
setSizePolicies(button1)
hlayout1.addWidget(button1)
model.setData(model.index(1, 0), "Only a push button")
self.setIndexWidget(model.index(1, 1), container1)
# When a button and editor are both put in a cell, the button is taller.
# Also there is some overlap between them.
container2, hLayout2 = createContainer()
button2 = QtGui.QPushButton("Taller button")
setSizePolicies(button2)
hLayout2.addWidget(button2)
lineEdit2 = QtGui.QLineEdit("Smaller editor")
setSizePolicies(lineEdit2)
hLayout2.addWidget(lineEdit2)
model.setData(model.index(2, 0), "A button and an editor")
self.setIndexWidget(model.index(2, 1), container2)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
tableView = MyTableView()
tableView.show()
sys.exit(app.exec_())
我找到了适合我的解决方法。当使用 QToolButton
而不是 QPushbutton
时,按钮的高度与行编辑器相同。
我想将 QPushButton 和 QLineEdit 放在一个 QTreeView 单元格中,方法是将它们放在具有 QHBoxLayout 的容器小部件中。然而,它看起来不太好,按钮比行编辑器高,您可以在下面的屏幕截图中看到。当单元格只包含一个按钮或编辑器时,它会填满整个单元格,这就是我想要的。
问题只发生在 OS-X(我使用的是 10.6.8),在 Windows 和 Linux 下它看起来符合预期。
我已经将布局的 contentsMargin 和间距设置为 0。使用样式表将小部件的填充和边距设置为 0 也没有帮助。我该如何解决这个问题?
我的示例是在 PyQt 中,但我还添加了 Qt 和 PySide 标签,因为我认为这不是 Python 问题。
import sys
if True:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt
else:
from PySide import QtCore, QtGui
from PySide.QtCore import Qt
def setSizePolicies(widget,
horPolicy=QtGui.QSizePolicy.MinimumExpanding,
verPolicy=QtGui.QSizePolicy.MinimumExpanding):
sizePolicy = widget.sizePolicy()
sizePolicy.setHorizontalPolicy(horPolicy)
sizePolicy.setVerticalPolicy(verPolicy)
widget.setSizePolicy(sizePolicy)
def createContainer():
container = QtGui.QFrame()
if False: # setting this to True doesn't help
container.setStyleSheet("""
QWidget {
margin: 0px;
padding: 0px;
border: 1px solid blue;
border-radius: 0px;
background-color: #CCCCCC;
}
QLineEdit { background-color: #FFFF00; }
QPushButton { background-color: #FF00FF; }
QPushButton:pressed { background-color: #AA00AA; }
""")
hLayout = QtGui.QHBoxLayout()
hLayout.setSpacing(0)
hLayout.setContentsMargins(0, 0, 0, 0)
container.setLayout(hLayout)
return container, hLayout
class MyTableView(QtGui.QTreeView):
def __init__(self):
super(MyTableView, self).__init__()
model = QtGui.QStandardItemModel(3, 2)
self.setModel(model)
self.header().resizeSection(0, 200)
self.header().resizeSection(1, 300)
self.resize(550, 400)
self.setUniformRowHeights(True)
self.setAlternatingRowColors(True)
# Create a single QLineEdit inside a container widgets that has a
# QHBoxLayout. The edior fills the entire table cell.
container0, hlayout0 = createContainer()
lineEdit0 = QtGui.QLineEdit("Fills entire cell")
setSizePolicies(lineEdit0)
hlayout0.addWidget(lineEdit0)
model.setData(model.index(0, 0), "Only a line editor")
self.setIndexWidget(model.index(0, 1), container0)
# Create a single QPushButton inside a container widgets that has a
# QHBoxLayout. The button fills the entire table cell.
container1, hlayout1 = createContainer()
button1 = QtGui.QPushButton("Fills entire cell")
setSizePolicies(button1)
hlayout1.addWidget(button1)
model.setData(model.index(1, 0), "Only a push button")
self.setIndexWidget(model.index(1, 1), container1)
# When a button and editor are both put in a cell, the button is taller.
# Also there is some overlap between them.
container2, hLayout2 = createContainer()
button2 = QtGui.QPushButton("Taller button")
setSizePolicies(button2)
hLayout2.addWidget(button2)
lineEdit2 = QtGui.QLineEdit("Smaller editor")
setSizePolicies(lineEdit2)
hLayout2.addWidget(lineEdit2)
model.setData(model.index(2, 0), "A button and an editor")
self.setIndexWidget(model.index(2, 1), container2)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
tableView = MyTableView()
tableView.show()
sys.exit(app.exec_())
我找到了适合我的解决方法。当使用 QToolButton
而不是 QPushbutton
时,按钮的高度与行编辑器相同。