Search code examples
c++qtopenglqtcoreqcoreapplication

how i can get 'argc' and 'argv' from main function to QGLWidget


I have a class QGLWidget in my project, this is a opengl class, and in the class i use 'argc' and 'argv' attributes of the class main. but i can not get pass these attributes to the my class, there is some method for this?

this the part of constructor of my class using the argv and argc attributes :

VideoOpenGL::VideoOpenGL(QWidget *parent) :
    QGLWidget(parent)
{

    XnStatus nRetVal = XN_STATUS_OK;

    if (argc > 1)
    {
        nRetVal = g_Context.Init();
        //CHECK_RC(nRetVal, "Init");
        nRetVal = g_Context.OpenFileRecording(argv[1], g_Player);
        if (nRetVal != XN_STATUS_OK)
        {
            printf("Can't open recording %s: %s\n", argv[1], xnGetStatusString(nRetVal));
            return;
        }
    }

and my head file:

class VideoOpenGL : public QGLWidget
    {
        Q_OBJECT
    public:
        explicit VideoOpenGL(QWidget *parent = 0);
        //explicit VideoOpenGL( int & argc, char ** argv );
        //void set_mainAtribs(const int & argc, char **argv);


    protected:
        //const int argc;
        //char **argv;

        // /*
        void initializeGL();
        //void resizeGL(int w, int h);
        //void paintGL();
        static void glutKeyboard (unsigned char key, int /*x*/, int /*y*/);
        static void glutDisplay(void);
        static void glutIdle (void);
        void CleanupExit();
        void LoadCalibration();
        void SaveCalibration();
      // */
    signals:

    public slots:

    };

Solution

  • You have different ways to achieve depending on more precise context you have not yet provided.

    I will assume you are using a Qt application. If not, try to use a setter for the class which gets set directly or indirectly from the main function.

    If you use a Qt application, you could initialize the application object with the main function's arguments, and then you can access it anywhere by the static arguments() method.

    You will also need to convert the QString returned to QByteArray with the toUtf8() method and then with data() to char *, i.e.:

    arguments.at(1).toUtf8().data()
    

    Alternatively, which is probably even better, you can use the qPrintable() function. I will use this below.

    qPrintable(arguments.at(1))
    

    I will personally use qApp to access it, but you could use QCoreApplication::arguments(), too. I find the former also shorter and I am a lazy programmer for good. ;)

    You could write something like this if you happen to use a Qt application:

    main.cpp

    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
        ...
        return a.exec();
    }
    

    videoopengl.cpp

    VideoOpenGL::VideoOpenGL(QWidget *parent) :
        QGLWidget(parent)
    {
    
        XnStatus nRetVal = XN_STATUS_OK;
    
        QStringList arguments = qApp->arguments();
        if (arguments.size() > 1)
        {
            nRetVal = g_Context.Init();
            //CHECK_RC(nRetVal, "Init");
            nRetVal = g_Context.OpenFileRecording(qPrintable(arguments.at(1)), g_Player);
            if (nRetVal != XN_STATUS_OK)
            {
                printf("Can't open recording %s: %s\n", qPrintable(arguments.at(1)), xnGetStatusString(nRetVal));
                return;
            }
        }
    }