Massimo
Massimo

Reputation: 966

QTabWidget. How to move one tab to right position?

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?

Upvotes: 4

Views: 6600

Answers (2)

smitkpatel
smitkpatel

Reputation: 730

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()

Upvotes: 8

Cui Heng
Cui Heng

Reputation: 1285

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

Upvotes: -1

Related Questions