项目中,使用QMenu,多个QAction时 超出页面范围,需要菜单栏可以上下滚动。
实际QMenu是带滚动的,但是要知道怎么使用 还是需要查看QT源码,现在简单记录下我的使用方法。
QT源码中:q->style()->styleHint(QStyle::SH_Menu_Scrollable, 0, q) 是支持滚动样式的,需要配置
void QMenuPrivate::init()
{
QMenu * const q = q_func();
Q_Q(QMenu);
#ifndef QT_NO_WHATSTHIS
q->setAttribute(Qt::WA_CustomWhatsThis);
#endif
q->setAttribute(Qt::WA_X11NetWmWindowTypePopupMenu);
defaultMenuAction = menuAction = new QAction(q);
menuAction->d_func()->menu = q;
QObject::connect(menuAction, &QAction::changed, [=] {
if (!tornPopup.isNull())
tornPopup->updateWindowTitle();
});
q->setMouseTracking(q->style()->styleHint(QStyle::SH_Menu_MouseTracking, 0, q));
if (q->style()->styleHint(QStyle::SH_Menu_Scrollable, 0, q)) {
scroll = new QMenuPrivate::QMenuScroller;
scroll->s