4

Standard layout tabs from left to right. All tabs are nearby. enter image description here

How to change the position of the one tab, so that it was attached to the right edge? Like This:

enter image description here

Is it possible to do?

Massimo
  • 836
  • 3
  • 17
  • 38
  • That sounds like a very confusing design. What is supposed to happen if there are more tabs than can fit in the available space? Where does the one at the end go? To me, it looks like the logs should be displayed in a different way (perhaps in a dialog). Or, even better, get rid of the tabs altogether, and use a toolbar. – ekhumoro Feb 03 '15 at 17:17

2 Answers2

8

You might want to insert an empty widget, before the last one and then disable it and set it to transparent. That achieves what you want. Attaching an example code for that.

from PyQt4 import QtGui
import sys

def main():

    app     = QtGui.QApplication(sys.argv)
    tabW    = QtGui.QTabWidget()
    tabW.setStyleSheet("QTabBar::tab:disabled {"+\
                        "width: 300px;"+\
                        "color: transparent;"+\
                        "background: transparent;}")
    pushButton1 = QtGui.QPushButton("QPushButton 1")
    pushButton2 = QtGui.QPushButton("QPushButton 2")

    tab1    = QtGui.QWidget()    
    tab2    = QtGui.QWidget()
    emptySpace = QtGui.QWidget()
    tab3    = QtGui.QWidget()

    vBoxlayout    = QtGui.QVBoxLayout()
    vBoxlayout.addWidget(pushButton1)
    vBoxlayout.addWidget(pushButton2)

    #Resize width and height
    tabW.resize(500, 500)


    #Set Layout for Third Tab Page
    tab3.setLayout(vBoxlayout)   

    tabW.addTab(tab1,"Tab 1")
    tabW.addTab(tab2,"Tab 2")
    tabW.addTab(emptySpace,"ES")
    tabW.setTabEnabled(2,False)
    tabW.addTab(tab3,"Tab 3")

    tabW.setWindowTitle('PyQt QTabWidget Add Tabs and Widgets Inside Tab')
    tabW.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
smitkpatel
  • 722
  • 6
  • 21
  • that's perfect! I can be picked up to the size of the window and change it and change the width of the hidden element. then I tab will always be against the right side box – Massimo Feb 06 '15 at 13:20
-1

Yes, you can set it using the the style sheet.

from PyQt4.QtGui import *


style = """QTabWidget::tab-bar{
    alignment: right;
}"""


class MyWidget(QTabWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.mW1 = QWidget(self)
        self.mW2 = QWidget(self)
        self.addTab(self.mW1, "w1")
        self.addTab(self.mW2, "w2")
        self.setStyleSheet(style)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    app.exec_()

Diaplay on the right

By the way, there is also a method

self.setTabPosition(QTabWidget.East)

this will display at East enter image description here

Cui Heng
  • 1,265
  • 8
  • 10
  • that's not what I want. I have to shift to the right is only one tab, not all. only one. the rest must stay left ... the picture I'm especially drawn that want to get ... – Massimo Feb 03 '15 at 11:45
  • In that case, you might inherit the class from QTabBar and overwrite the paintEvent method, for QTabWidget, you need set the customized tabBar using setTabBar – Cui Heng Feb 03 '15 at 13:03
  • thank you. I just thought that there is a ready-made way. again it is not - then I will do as you say. – Massimo Feb 03 '15 at 13:17