Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:12:06+00:00 2026-06-17T11:12:06+00:00

Is there an easy way to use QMatrix4x4 with OpenGL functions, specifically glMultMatrixf ?

  • 0

Is there an easy way to use QMatrix4x4 with OpenGL functions, specifically glMultMatrixf?

If I understand this right, I’d have to transpose the matrix, and be sure to convert qreal (which can be either float or double depending on the underlying system) to GLfloat.
Isn’t there a function which does this for me?

I also have the same problem with QVector3D, which again I need as GLfloat array in the function glVertex3fv.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:12:10+00:00Added an answer on June 17, 2026 at 11:12 am

    First I want to mention that QMatrix4x4 has built-in operators for matrix multiplication (with a second matrix, a vector or a scalar). However, your question still needs an answer, as you want to pass a matrix to OpenGL sooner or later.

    QMatrix4x4 uses qreals for internal representation. While the class is intended to be used with OpenGL directly (using constData() as suggested by Bart in his answer), you can make sure you pass it to the corresponding OpenGL function depending on the type, if you want to keep platform compatibility (on embedded devices, qreal is float!):

    // these are defined in the OpenGL headers:
    void glMultMatrixf(const GLfloat *m);
    void glMultMatrixd(const GLdouble *m);
    
    // add overloaded functions which call the underlying OpenGL function
    inline void glMultMatrix(const GLfloat  *m) { glMultMatrixf(m); }
    inline void glMultMatrix(const GLdouble *m) { glMultMatrixd(m); }
    
    // add an overload for QMatrix4x4 for convenience
    inline void glMultMatrix(const QMatrix4x4 &m) { glMultMatrix(m.constData()); }
    

    You can also use this mechanism for vectors, here the glVertex* family, where it makes even more sense because of the number of components the “raw pointer” overloads need to consider, but the object oriented overloads can do automatically for you:

    inline void glVertex2v(const GLfloat  *v) { glVertex2fv(v); }
    inline void glVertex2v(const GLdouble *v) { glVertex2dv(v); }
    inline void glVertex3v(const GLfloat  *v) { glVertex3fv(v); }
    inline void glVertex3v(const GLdouble *v) { glVertex3dv(v); }
    inline void glVertex4v(const GLfloat  *v) { glVertex4fv(v); }
    inline void glVertex4v(const GLdouble *v) { glVertex4dv(v); }
    
    // Note that QVectorND use floats, but we check this during compile time...
    Q_STATIC_ASSERT(sizeof(QVector2D) == 2*sizeof(float));
    Q_STATIC_ASSERT(sizeof(QVector3D) == 3*sizeof(float));
    Q_STATIC_ASSERT(sizeof(QVector4D) == 4*sizeof(float));
    inline void glVertex(const QVector2D &v) { glVertex2v(reinterpret_cast<const float*>(&v)); }
    inline void glVertex(const QVector3D &v) { glVertex3v(reinterpret_cast<const float*>(&v)); }
    inline void glVertex(const QVector4D &v) { glVertex4v(reinterpret_cast<const float*>(&v)); }
    
    // Even for QPointF we can do it!
    Q_STATIC_ASSERT(sizeof(QPointF) == 2*sizeof(qreal));
    inline void glVertex(const QPointF &v) { glVertex4v(reinterpret_cast<const qreal*>(&v)); }
    

    So your code keeps valid if the following changes are made:

    • Qt changes the representation of QVector* / QMatrix to use floats / doubles respectively,
    • Your code changes the number of components of vectors

    … while especially the second isn’t the case when using raw OpenGL commands like glVertex3f.

    The Q_STATIC_ASSERTions in the code above are only defined since Qt 5.0. If you are using Qt4 (or code with Qt4 compatibility), add this in some global header file / before your definitions: http://ideone.com/VDPUSg [source: Qt5 / qglobal.h]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering there is any easy way to use masks/cliping with openGL in objective
Is there any easy way to use grep functionality in NSIS scripts? I have
Is there an easy way to use UIElement.Effect in WPF to shift the color
Is there an easy way to use DirectX in Java? In particular, DirectX's video
Is there an easy way to use Bootstrap in an existing project? Currently it
Is there an easy way to calculate the derivative of non-liner functions that are
Is there an easy way to use Spring IoC for a factory which returns
Is there an easy way to use the google maps searchbar to only recieve
Is there any easy way to use the UIButton icons (UIButtonTypeInfoLight) on a UIBarButton
Is there an easy way to use a Kohana DB config file in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.