Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTabBar Indexing is wrong for right mouse click
#1
Hi all, I am trying to grab ahold of the mouse position + right mouse click on QTabBar where it will pops up a message to User if they want to remove the said tab.

(Pardon the vague design) This is what my QTabBar looks like:
| + | food | snacks | drinks |
However, in my following code, whenever I tried to print out the index as I do a right-mouse click on the tabs, the returned index value is wrong.
It seems to have taken into account of the ‘+’ button as it is indicating as index 0 where index 0 should actually starts from the ‘food’ tab onwards.

Here is my code:
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):
        self.tab_bar.addTab(opt)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.RightButton:
            index = self.tab_bar.tabAt(event.pos())
            print index
            menu = QtGui.QMenu(self)
            action = menu.addAction('Remove tab', partial(self.removal_tab, index))
        else:
            super(MyWin, self).mousePressEvent(event)
    
    def removal_tab(self, index):
        self.tab_bar.removeTab(index)


my_win = MyWin()
my_win.show()
Appreciate for any insights and many thanks in advance for any response!
Quote
#2
That's only a part of the code.

what is opt, partial ?

and I miss something like this at the end

if __name__ == '__main__':
    import sys
    app = GtGui.QApplication(sys.argv)
    my_win = MyWin()
    my_win.show()
    app.exec()
Quote
#3
Hi Axel,

partial are part of the import module that I did not added in my above code.
And so it should be (I can't seem to find the 'edit; to my post):
from functools import partial

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):
        self.tab_bar.addTab(opt)
 
    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.RightButton:
            index = self.tab_bar.tabAt(event.pos())
            print index
            menu = QtGui.QMenu(self)
            action = menu.addAction('Remove tab', partial(self.removal_tab, index))
        else:
            super(MyWin, self).mousePressEvent(event)
     
    def removal_tab(self, index):
        self.tab_bar.removeTab(index)
 
 
my_win = MyWin()
my_win.show()
opt is part of the menu options as stated in line 28.
This is a complete code despite I did not added in the execution code as you have defined, as the tool is still operatable.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] PyQt4 handle dynamic checkbox click littleGreenDude 1 113 Dec-27-2018, 09:17 PM
Last Post: littleGreenDude
  right mouse button click with PyQt5 brecht83 4 403 Nov-09-2018, 02:55 PM
Last Post: brecht83
  [Tkinter] loop function when called from tkinter button click WantedStarling 5 698 Jul-13-2018, 06:12 PM
Last Post: nilamo
  [PyQt] QSlider jump to mouse click position MegasXLR 2 1,009 May-26-2018, 08:55 AM
Last Post: MegasXLR
  Tkinter touch screen button click ashtona 2 1,331 Apr-13-2018, 11:46 AM
Last Post: buran
  Screen Shots with mouse cursor anurag2904 3 974 Mar-06-2018, 05:36 AM
Last Post: Larz60+
  Button click doesnt work from my second class/layout in Python imamideb 0 467 Feb-13-2018, 12:09 PM
Last Post: imamideb
  Error while changing button relief from raised to sunken on left click mgtheboss 1 1,067 Jan-09-2018, 06:05 PM
Last Post: Gribouillis
  Problem with pygame not switching the mouse image Mondaythe1st 6 1,491 Jul-26-2017, 10:53 PM
Last Post: metulburr
  Python minesweeper game gtk3 get label value after click lukassz 0 859 Mar-25-2017, 08:14 PM
Last Post: lukassz

Forum Jump:


Users browsing this thread: 1 Guest(s)