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. Using a QGraphicsView Widget
Forum Updated to NodeBB v4.3 + New Features

Using a QGraphicsView Widget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 776 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mister Rose
    wrote on last edited by
    #1

    Maybe I am not following the correct workflow, but this has worked on other elements.

    I am using Qt Creator 3.6.1, which has Qt 5.6.0 MSVC 2013 on a Windows PC.

    I added a QGraphicsView widget to the Window form, and I want to draw a rectangle in it.

    In the form's constructor, I put the following code, the last line of which causes a segmentation fault. Note that the rect is a data member of the Form's class. (The first line was already there).

    ui->setupUi(this);
    rect.setRect(10,10,20,30);
    ui->graphicsView->scene()->addItem(&rect);
    

    I also tried putting the rect on the heap like this.

    rect = new QGraphicsRectItem(10,10,20,30);
    ui->graphicsView->scene()->addItem(rect);
    

    Same problem. The Segfault occurs in the addItem call.

    Does the scene need to be allocated? I don't see a setScene method, so I assumed that the framework initialized everything.

    Thoughts?

    ? 1 Reply Last reply
    0
    • M Mister Rose

      Maybe I am not following the correct workflow, but this has worked on other elements.

      I am using Qt Creator 3.6.1, which has Qt 5.6.0 MSVC 2013 on a Windows PC.

      I added a QGraphicsView widget to the Window form, and I want to draw a rectangle in it.

      In the form's constructor, I put the following code, the last line of which causes a segmentation fault. Note that the rect is a data member of the Form's class. (The first line was already there).

      ui->setupUi(this);
      rect.setRect(10,10,20,30);
      ui->graphicsView->scene()->addItem(&rect);
      

      I also tried putting the rect on the heap like this.

      rect = new QGraphicsRectItem(10,10,20,30);
      ui->graphicsView->scene()->addItem(rect);
      

      Same problem. The Segfault occurs in the addItem call.

      Does the scene need to be allocated? I don't see a setScene method, so I assumed that the framework initialized everything.

      Thoughts?

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @Mister-Rose Hi! Yes, you need to create the scene, too, and give it to the view:

      QGraphicsScene *scene = new QGraphicsScene(this);
      ui->graphicsView->setScene(scene);
      
      M 1 Reply Last reply
      0
      • ? A Former User

        @Mister-Rose Hi! Yes, you need to create the scene, too, and give it to the view:

        QGraphicsScene *scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        
        M Offline
        M Offline
        Mister Rose
        wrote on last edited by
        #3

        @Wieland said in Using a QGraphicsView Widget:

        ui->graphicsView->setScene(scene);

        Thank you. Works as expected.

        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