Last active
May 17, 2017 17:14
-
-
Save altendky/72d4b2582eda96c7f6da84abbfc7e1f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import PyQt5.QtWidgets | |
class ProxyStyle(PyQt5.QtWidgets.QProxyStyle): | |
def subElementRect(self, sr, opt, widget): | |
r = super().subElementRect(sr, opt, widget) | |
if sr == PyQt5.QtWidgets.QStyle.SE_TabWidgetRightCorner: | |
twf = opt | |
paneRect = self.subElementRect(PyQt5.QtWidgets.QStyle.SE_TabWidgetTabPane, twf, widget) | |
if twf.shape in (PyQt5.QtWidgets.QTabBar.RoundedNorth, PyQt5.QtWidgets.QTabBar.TriangularNorth): | |
# r = PyQt5.QtCore.QRect(PyQt5.QtCore.QPoint(paneRect.width() - twf.rightCornerWidgetSize.width(), | |
# paneRect.y() - twf.rightCornerWidgetSize.height()), | |
# twf.rightCornerWidgetSize) | |
width = paneRect.width() - twf.tabBarSize.width() | |
width = max(width, twf.rightCornerWidgetSize.width()) | |
r = PyQt5.QtCore.QRect(PyQt5.QtCore.QPoint(paneRect.width() - width, | |
paneRect.y() - twf.rightCornerWidgetSize.height()), | |
PyQt5.QtCore.QSize(width, twf.rightCornerWidgetSize.height())) | |
elif twf.shape in (PyQt5.QtWidgets.QTabBar.RoundedSouth, PyQt5.QtWidgets.QTabBar.TriangularSouth): | |
r = PyQt5.QtCore.QRect(PyQt5.QtCore.QPoint(paneRect.width() - twf.rightCornerWidgetSize.width(), | |
paneRect.height(), | |
twf.rightCornerWidgetSize.height())) | |
r = self.visualRect(twf.direction, twf.rect, r) | |
return r | |
class ProxyStyle2(PyQt5.QtWidgets.QProxyStyle): | |
def subElementRect(self, sr, opt, widget): | |
r = super().subElementRect(sr, opt, widget) | |
if sr == PyQt5.QtWidgets.QStyle.SE_TabWidgetRightCorner and widget.property('expand_corner_widget'): | |
paneRect = self.subElementRect(PyQt5.QtWidgets.QStyle.SE_TabWidgetTabPane, opt, widget) | |
width = paneRect.width() - opt.tabBarSize.width() | |
width = max(width, opt.rightCornerWidgetSize.width()) | |
delta_width = width - r.width() | |
r.adjust(-delta_width, 0, delta_width, 0) | |
return r | |
app = PyQt5.QtWidgets.QApplication(sys.argv) | |
app.setStyle(ProxyStyle2()) | |
tw = PyQt5.QtWidgets.QTabWidget() | |
tw.setProperty('expand_corner_widget', True) | |
for name in ('red', 'green', 'blue'): | |
w = PyQt5.QtWidgets.QLabel(name) | |
tw.addTab(w, name) | |
a = PyQt5.QtWidgets.QPushButton('a') | |
b = PyQt5.QtWidgets.QPushButton('b') | |
tb = PyQt5.QtWidgets.QToolBar() | |
tb.addWidget(a) | |
tb.addWidget(b) | |
tw.setCornerWidget(tb) | |
window = PyQt5.QtWidgets.QMainWindow() | |
window.setCentralWidget(tw) | |
window.show() | |
app.exec() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment