QPushButton和QLineEdit的高度在OS-X下有所不同

时间:2015-05-04 13:18:40

标签: qt pyqt pyside

我希望将QPushButton和QLineEdit放在一个QTreeView单元格中,方法是将它们放在具有QHBoxLayout的容器窗口小部件中。但是,它看起来并不好看,按钮比线编辑器高,正如您在下面的屏幕截图中看到的那样。当单元格只包含一个按钮或编辑器时,它会填充整个单元格,这就是我想要的。

screen shot

问题只发生在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_())

1 个答案:

答案 0 :(得分:2)

我找到了适用于我的解决方法。使用QToolButton代替QPushbutton时,按钮的高度与行编辑器相同。