How to find an index of clicked tab in QTabWidget in Python QT? -
i developing simple text editor has mutlitabs. want implement rightclick menu rename clicked (not current) tab of qtabwidget instance. have find index of clicked tab. how can this?
from pyqt4.qtcore import * pyqt4.qtgui import * class myapplication(...): ... def contextmenuevent(self, event): tabindex = ... # <- should type here? menu = qmenu(self) renameaction = menu.addaction("rename") action = menu.exec_(self.maptoglobal(event.pos())) if action == renameaction: self.renametabslot(tabindex) def renametabslot(self, tabindex): ...
you need check clicked position (i.e. event.pos ()
) against tab regions manually. python bit rusty, here's c++ code instead. assuming tabwidget called mytabwidget
:
int tabindex = -1; { qtabbar* tabbar = mytabwidget->tabbar (); qpoint globalpos = ->maptoglobal (event->pos ()); qpoint posintabbar = tabbar->mapfromglobal (globalpos); (int i=0; i<tabbar->count (); ++i) { if (tabbar->tabrect (i).contains (posintabbar)) { tabindex = i; break; } } } if (tabindex < 0) { // no tab hit... return; }
i didn't compile&run this, idea should clear. note maptoglobal
, mapfromglobal
calls transform given event's position tab-bars native coordinates. hope did right.
Comments
Post a Comment