0

我的项目中有一个 QGLWidget 类,这是一个 opengl 类,在该类中我使用主类的“argc”和“argv”属性。但我无法将这些属性传递给我的班级,有一些方法吗?

这是使用 argv 和 argc 属性的类的构造函数的一部分:

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;
        }
    }

和我的头文件:

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:

    };
4

2 回答 2

4

根据您尚未提供的更精确的上下文,您有不同的方法来实现。

我将假设您正在使用 Qt 应用程序。如果没有,请尝试对从主函数直接或间接设置的类使用设置器。

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;
        }
    }
}
于 2014-05-22T03:05:39.303 回答
3

您始终可以在应用程序的任何位置使用静态QCoreApplication::arguments()函数。

于 2014-05-22T02:19:23.323 回答