将argv []从main传递给其他类

时间:2013-06-06 09:09:39

标签: constructor parameter-passing argv

您好我有关于将argv []值传递给其他类的问题。在我的代码中,我想将cmd中的argv [1]参数传递给类mainWindow以触发空间事件。这是代码。 #include“MainWindow.h”

int main(int argc, char *argv[])
{
    #if _DEBUG
        // Do not print memory leaks at end of program (QtWebKit causes a large number of them, see Menu class)
        _CrtSetDbgFlag(_crtDbgFlag &~ _CRTDBG_LEAK_CHECK_DF);
    #endif

    QApplication  app(argc, argv);

    app.setApplicationName("Example_Qt");

    MainWindow window;
    window.show();
    return app.exec();
}

MainWindow.cpp

MainWindow::MainWindow() :
    m_pCurrentTutorial(0),
    m_pCurrentTutorialAREL(0)
{
    setupUi(this);

    quitTutorialButton->setVisible(false);

    QObject::connect(quitTutorialButton, SIGNAL(clicked()), this, SLOT(onQuitTutorialButtonClicked()));

    m_pMenu = new Menu(this, this);

    // Init the main view for the scene using OpenGL
    QGLWidget *glWidget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
    m_pGraphicsView = graphicsView;
    m_pGraphicsView->setScene(m_pMenu);
    m_pGraphicsView->setViewport(glWidget);
    m_pGraphicsView->setFrameShape(QFrame::NoFrame);

    // Do not show context menu in web view
    m_pGraphicsView->setContextMenuPolicy(Qt::NoContextMenu);
}

MainWindow::~MainWindow()
{
    delete m_pMenu;
    m_pMenu = 0;

    delete m_pGraphicsView;
    m_pGraphicsView = 0;
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <vector>

#include <QVBoxLayout>
#include <QWidget>
#include <QWebView>

class TutorialBase;
class TutorialBaseAREL;
#include "Menu.h"

#include "ui_MainWindow.h"

QT_BEGIN_NAMESPACE
class QGraphicsView;
class QBoxLayout;
QT_END_NAMESPACE


class MainWindow : public QMainWindow, public Ui::MainWindow, public Menu::TutorialSelectionCallback
{
    Q_OBJECT

public:
    MainWindow();
    virtual ~MainWindow();

    QBoxLayout* getButtonBar();

protected slots:
    void onQuitTutorialButtonClicked();

protected:
    void keyPressEvent(QKeyEvent *event);

    void quitTutorialIfAny();

    void startTutorial(int tutorialNumber);

    void startTutorialAREL(int tutorialNumber);

    TutorialBase*               m_pCurrentTutorial;
    TutorialBaseAREL*           m_pCurrentTutorialAREL;
    QGraphicsView*              m_pGraphicsView;
    Menu*                       m_pMenu;
};


#endif

有人可以帮我将参数从argv []传递给mainwindow.cpp吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

您应该能够提供另一个构造函数(或替换当前构造函数),该构造函数采用单个char *参数,例如:

MainWindow::MainWindow (char *arg) :
    m_pCurrentTutorial(0),
    m_pCurrentTutorialAREL(0)
{
    // body here, including checking argument, such as:
    if (strcmp (arg, "-help") == 0) {
        provideHelp();
    }
}

然后在创建对象时使用 构造函数:

MainWindow window (argv[1]);

请注意,这个新构造函数需要在类中列出(在Mainwindow.h中),而不仅仅作为函数添加到Mainwindow.cpp

public:
    MainWindow();
    MainWindow(char*);
    virtual ~MainWindow();