QTCreator opengl mainwindow app - how to pass toolbar events into my sublcassed qglwidget?

  • I created my first QT opengl app (4.8.3), and it displays the hardcoded png file just fine. Now I want to use the QFileDialog and select different image files. I've created a method in my glwidget class to receive the new filename, but this class has no visibility in the MainWindow. Even with slots & signals, I have nothing to hang the call on in my MainWindow.

  • Figured it out with a little c++ obfuscation.

    in my mainwindow.cpp definition
    #include "glwidget.h"

    the method that gets the filename from the QFileDialog - after pressing a toolbar button that calls this slot:
    void MainWindow::on_actionLoadImage_triggered()
    QString strFilename = QFileDialog::getOpenFileName(this, "Open File", "", "Files (.)");
    QWidget *cw = centralWidget();
    if (strFilename.length() > 0)
    ((glwidget *)cw)->loadImage(strFilename);
    In my glwidget.h file,
    public slots:
    int loadImage(QString & filename);
    and in the glwidget.cpp file there is a method definition.

    In the VS2010 debug environment, the call went through to the method definition code.

  • Boy my reply looks ugly. How do folks post code that looks like code in this forum?

  • Moderators

    Let me prettify that for you... by sprinkling code markups over your post:-) Check the icons on top of the message window. The one to the far right is the one you want.

  • You could create an ordinairy mainwindow (Qtcreator will do that for you when you create a new project) and add an QGLWidget as child:
    OGLWin = new OGLWindow(...)
    This way you can handle all the GUI stuff via the mainwindow and the OpenGl stuff in the QGLWidget.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.