Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding pixmap to scene* gives segfault
Forum Updated to NodeBB v4.3 + New Features

Adding pixmap to scene* gives segfault

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Akito_KamiA Offline
    Akito_KamiA Offline
    Akito_Kami
    wrote on last edited by Akito_Kami
    #1

    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);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • Akito_KamiA Akito_Kami

      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);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Akito_KamiA 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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.

        Akito_KamiA Offline
        Akito_KamiA Offline
        Akito_Kami
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved