开发者

Strange thing about tabbar.. Help me to solve

开发者 https://www.devze.com 2022-12-14 13:29 出处:网络
I just started learning pyqt.. When i\'m experimenting with the tabbar i encountered this.. As a minimal example i want to show a button in tab1 and a label in tab2.. Here\'s what i did

I just started learning pyqt.. When i'm experimenting with the tabbar i encountered this.. As a minimal example i want to show a button in tab1 and a label in tab2.. Here's what i did

from PyQt4 import QtGui    
class Ui_TabWidget(QtGui.QTabWidget):        
    def __init__(self,parent=None):
        QtGui.QTabWidget.__init__(self,parent)

        self.setObjectName("TabWidget")
        self.resize(400, 300)
        self.setWindowTitle(QtGui.QApplication.translate("TabWidget", "TabWidget", None,开发者_StackOverflow中文版 QtGui.QApplication.UnicodeUTF8))

        #Creating the tabbar
        self.tabBar=QtGui.QTabBar(self)

        #Adding the first tab
        self.tabBar.addTab("tab1")
        self.tabBar.setTabText(0,"TAB1")

        #The widget intended for tab1
        self.widgetforTab1=QtGui.QWidget()
        self.addTab(self.widgetforTab1,"")
        self.buttonForTab1=QtGui.QPushButton(self.widgetforTab1)
        self.buttonForTab1.setText("Button in Tab1")

        #Adding the second Tab
        self.tabBar.addTab("tab2")
        self.tabBar.setTabText(1,"TAB2")

        #The widget intended for tab2
        self.widgetForTab2=QtGui.QWidget()
        self.addTab(self.widgetForTab2,"")
        self.labelForTab2=QtGui.QLabel(self.widgetForTab2)
        self.labelForTab2.setText("Label in Tab2")

        #Adding the tabbar to the tabwidget
        self.setTabBar(self.tabBar)

        self.tabBar.setMovable(True)
        self.setCurrentIndex(0)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_TabWidget()
    ui.show()
    sys.exit(app.exec_())

In the above program the widget intended for tab1 and tab2 functioned well. Anyhow i can't see a connection between the widget and the tabbar.. The tabbar tab is created independently and so the tabwidget tab. Both have tab titles the one given for tabbar alone is shown.. But if i set the index as o the first tab is shown along with widgetForTab1..

In my second program the lack of coupling between the widget and tabbar is the cause for the problem..

from PyQt4 import QtGui    
class Ui_TabWidget(QtGui.QTabWidget):
    def __init__(self,parent=None):
        QtGui.QTabWidget.__init__(self,parent)

        self.setObjectName("TabWidget")
        self.resize(400, 300)
        self.setWindowTitle(QtGui.QApplication.translate("TabWidget", "TabWidget", None, QtGui.QApplication.UnicodeUTF8))

        #Creating the tabbar
        self.tabBar=QtGui.QTabBar(self)

        #Adding the first tab
        self.tabBar.addTab("tab1")
        self.tabBar.setTabText(0,"TAB1")

        #Adding the second Tab
        self.tabBar.addTab("tab2")
        self.tabBar.setTabText(1,"TAB2")

        self.tabBar.setMovable(True)
        #Adding the tabbar to the tabwidget
        self.setTabBar(self.tabBar)

        #The widget intended for tab1
        self.widgetforTab1=QtGui.QWidget()
        self.addTab(self.widgetforTab1,"")
        self.buttonForTab1=QtGui.QPushButton(self.widgetforTab1)
        self.buttonForTab1.setText("Button in Tab1")

        #The widget intended for tab2
        self.widgetForTab2=QtGui.QWidget()
        self.addTab(self.widgetForTab2,"")
        self.labelForTab2=QtGui.QLabel(self.widgetForTab2)
        self.labelForTab2.setText("Label in Tab2")

        self.setCurrentIndex(0)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_TabWidget()
    ui.show()
    sys.exit(app.exec_())

The output for the second program was horrible.. I got four with first two tabs with no tab text and the tabs had Button in Tab1,Label in Tab2,Label in Tab2 and Label in Tab2 respectively.. Can you tell me why this is happening??? What should i do to solve this problem???


I know it's been a while since this question was asked, but I notice your response to the previous answer was that you needed to be able to customize the tabs using stylesheets. This is also possible when using QTabWidget! Since all QWidgets have setStyleSheet(), and QTabWidget uses a QTabBar under the hood, you can pretty much use the same stylesheet out of the box. For instance, here's a stylesheet I'm using to change the look of the tabs in my PyQt/PySide-based web browser, which uses QTabWidget:

QTabWidget::tab-bar {
    left: 2px; /* move to the right by 2px */
}

/* Style the tab using the tab sub-control. Note that
 * it reads QTabBar _not_ QTabWidget
 */
QTabBar::tab {
    min-width: 24px;
    max-width: 280px;
    padding: 2px 5px;
    font-size: .85em;
}

QTabBar::tab:!selected {
    margin-top: 2px; /* make non-selected tabs look smaller */
}

/* make use of negative margins for overlapping tabs */
QTabBar::tab:selected {
    /* expand/overlap to the left and right by 4px */
    margin-left: -2px;
    margin-right: -2px;
}

QTabBar::tab:first:selected {
    /* first selected tab has nothing to overlap with on the left */
    margin-left: 0;
}

QTabBar::tab:last:selected {
    /* last selected tab has nothing to overlap with on the right */
    margin-right: 0;
}

QTabBar::tab:only-one {
    /* if there is only one tab, we don't want overlapping margins */
    margin-left: 0;
    margin-right: 0;
}

The stylesheet itself definitely needs some tweaking, but it does demonstrate what's possible when applying styles to QTabWidget. You can also check out the QTabBar and QTabWidget section on the official Qt Style Sheets Examples page.


The best way to accomplish what you're trying here is to use a QTabWidget. It's much more convenient than QTabBar and will do everything you want 99.99% of the time.

Here's a canonical example of working with QTabWidget:

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.create_main_frame()       

    def create_main_frame(self):        
        tabs = QTabWidget()

        # Create first page
        page1 = QWidget()
        button1 = QPushButton('joy', page1)
        vbox1 = QVBoxLayout()
        vbox1.addWidget(button1)
        page1.setLayout(vbox1)

        # Create second page
        page2 = QWidget()
        label2 = QLabel('here I am')
        button2 = QPushButton('button on second page', page1)
        vbox2 = QVBoxLayout()
        vbox2.addWidget(label2)
        vbox2.addWidget(button2)
        page2.setLayout(vbox2)

        tabs.addTab(page1, 'First page')
        tabs.addTab(page2, 'Second page')

        self.setCentralWidget(tabs)



if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()
0

精彩评论

暂无评论...
验证码 换一张
取 消