1

嗨,我有一个关于将 argv[] 值传递给其他类的问题。在我的代码中,我想将 argv[1] 参数从 cmd 传递给 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;
}

主窗口.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 ?

非常感谢

4

1 回答 1

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]);

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

public:
    MainWindow();
    MainWindow(char*);
    virtual ~MainWindow();
于 2013-06-06T09:14:08.610 回答