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. How to place a GraphicsView in a layout object
Forum Updated to NodeBB v4.3 + New Features

How to place a GraphicsView in a layout object

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k 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.
  • W Offline
    W Offline
    webguy85
    wrote on last edited by
    #1

    I need to create a scene and place it in a GraphicsView. Once I finish the scene and place it in the GraphicsView, how do I place the GraphicsView inside a layout object? My layout object is called vlayout and is an instance of QVBoxLayout. This is what I have so far (mostly from online examples):

    @
    widget = new QWidget();
    vlayout = new QVBoxLayout(widget);
    QGraphicsScene scene;
    scene.addText("Hello, world!");
    QGraphicsView view(&scene);
    view.show();

    //Here I want to place the view inside a layout object called vlayout
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on last edited by
      #2

      @ vlayout->addWidget(view);@ Have a look at the "documentation for QVBoxLayout":http://developer.qt.nokia.com/doc/qt-4.8/QVBoxLayout.html for more examples.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        webguy85
        wrote on last edited by
        #3

        When I do that, I get an error:

        'QBoxLayout::addWidget' : cannot convert parameter 1 from 'QGraphicsView' to 'QWidget *'

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          You have to pass an address, so:

          vlayout->addWidget(&view); // & = reference as address

          1 Reply Last reply
          0
          • W Offline
            W Offline
            webguy85
            wrote on last edited by
            #5

            I see. Im new to C++ so I am just getting used to pointers and addresses. Thanks.

            I dont see the text though. Any idea why?

            1 Reply Last reply
            0
            • W Offline
              W Offline
              webguy85
              wrote on last edited by
              #6

              Syntactically, everything is fine, but I can't get my image to show up. This is what I have:

              @
              QGraphicsScene scene;
              QGraphicsView view(&scene);
              QPixmap pixmap(":/images/icons/dsp.gif");
              QGraphicsPixmapItem* dsp = scene.addPixmap(pixmap);
              view.show();
              vlayout->addWidget(&view);
              vlayout->addWidget(new QPushButton("some button here"));
              @

              The QPushButton shows up fine in the layout, but not the view. Any idea what I am doing wrong?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris H
                wrote on last edited by
                #7

                Assuming this is in a function, the QGraphicsView is a local variable: when it goes out of scope it's gone. You should dynamically allocate it (this seems to be "the Qt way") or make it a member variable of your class.

                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