PDA

View Full Version : Tab keeps hover style after a context menu is closed



gui developer
24th September 2010, 14:45
Hi,

I have added a custom context menu to the QTabWidget. However, if a user moves mouse over one of the tabs, right-clicks to display the context menu, moves the mouse away and clicks on some other control, the menu disappears but the tab keeps its hover style.

A simple example of the way I am adding the context menu is below:


class ContextMenuTabWidget : public QTabWidget
{
Q_OBJECT
public:
ContextMenuTabWidget(QWidget * parent = 0);
public slots:
void showContextMenu(const QPoint& point);
}



ContextMenuTabWidget::ContextMenuTabWidget(QWidget * parent) : QTabWidget(parent)
{
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(showContextMenu(const QPoint&)));
}

void ContextMenuTabWidget::showContextMenu(const QPoint& point)
{
QMenu menu;
menu.addAction(tr("Menu Item"));
menu.exec(mapToGlobal(point));
}


How can I avoid the problem of the tab keeping its hover style?

Thanks in advance!

gui developer
29th September 2010, 01:44
If anyone is interested, I have solved it by firing a HoverLeave event if mouse moved away from the tab.

Something like:


int hoverTab = tabBar()->tabAt(tabBar()->mapFromGlobal(QCursor::pos()));
if (clickedTab != hoverTab) {
QEvent e(QEvent::HoverLeave);
QApplication::sendEvent(tabBar(), &e);
}