Python Forum
QTabBar Indexing is wrong for right mouse click
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTabBar Indexing is wrong for right mouse click
#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.
Reply


Messages In This Thread
RE: QTabBar Indexing is wrong for right mouse click - by xenas - Jan-04-2019, 10:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 867 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 7,458 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Tkinter] program unresponsive during pynput mouse click RobotTech 1 3,503 May-07-2020, 04:43 PM
Last Post: RobotTech
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,769 Dec-16-2019, 04:47 AM
Last Post: woooee
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,031 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [PyQt] `QPushButton` without mouse click animation Atalanttore 2 5,873 Apr-22-2019, 01:00 PM
Last Post: Atalanttore
  right mouse button click with PyQt5 brecht83 4 19,422 Nov-09-2018, 02:55 PM
Last Post: brecht83
  [PyQt] QSlider jump to mouse click position MegasXLR 2 8,261 May-26-2018, 08:55 AM
Last Post: MegasXLR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020