窗体部件之QMenu

QMenu常见的分为以下几种:

(1)右键菜单

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    m_menu = new QMenu(this);

    m_act = new QAction("hello", m_menu);

    m_menu->addAction(m_act);

    //注意exec和popup两种方式的区别
//    m_menu->exec(QCursor::pos());
//    m_menu->popup(QCursor::pos());
    m_menu->popup(mapToGlobal(event->pos()));
}

(2)按钮菜单

    pushBtn = new QPushButton("hello", this);
    menu = new QMenu(this);
    act1 = new QAction("1", menu);
    act2 = new QAction("2", menu);
    menu->addAction(act1);
    menu->addAction(act2);

    pushBtn->setMenu(menu);

    //去掉按钮显示下标
    pushBtn->setStyleSheet("QPushButton::menu-indicator{image:None;}");

(3)托盘菜单

    QSystemTrayIcon *systemTrayIcon = new QSystemTrayIcon(this);
    systemTrayIcon ->setToolTip(QString("hello"));
    systemTrayIcon ->setIcon(QIcon(":/icon.png"));
    QMenu *menu = new QMenu(this);
    QAction *act = new QAction("hello", menu);
    menu->addAction(act);
    systemTrayIcon->setContextMenu(menu);

    systemTrayIcon->show();
    systemTrayIcon->showMessage(QString("托盘"), QString("hello"));

你可能感兴趣的:(Qt5.5.0+)