python - 角上的小部件 QTabBar 不在角上

标签 python qt4 pyqt4 qtabwidget

我有一个按钮可以在 QTabWidget 中添加选项卡。但是当我改变按钮的大小时,它就不再在角落里了。那么我怎样才能像第一张图片那样把它带到角落呢?

不改变大小:

/image/10NJy.png

更改尺寸:

/image/cA4Zp.png

这是我的代码:

from PyQt4 import QtGui, QtCore


class PlaylistTable(QtGui.QWidget):
    def __init__(self):
        super(PlaylistTable, self).__init__()
        self.playlistTable = QtGui.QTableWidget(self)
        self.playlistTable.setFrameShape(QtGui.QFrame.NoFrame)
        self.playlistTable.setFrameShadow(QtGui.QFrame.Sunken)
        self.playlistTable.setLineWidth(0)
        self.playlistTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.playlistTable.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
        self.playlistTable.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
        self.playlistTable.setShowGrid(True)
        self.playlistTable.setGridStyle(QtCore.Qt.SolidLine)
        self.playlistTable.setWordWrap(True)
        self.playlistTable.setCornerButtonEnabled(True)
        self.playlistTable.verticalHeader().setVisible(False)


class CustomTabWidget(QtGui.QTabWidget):
    """Tab Widget that that can have new tabs easily added to it."""

    def __init__(self, parent=None):
        super(CustomTabWidget, self).__init__(parent)
        # QtGui.QTabWidget.__init__(self, parent)

        # Tab Bar
        self.tab = QtGui.QTabBar()
        self.setTabBar(self.tab)

        # Properties
        self.setMovable(True)
        self.setTabsClosable(True)

        self.plusButton = QtGui.QPushButton("+")
        self.plusButton.setFixedSize(QtCore.QSize(22, 22))
        self.setCornerWidget(self.plusButton)

        # Signals
        self.connect(self.plusButton, QtCore.SIGNAL('clicked()'), self.addTab)
        # self.tab.plusClicked.connect(self.addTab)
        self.tab.tabMoved.connect(self.tab.moveTab)
        self.tabCloseRequested.connect(self.removeTab)

    def addTab(self):
        string = QtCore.QString.fromUtf8("Playlist")
        tab = PlaylistTable()
        super(CustomTabWidget, self).addTab(tab, string)


class AppDemo(QtGui.QMainWindow):
    def __init__(self):
        super(AppDemo, self).__init__()
        self.centralwidget = QtGui.QWidget(self)
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setContentsMargins(0, -1, 0, -1)

        self.playlist_manager = CustomTabWidget(self.centralwidget)

        self.horizontalLayout.addWidget(self.playlist_manager)

        self.playlist_manager.addTab()
        self.setCentralWidget(self.centralwidget)

        self.show()
# end class AppDemo


def main():
    import sys
    app = QtGui.QApplication(sys.argv)

    w = AppDemo()
    w.setWindowTitle('AppDemo')
    w.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

最佳答案

我猜原因是这样的,来自 QTabWidget 文档:

The geometry of the widget is determined based on the widget's sizeHint() and the style().

如果您打印 QPushButton 的 sizeHint(),您将看到 width 永远不会低于某个值。

我发现一种替代方法是使用 QToolButton,它可以完成 QPushButton 可以完成的所有操作(甚至更多)。

self.plusButton = QtGui.QToolButton(self)
self.plusButton.setText("+")

关于python - 角上的小部件 QTabBar 不在角上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694561/

相关文章:

python - 使用 Qt4 后端的 matplotlib 存在缓存渲染器问题

python - 编辑曲线和轴参数选项未在 matplotlib 中显示

python - struct pack 打印字符串而不是二进制值

python - 根据索引列表从列表中删除项目

c++ - 关于 Qt4 setValidator 用法的莫名其妙的 gcc 投诉

python - QT QFileSystemWatcher

python - 在单独的线程中运行 pyQT GUI 主应用程序

python - 如果大小写与现有项目不同,QComboBox 会替换编辑文本

python - Snakemake 使用脚本进行 shell I/O 重定向和访问 Snakemake 变量

python - 使用 Python 在 3 空间中绘制最大边距超平面