然后我将QWidgetAction添加到菜单中:
QMenu menu = new QMenu(this);
QCustomWidget* widget = new QCustomWdiget(this); // inherits from QWidget
QCustomAction* action = new QCustomAction(this); // Inherits from QWidgetAction
action->setDefaultWidget(widget);
menu->addAction(action);我只是得到了一个不继承普通菜单项行为的小部件。我尝试设置我的自定义小部件的sytlesheet。有没有办法继承标准操作项的样式和/或行为?
发布于 2014-09-11 20:45:02
这是一个让hover工作的解决方案:
void QCustomWidget::enterEvent(QEvent* e)
{
emit entered();
}
void QCustomAction::onWidgetEntered()
{
if (QMenu* menu = qobject_cast<QMenu*>(sender()->parent()))
{
menu->setActiveAction(this);
}
}
// when you created both:
connect(customWidget, SIGNAL(entered()), customAction, SLOT(onWidgetEntered()));发布于 2016-05-27 15:48:04
@Ezee我试过你的方法,但是没有效果。我使用enterEvent和leaveEvent来实现悬停效果;当鼠标移到动作上时,代码会运行到enterEvent中,但是样式表不起作用。
但是我发现了另一种实现我想要的图标的方法,当鼠标悬停在动作上时,我可以使用QIcon::addIcon(QIcon::feature.What,XXXXX).In,这样我就可以很容易地更改悬停图标。
https://stackoverflow.com/questions/25784217
复制相似问题