Teh Ki
Teh Ki

Reputation: 455

PyQt QTabBar is not creating new tabs

I am trying to add new tabs into a QTabBar and it has been unsuccessful.

What I am trying to achieve is that when you click onto the 'add' icon, there will be 3 menu items. Depending on which item is selected, it will create a new tab besides the 'add' icon.

For example, if I selected the option - food, the gui will appear something as follows:

| + | food |

In my set_new_tab(), while it appears to have printed out the option I have selected, the addTab does not seems to be doing anything.

Wondering if I have missed out something, or if a QTabBar should be used in conjunction with QTabWidget?

This is my code:

class MyWin(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__()

        self.tab_bar = QtGui.QTabBar()
        self.add_button = QtGui.QToolButton(self)
        self.add_button.setIcon(QtGui.QIcon('add.png'))
        self.add_button.setMenu(self.set_menu())
        self.add_button.setPopupMode(QtGui.QToolButton.InstantPopup)

        self.tab_bar.setTabButton(
            0,
            QtGui.QTabBar.ButtonPosition.RightSide,
            self.add_button
        )

    def set_menu(self):
        menu_options = ['food', 'drinks', 'snacks']
        qmenu = QtGui.QMenu(self.add_button)

        for opt in menu_options:
            qmenu.addAction(opt, partial(self.set_new_tab, opt))
        qmenu.addAction
        return qmenu

    def set_new_tab(self, opt):
        print ">>> opt clicked on : ", opt
        self.tab_bar.addTab(opt) # This does not seems to creating new tabs..

Appreciate for any advice.

Upvotes: 1

Views: 297

Answers (1)

eyllanesc
eyllanesc

Reputation: 243983

The problem is not to create in the tabs but in that the size of the tab is small and is behind the button, so the solution is to use the layouts to place the widgets correctly.

from functools import partial
from PyQt4 import QtGui

class MyWin(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__()
        central_widget = QtGui.QWidget()
        self.setCentralWidget(central_widget)
        vlay = QtGui.QVBoxLayout(central_widget)
        hlay = QtGui.QHBoxLayout()
        vlay.addLayout(hlay)
        vlay.addStretch()

        self.add_button = QtGui.QToolButton()
        self.tab_bar = QtGui.QTabBar(self)
        self.add_button.setIcon(QtGui.QIcon('add.png'))
        self.add_button.setMenu(self.set_menu())
        self.add_button.setPopupMode(QtGui.QToolButton.InstantPopup)

        self.tab_bar.setTabButton(
            0,
            QtGui.QTabBar.ButtonPosition.RightSide,
            self.add_button
        )
        hlay.addWidget(self.add_button)
        hlay.addWidget(self.tab_bar)

    def set_menu(self):
        menu_options = ['food', 'drinks', 'snacks']
        qmenu = QtGui.QMenu(self.add_button)
        for opt in menu_options:
            qmenu.addAction(opt, partial(self.set_new_tab, opt))
        qmenu.addAction
        return qmenu

    def set_new_tab(self, opt):
        print(">>> opt clicked on : ", opt)
        self.tab_bar.addTab(opt)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MyWin()
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions