Here is what I did here http://code.google.com/p/qtedit4/sou...itabwidget.cpp
{
if (obj != tabBar())
return QObject::eventFilter(obj, event
);
if (event
->type
() != QEvent::MouseButtonPress) return QObject::eventFilter(obj, event
);
// compute the tab number
QMouseEvent *mouseEvent
= static_cast<QMouseEvent
*>
(event
);
QPoint position
= mouseEvent
->pos
();
int c = tabBar()->count();
int clickedItem = -1;
for (int i=0; i<c; i++)
{
if ( tabBar()->tabRect(i).contains( position ) )
{
clickedItem = i;
break;
}
}
// just in case
if (clickedItem == -1)
return QObject::eventFilter(obj, event
);
switch( mouseEvent->button() )
{
case Qt::LeftButton:
return QObject::eventFilter(obj, event
);
break;
case Qt::RightButton:
on_rightMouse_pressed( clickedItem, position );
break;
case Qt::MidButton:
on_middleMouse_pressed( clickedItem, position );
break;
default:
return QObject::eventFilter(obj, event
);
}
return true;
}
bool qmdiTabWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj != tabBar())
return QObject::eventFilter(obj, event);
if (event->type() != QEvent::MouseButtonPress)
return QObject::eventFilter(obj, event);
// compute the tab number
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
QPoint position = mouseEvent->pos();
int c = tabBar()->count();
int clickedItem = -1;
for (int i=0; i<c; i++)
{
if ( tabBar()->tabRect(i).contains( position ) )
{
clickedItem = i;
break;
}
}
// just in case
if (clickedItem == -1)
return QObject::eventFilter(obj, event);
switch( mouseEvent->button() )
{
case Qt::LeftButton:
return QObject::eventFilter(obj, event);
break;
case Qt::RightButton:
on_rightMouse_pressed( clickedItem, position );
break;
case Qt::MidButton:
on_middleMouse_pressed( clickedItem, position );
break;
default:
return QObject::eventFilter(obj, event);
}
return true;
}
To copy to clipboard, switch view to plain text mode
I assume I would use a binary approach to reduce the time from O(n) to O(log(n)) [where n is the number of tabs), but in such magnitudes this optimization is irrelevant IMHO.
Bookmarks