2013-06-06 89 views
1

嗨我有一個關於通過argv []值給其他職業的問題。在我的代碼中,我想將cmd中的argv [1]參數傳遞給類mainWindow以觸發空間事件。這是代碼。 #包括 「MainWindow.h」通過argv []從主到其他職業

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?

非常感謝

回答

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(); 
+0

但是,當我修改構造函數有返回的錯誤。這裏是mainwindow.h請問你看看? – saul203

+0

@ saul203,你也必須將新的構造函數添加到類聲明中(可能在頭文件中)。 – paxdiablo

+0

對不起,這個愚蠢的問題,但我很新的C++。 – saul203