菜单条 Menu Bar Action

时间:2023-01-16 14:19:11

//
.h /***Action**/ QAction * act_openImage; QAction * act_openVideo; QAction * act_openAudio; /***Menu***/ QMenu * menu_Image; QMenu * menu_Video; QMenu * menu_Audio;
private:

void createActions();

void createMenus();
public slots:
    void slotOpenImage();
    void slotOpenVideo();
    void slotOpenAudio();
//.cpp

void MainWindow::createActions()
{
    act_openImage = new QAction(QIcon(":/main/res/图片.png"),tr("图片"),this);
    act_openVideo = new QAction(QIcon(":/main/res/视频.png"),tr("视频"),this);
    act_openAudio = new QAction(QIcon(":/main/res/音乐.png"),tr("音乐"),this);

    connect(act_openImage,SIGNAL(triggered(bool)),this,SLOT(slotOpenImage()));
    connect(act_openVideo,SIGNAL(triggered(bool)),this,SLOT(slotOpenVideo()));
    connect(act_openAudio,SIGNAL(triggered(bool)),this,SLOT(slotOpenAudio()));
}

void MainWindow::createMenus()
{
    menu_Image = menuBar()->addMenu(tr("图片"));
                //MenuBar()返回 menu bar for the main window
                //addMenu() Append menu to the menu bar
    menu_Image->addAction(act_openImage);

    menu_Video = menuBar()->addMenu(tr("视频"));
    menu_Video->addAction(act_openVideo);

    menu_Audio = menuBar()->addMenu(tr("音频"));
    menu_Audio->addAction(act_openAudio);

}

效果:

菜单条 Menu Bar Action菜单条 Menu Bar Action菜单条 Menu Bar Action菜单条 Menu Bar Action