当前位置: 代码迷 >> QT开发 >> QT 菜单栏的file选项显示不出来解决思路
  详细解决方案

QT 菜单栏的file选项显示不出来解决思路

热度:226   发布时间:2016-04-25 03:06:51.0
QT 菜单栏的file选项显示不出来
各位大侠求帮忙,本人刚学习QT,发现运行程序和教程不一样,代码如下:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void open();
    QAction *openAction;
};

#endif // MAINWINDOW_H



//mainwindow.cpp
#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QToolBar>
#include <QStatusBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
       setWindowTitle(tr("Main Window"));

       openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this);
       openAction->setShortcuts(QKeySequence::Open);
       openAction->setStatusTip(tr("Open an existing file"));
       connect(openAction, &QAction::triggered, this, &MainWindow::open);

    QMenu *file = menuBar()->addMenu(tr("&File"));
    file->addAction(openAction);

    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);

    statusBar();
}

void MainWindow::open()
{
    QMessageBox::information(this, tr("Information"), tr("Open"));
}

MainWindow::~MainWindow()
{

}

------解决思路----------------------
connect(openAction, &QAction::triggered, this, &MainWindow::open);

这个可以运行过去吗?
槽声明
public slots:
    void open();
    // 使用
    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

------解决思路----------------------
引用:
connect(openAction, &QAction::triggered, this, &MainWindow::open);

这个可以运行过去吗?
槽声明
public slots:
    void open();
    // 使用
    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));


这是新版本QT5的改进后的连接方法,

另外,从楼主的代码上看应该没有问题的,你的main函数没有贴出,菜单没有显示是不是隐藏了。
  相关解决方案