1. 程式人生 > >Qt實現多級選單

Qt實現多級選單

       Qt實現(多級)選單 在Qt中通過QMenu和QAction就可以實現這些選單,QMenu就是上圖中我們看到的三個級聯選單,每個QMenu裡面的小專案就是一個QAction,一個QAction由一個圖示和一段文字描述組成。使用快捷鍵觸發事件,需重寫void keyPressEvent(QKeyEvent *event)方法。事例見下連結:https://blog.csdn.net/qq_16093323/article/details/83341880

下面實現多級選單的構建:

    QMenu *menu = new QMenu();    

    SystemMenu = new QMenu(this);
    SystemMenu->setTitle(tr("幫助"));
    QIcon icon(":/images/help1.png");
    SystemMenu->setIcon(icon);

    LogMgtMenu = new QMenu(this);
    LogMgtMenu->setTitle(tr("日誌管理"));
    QIcon icon2(":/images/logMgn.png");
    LogMgtMenu->setIcon(icon2);

    SettingMenu = new QMenu(this);
    SettingMenu->setTitle(tr("設定"));
    QIcon icon4(":/images/setting.png");
    SettingMenu->setIcon(icon4);

    menu->addMenu(LogMgtMenu);
    menu->addMenu(SettingMenu);
    menu->addMenu(SystemMenu);

    HandBookAction = new QAction(this);
    HandBookAction->setText(tr("IPS 幫助"));
    HandBookAction->setShortcut(Qt::Key_F1);
    connect(HandBookAction,SIGNAL(triggered()),this,SIGNAL(HandBook()));
    QIcon icon(":/images/help1.png");
    HandBookAction->setIcon(icon);

    AboutAction = new QAction(this);
    AboutAction->setText(tr("關於"));
    connect(AboutAction,SIGNAL(triggered()),this,SIGNAL(ShowAbout()));
    QIcon icon1(":/images/about.png");
    AboutAction->setIcon(icon1);

    ExitAction = new QAction(this);
    ExitAction->setText(tr("退出"));
    ExitAction->setShortcut(Qt::CTRL|Qt::Key_Q);
    connect(ExitAction, SIGNAL(triggered()), this, SIGNAL(Exit()));
    QIcon icon2(":/images/exit.png");
    ExitAction->setIcon(icon2);

    SystemMenu->addAction(HandBookAction);
    SystemMenu->addAction(AboutAction);

    menu->addAction(ExitAction);