Qt 在OS-X下,QPushButton和QLineEdit的高度不同
我想将QPushButton和QLineEdit放在一个QTreeView单元格中,方法是将它们放在一个具有QHBoxLayout的容器小部件中。但是,它看起来不太好,按钮比线条编辑器高,你可以在下面我的屏幕截图中看到。当单元格只包含一个按钮或编辑器时,它将填充整个单元格,这正是我想要的 这个问题只发生在OS-X上(我使用的是10.6.8),在Windows和Linux下,它看起来和预期的一样 我已经将版面的内容边距和间距设置为0。使用样式表将小部件的填充和边距设置为0也没有帮助。我怎样才能解决这个问题 我的例子是PyQt,但我也添加了Qt和PySide标记,因为我认为这不是Python的问题Qt 在OS-X下,QPushButton和QLineEdit的高度不同,qt,pyqt,pyside,Qt,Pyqt,Pyside,我想将QPushButton和QLineEdit放在一个QTreeView单元格中,方法是将它们放在一个具有QHBoxLayout的容器小部件中。但是,它看起来不太好,按钮比线条编辑器高,你可以在下面我的屏幕截图中看到。当单元格只包含一个按钮或编辑器时,它将填充整个单元格,这正是我想要的 这个问题只发生在OS-X上(我使用的是10.6.8),在Windows和Linux下,它看起来和预期的一样 我已经将版面的内容边距和间距设置为0。使用样式表将小部件的填充和边距设置为0也没有帮助。我怎样才能解
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
时,按钮的高度与行编辑器的高度相同。这是一个Qt错误,不幸的是,我没有发现解决方法。布局中的按钮因某种原因而损坏。