python - PySide 自定义小部件不会自动适应 QTableWidget

标签 python layout pyqt pyside qtablewidget

我正在尝试创建一个自定义小部件RangeSpinBox。该小部件不会自动适应 QTableWidget,如下面所附的图片所示。

enter image description here

import sys
from PySide import QtGui


class RangeSpinBox(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(RangeSpinBox, self).__init__(*args, **kwargs)

        self.__minimum = 0
        self.__maximum = 100

        main_layout = QtGui.QHBoxLayout()

        self.__minimum_spin_box = QtGui.QSpinBox()
        self.__range_label = QtGui.QLabel('-')
        self.__maximum_spin_box = QtGui.QSpinBox()

        main_layout.addWidget(self.__minimum_spin_box)
        main_layout.addWidget(self.__range_label)
        main_layout.addWidget(self.__maximum_spin_box)

        self.setLayout(main_layout)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    spin_box = RangeSpinBox()
    table_widget = QtGui.QTableWidget()
    table_widget.setColumnCount(2)
    table_widget.setRowCount(1)
    table_widget.setCellWidget(0, 0, spin_box)
    table_widget.setCellWidget(0, 1, QtGui.QSpinBox())
    table_widget.show()
    app.exec_()

最佳答案

您需要删除布局上的默认边距,并更改旋转框上的大小策略以在两个方向上扩展:

        main_layout = QtGui.QHBoxLayout()
        main_layout.setContentsMargins(0, 0, 0, 0)

        size_policy = QtGui.QSizePolicy(
            QtGui.QSizePolicy.MinimumExpanding,
            QtGui.QSizePolicy.MinimumExpanding)

        self.__minimum_spin_box = QtGui.QSpinBox()
        self.__minimum_spin_box.setSizePolicy(size_policy)
        self.__range_label = QtGui.QLabel('-')
        self.__maximum_spin_box = QtGui.QSpinBox()
        self.__maximum_spin_box.setSizePolicy(size_policy)

关于python - PySide 自定义小部件不会自动适应 QTableWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38941180/

相关文章:

html - Css 流体布局

javascript - cytoscape.js 复合节点忽略布局?

python - QListWidget中字符后的屏蔽文本

flutter - 在 Flutter 中设置 ToggleButtons 之间的间隙

python - 是否可以使用最旧的依赖项来 pip 编译requirements.txt?

python - Flask-Python 空 html 输入返回值的特征

javascript - 将 python `time` 对象返回到前端?

python - 通过文本输入添加新项目时,阻止 QScrollArea 项目跳跃

python - 在 QStyledItemDelegate 中显示 QComboBox 文本而不是索引值

python - 有没有办法在 SQLite 中注册查询?