Python 角落QTabar上的小部件不在角落中
我有一个按钮,可以在QTabWidget中添加选项卡。但是当我改变按钮的大小时,它就不在角落里了。那么我怎样才能像第一张照片那样把它带到角落里呢 不更改大小: 更改大小: 这是我的密码:Python 角落QTabar上的小部件不在角落中,python,qt4,pyqt4,qtabwidget,Python,Qt4,Pyqt4,Qtabwidget,我有一个按钮,可以在QTabWidget中添加选项卡。但是当我改变按钮的大小时,它就不在角落里了。那么我怎样才能像第一张照片那样把它带到角落里呢 不更改大小: 更改大小: 这是我的密码: from PyQt4 import QtGui, QtCore class PlaylistTable(QtGui.QWidget): def __init__(self): super(PlaylistTable, self).__init__() self.p
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文档: 小部件的几何图形已确定 基于小部件的sizeHint()和style() 如果打印QPushButton的
sizeHint()
,您将看到宽度从未低于某个值
我发现另一种选择是使用QToolButton
,它可以完成QPushButton所做的一切(甚至更多)
self.plusButton = QtGui.QToolButton(self)
self.plusButton.setText("+")