Adding pixmap to scene* gives segfault



  • I have code to load from a filepath into a QGraphicsView, a image file as so:

    void PDF::disp(QGraphicsView *view, QGraphicsScene *scene, const QString path)
    {
        QImage image(path);
        QFile f(path);
        QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
        scene->addItem(item);
        view->setScene(scene);
        view->show();
     //   imageVector.push_back(scene);
    }
    

    In one of my projects, this works. In my current project, it gives me a segmentation fault. I have tried other methods to load the exact same image but also gives me a segementation fault.
    My current usage example is

    void PDF::on_pageListView_currentRowChanged(int index)
    {
        QGraphicsScene* l;
        gen.removeAt(3);
        gen.insert(3, list.at(index));
        const QString i = gen.join("");
        disp(ui->leftView, l, i);
    }
    

    Other method that also gives a segfault:

    
    void Reader::DisplayPages(QGraphicsScene *source, QGraphicsView *target, QString imgSrc) {
        if (!imgSrc.isEmpty()) {
            QPixmap pix = QPixmap(imgSrc);
            source->addPixmap(pix);
        }
    
        target->setScene(source);
        target->fitInView(target->sceneRect(), Qt::KeepAspectRatioByExpanding);
        target->verticalScrollBar()->setValue(0);
        target->show();
        imageVector.push_back(source);
    }
    

  • Moderators

    @Akito_Kami Here you did not create a QGraphicsScene instance, so you pass a dangling pointer to disp:

    void PDF::on_pageListView_currentRowChanged(int index)
    {
        QGraphicsScene* l; // new ...?
        gen.removeAt(3);
        gen.insert(3, list.at(index));
        const QString i = gen.join("");
        disp(ui->leftView, l, i); // l points to nowhere
    }
    

    Besides that you should first debug step by step to see where exactly it crashes.



  • @jsulm Thanks for the reply. On my other program, this didn't occur. So I was thinking that wouldn't be the case. It now works.


Log in to reply
 

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