QT(4.8.4)设计师怎么给菜单项设置信号槽呢

QT(4.8.4)设计师怎么给菜单项设置信号槽呢,第1张

QAction *m_act = new QAction("Open Folder", this)

QMenu *m_Menu = = new QMenu(this)

m_Menu->addAction(m_act) //菜单绑定动作

connect(m_act, SIGNAL(triggered()), this, SLOT(OpenAct()))//////m_act,选择后发送链接到OpenAct()

Qt 5.6样例:

MainWindow.h:

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QObject>

#include <QMainWindow>

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

    MainWindow(QWidget *parent = 0)

    ~MainWindow()

private:

    void initialAction()

    void initialMenuBar()

    void initialToolBar()

    void initialStatusBar()

    void open()

    QAction *openAction

}

#endif // MAINWINDOW_HMainWindow.cpp:#include <QAction>

#include <QMenuBar>

#include <QMessageBox>

#include <QStatusBar>

#include <QToolBar>

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent)

{

    setWindowTitle(tr("Main Window"))

    initialAction()

    initialMenuBar()

    initialToolBar()

    initialStatusBar()

}

MainWindow::~MainWindow()

{

}

void MainWindow::initialAction()

{

    openAction = new QAction(QIcon(":/images/doc-open"), QObject::tr("&Open..."), this)

    openAction->setShortcuts(QKeySequence::Open)

    connect(openAction, &QAction::triggered, this, &MainWindow::open)

}

void MainWindow::initialMenuBar()

{

    QMenu *file = menuBar()->addMenu(QObject::tr("&File"))

    file->addAction(openAction)

}

void MainWindow::initialToolBar()

{

    QToolBar *toolBar = addToolBar(QObject::tr("&File"))

    toolBar->addAction(openAction)

}

void MainWindow::initialStatusBar()

{

    statusBar()

    openAction->setStatusTip(QObject::tr("Open an existing file"))

}

void MainWindow::open()

{

    QMessageBox::information(this, tr("Information"), tr("Open"))

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11621805.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存