PDA

View Full Version : QMenu on hover leave issue



lolcott
9th August 2017, 22:37
Hi all, real newbie here. I'm working with a custom qGraphicsItem. When i hover over it i display a contact menu. Works great


void pieces::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{


myMenu->clear();
setCursor(Qt::ClosedHandCursor);
sandbox *myScene = dynamic_cast<sandbox *>(scene());
if(myScene->mode()==sandbox::ModeMakeConnection)
{
QAction action1("EchoConnect", this);
myMenu->addAction(&action1);
myMenu->exec(QPoint(event->screenPos().x(),event->screenPos().y()));
}
}

however i'm trying to close it when i hover off. Running into issue here. I can't call the QGraphicsItem's hover leave because my focus is on the menu. so i'm trying to connect to qMenu's leaveEvent and failing. Here's what i'm trying:


myMenu = new QMenu();
connect(myMenu, SIGNAL(QWidget::leaveEvent(QEvent* event)), this, SLOT(popupLeave()));


but i get QObject::connect: No such signal QMenu::QWidget::leaveEvent(QEvent* event)

Any pointers??

Thanks in advance

d_stranz
9th August 2017, 23:31
Any pointers??

Yes, first use the correct syntax for the connect() statement:


connect(myMenu, SIGNAL(leaveEvent(QEvent*)), this, SLOT(popupLeave()));

but in your case, this won't solve the problem. Your problem is you are trying to connect a slot to something that is not a signal. QWidget::leaveEvent() is an ordinary protected member function, not a signal. Even though there are several variations on the QObject::connect(), in all cases the first method in the connect() must be declared as a signal in the class definition. The method used as a slot can be any member function of the receiver, even a lambda.

If you are trying to implement a context menu, then the QGraphicsItem::contextMenuEvent() exists for exactly that purpose.