首页 文章

如何在Qt中使用qAction子菜单

提问于
浏览
0

当我点击主窗口中的子菜单时,我想实现像qDebug()这样的简单命令 . 我指的是与Qt 5 IDE( ...\Qt\Qt5.2.0\5.2.0\msvc2010\examples\widgets\mainwindows\menus )一起提供的示例程序,并使用它,我设法构建代码 . 我没有收到任何编译时或运行时错误 .

我使用设计模式创建了mainwindow.ui . 它有一个名为actionInterval的QAction类的对象 .
snapshot of UI, which requires action when I click on Interval button

但是当我点击它时,没有任何反应,我无法在void interval()中实现该命令 . 我想我没有正确连接 . 我在这里错过了什么?请指教 .

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private:
    Ui::MainWindow *ui;
    void createActions();

private slots:
    void interval();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    createActions();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createActions()
{
    ui->actionInterval = new QAction(tr("&Interval"), this);
    ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
    connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}

void MainWindow::interval()
{
    qDebug()<<"inside interval qdialog";
}

main.cpp中

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

1 回答

  • 1
    void MainWindow::createActions()
    {
        ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
        connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
    }
    

    你不应该需要 ui->actionInterval = new QAction(tr("&Interval"), this); 行, ui->setupUi() 为你处理,所以's potentially causing an incorrect reference so when you do click on it it'没有正确触发 .

相关问题