Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] application segfaults when assigning pixmap for Label



  • i am writing a qt4 gui application which has the following:
    -a main window with a widget which will hold:
    -an ogre instance (that is not important now, it is left empty)
    -a dockwidget with 2 buttons: one will cause the rendering of an image and show a dialog window, the other button just shows/hides the dialog window
    -a dialog window to show the image (i have read somewhere that i should just put a label and use setpixmap to assign a pixmat to it)

    here are my sources: http://pastebin.com/HXkT3Jut
    here are the ui files: http://pastebin.com/0TKKDziv

    the segfault happens at this line:
    raytracerRenderDialog->renderedImageContainer->setPixmap(QPixmap::fromImage(renderedImage));

    a few notes:
    -i know that the way i implemented on_actionShow_render_triggered and on_actionRender_triggered is not the correct way, it is just temporary to see how it works
    -the application flow is like this: press render button -> generate raw image pixels -> show generated image in dialog


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you check that the QImage is valid ? e.g. do you have Tux.png in the same folder as your application executable ?



  • i'm pretty sure that it loads the image, because when i run it in debug mode, in the locals it displays the resolution of the image


  • Lifetime Qt Champion

    You should confirm it with a test like

    @
    if (!renderedImage.isNull())
    qDebug() << "invalid image";
    @

    On a related note, you can create a QPixmap from a file, that would save you the conversion there



  • modified the code as follows:
    @void RaytracerMainWindow::on_actionRender_triggered()
    {
    QImage renderedImage;
    renderedImage.load("Tux.png");
    if(!renderedImage.isNull()){
    qDebug() << "invalid image";
    }else{
    qDebug() << "image loaded";
    }

    raytracerRenderDialog->renderedImageContainer->show();
    
    //raytracerRenderDialog->renderedImageContainer->setPixmap(QPixmap::fromImage(renderedImage));
    
    ui->actionShow_render->setChecked(true);
    

    }@

    it outputs "image loaded "

    and on a sidenote raytracerRenderDialog->renderedImageContainer->show(); crashes the application

    could you please take a look at the RaytracerRenderDialog class?
    i think the problem comes from there.

    see i created a mainwindow (RaytracerMainWindow) and a dialog (RenderDialog), the creator generated the correct headers and classes for them.
    but when i tried to instanciate a RaytracerRenderDialog inside RaytracerMainWindow, the compiler threw all kinds of redefinition error and such.
    so i read somewhere i must derive a class to be able to instantiate it.
    i think something is messed up in the constructor of RaytracerRenderDialog.

    in the past few yeard i coded mostly strictly in C, so it is hard for me to get back to the C++ mentality, let aloge getting used to QT's own mechanisms.

    thanks


  • Lifetime Qt Champion

    You are doing the test wrong, if isNull() is true you have an invalid image

    Are you sure renderedImageContainer is initialized correctly ?



  • yeah, that was a copy-paste problem, and it did not have the tux.png next to it because i moved the project folder and the build folder changed too...
    yeah and i copy pasted your code without actually checking... :D

    i THINK i initialize it correctly:
    @RaytracerMainWindow::RaytracerMainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::RaytracerMainWindow)
    {
    ui->setupUi(this);
    raytracerRenderDialog= new RaytracerRenderDialog(parent);
    }@

    which calls this:
    @RaytracerRenderDialog::RaytracerRenderDialog(QWidget *parent):
    QDialog(parent),
    renderDialog(new Ui::RenderDialog)
    {
    renderDialog->setupUi(this);
    }@

    somewhere in the middle i realized that qt widget classes keep a reference to themselves as a pointer of ther class, but some things are not very clean to me

    if it would help i could upload an archive of the project itself


  • Lifetime Qt Champion

    Where is renderedImageContainer declared ?



  • since it was added with the desginer:

    <widget class="QLabel" name="renderedImageContainer">
    <property name="styleSheet">
    <string notr="true">background-color: rgb(128, 128, 128);</string>
    </property>
    <property name="text">
    <string/>
    </property>
    </widget>

    so it is declared in the intermediary ui_renderdialog.h, which is generated.

    i assumed it should be initialized since i call the setuUi method for RenderDialog from RaytracerRenderDialog


  • Lifetime Qt Champion

    Did you run your application through the debugger ?



  • solved it.

    added
    @void RenderDialog::setRenderedImage(QPixmap pixmap)
    {
    ui->renderedImageContainer->setPixmap(pixmap);
    }@

    to RenderDialog

    and called it like this from raytreacermainwindow:
    @void RaytracerMainWindow::on_actionRender_triggered()
    {
    QImage renderedImage;
    renderedImage.load("Tux.png");
    if(renderedImage.isNull()){
    qDebug() << "invalid image";
    }else{
    qDebug() << "image loaded";
    }

    RenderDialog *renderDialog= new RenderDialog();
    
    renderDialog->setRenderedImage(QPixmap::fromImage(renderedImage));
    
    renderDialog->show();
    

    }@


Log in to reply