Results 1 to 4 of 4

Thread: Adding Scene/Item to a graphicView which was created using the design mode

  1. #1
    Join Date
    Jun 2015
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Adding Scene/Item to a graphicView which was created using the design mode

    Hi everyone. I am a newbie in Qt and I have a question.

    So I created a new project of QMainWindows. Then I go directly to Forms -> mainwindow.ui and drag Graphics View into the main window. The next thing that I want to do is to add a rectangle inside the graphicView (name of the QGraphicView object) and make it move around just as I want it to be. Any suggestions on how to do that?

    I want to make the animation inside the MainWindow, not in another window. I have tried creating scene and trying to put in by code in the mainwindow.cpp (as below), but it doesn't work. If i was able to put the rectangle into the scene/view, i can move to animating the rectangle. But i'm still stuck here.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. //create a scene
    8. QGraphicsScene * scene = new QGraphicsScene();
    9.  
    10. //create the rectangle
    11. rect->setRect(0,0,50,50);
    12.  
    13. //add the item into the scene
    14. scene->addItem(rect);
    15.  
    16. //use the view from the mainwindow
    17. graphicsView = new QGraphicsView(scene);
    18. graphicsView.show();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    I have randomly tried including "ui_mainwindow.h" but it still doesn't work. Haha

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding Scene/Item to a graphicView which was created using the design mode

    Then I go directly to Forms -> mainwindow.ui and drag Graphics View into the main window.
    I presume you mean you did this in Qt Designer. What name did you give to this QGraphicsView instance? (Look at the properties in Qt Designer if you don't remember).

    And if you did that, why are you creating another QGraphicsView instance inside the MainWindow constructor when you already have one created during the call to ui->setupUi() (because you dragged one into the main window in Qt Designer)?

    Use that one:
    Qt Code:
    1. ui->whateverTheGraphicsViewIsNamed->setScene( scene );
    To copy to clipboard, switch view to plain text mode 
    No need to call show(), because it will be shown when the MainWindow is shown.

    I don't create MainWindow UIs in Qt Designer. Dialogs and other QWidget-based forms, yes, but not main windows. So I don't know if dragging something into a main window form makes that the main window's "central widget" or not. If the graphics view does not fill the entire main window when the app comes up, then it hasn't been set as the central widget. You should add a call to
    Qt Code:
    1. setCentralWidget( ui->whateverTheGraphicsViewIsNamed );
    To copy to clipboard, switch view to plain text mode 
    at the end of the MainWindow constructor.
    Last edited by d_stranz; 27th June 2015 at 22:37.

  3. The following user says thank you to d_stranz for this useful post:

    tuerahyosua (28th June 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding Scene/Item to a graphicView which was created using the design mode

    Quote Originally Posted by d_stranz View Post
    I don't create MainWindow UIs in Qt Designer. Dialogs and other QWidget-based forms, yes, but not main windows. So I don't know if dragging something into a main window form makes that the main window's "central widget" or not. If the graphics view does not fill the entire main window when the app comes up, then it hasn't been set as the central widget. You should add a call to
    Qt Code:
    1. setCentralWidget( ui->whateverTheGraphicsViewIsNamed );
    To copy to clipboard, switch view to plain text mode 
    at the end of the MainWindow constructor.
    The generated code takes care of that. The GraphicsView will be a child of the central widget.

    Cheers,
    _

  5. The following 2 users say thank you to anda_skoa for this useful post:

    d_stranz (28th June 2015), tuerahyosua (28th June 2015)

  6. #4
    Join Date
    Jun 2015
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding Scene/Item to a graphicView which was created using the design mode

    Problem Solved! Many thanks

Similar Threads

  1. Data Visualization GraphicsView/Scene/Item Design
    By vgrunert in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2015, 17:31
  2. Replies: 4
    Last Post: 8th April 2013, 05:19
  3. Replies: 0
    Last Post: 28th October 2010, 14:24
  4. Replies: 5
    Last Post: 10th August 2009, 11:50
  5. Adding item to scene
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 18:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.