Results 1 to 12 of 12

Thread: QGraphicsView

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView

    Hello,

    I am trying to output some graphical items (circles, rectangles, etc...) on a QGraphicsView object. For that purpose, I created a QGraphicsView object in the main window form. How do I get to make it output the graphical content of the QGraphicsScene?

    Thank you very much!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

  3. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView

    Thank you. However, I cant still output graphics through the QGraphicsView object I previously created in my main form. When I use the following code, the QGraphicsScene is visualized through a window that is created out of the main form (and not in the Graphics window in my main form):

    QGraphicsScene scene;
    QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));

    QGraphicsItem *item = scene.itemAt(50, 50);

    QGraphicsView view(&scene);

    view.show();


    Thank you very much!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView

    I'm not sure I understand you.

    Did you use designer and put a graphics view on your widget/dialog/mainwindow? And do you want to show the scene in that graphics view?
    If so, you need to access it via ui->nameOfTheViewObject->setScene(&scene);

    If not, please post your complete code.

  5. #5
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView

    Yes, I did create a graphics view on my mainwindow and tried to show the scene there. I used the following code in my main.cpp file :


    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include <QGraphicsScene>
    #include <QGraphicsView>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));

    QGraphicsItem *item = scene.itemAt(50, 50);

    QGraphicsView graphicsView(&scene);


    graphicsView.show();

    MainWindow w;
    w.show();
    return a.exec();
    }

    I cant access the object, a graphicsView window, in the main.cpp file. What should I do?

    Thank you very much

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView

    Replace

    Qt Code:
    1. graphicsView.show();
    2.  
    3. MainWindow w;
    To copy to clipboard, switch view to plain text mode 

    with

    Qt Code:
    1. MainWindow w;
    2.  
    3. w.setCentralWidget(&graphicsView);
    To copy to clipboard, switch view to plain text mode 

    You created two distinct widgets and showed them.
    You need to add the graphics view to the main window.

  7. #7
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView

    Thank you. However, that code uses my whole mainwindow as a graphical output, whereas I am trying to output graphics through a graphics window of a certain size placed within my mainwindow.

    Thank you very much

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView

    Use a layout

  9. The following user says thank you to tbscope for this useful post:

    Maluko_Da_Tola (18th July 2010)

  10. #9
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView

    I've just created a vertical layout and put my graphicsView object inside it. How do I get it to display the graphics? I've tried the following code, but it does not work:

    MainWindow w;

    w.setLayout(&graphicsView);

    Thank you

  11. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView

    You can create your layout with the QWidget as parent (and the widgets added to layout will be re-parented to the MainWindow/Widget), like this:
    Qt Code:
    1. MainWindow w;
    2. QLabel label = new QLabel; //this will have w as a parent after it's added to the v_layout
    3. //add more widgets to layout
    4. //...
    5. QVBoxLayout *v_layout = new QVBoxLayout(&w);
    6. v_layout->addWidget(label);
    7. //...
    8. w.show(); //you need to show just w
    To copy to clipboard, switch view to plain text mode 

    LE: the code above will work if you use a QWidget as the window (sorry)

    if you use the QMainWindow or derived from it, you will need to parent the layout to a "container" QWidget and then add that QWidget as a centralWidget of your QMainWindow object, something like:
    Qt Code:
    1. MainWindow w;
    2. QWidget *container = new QWidget;
    3. QLabel label = new QLabel; //this will have w as a parent after it's added to the v_layout
    4. //add more widgets to layout
    5. //...
    6. QVBoxLayout *v_layout = new QVBoxLayout(container);
    7. v_layout->addWidget(label);
    8. //...
    9. w.setCentralWidget(container);
    10. w.show(); //you need to show just w
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zlatomir; 18th July 2010 at 23:25.

  12. The following user says thank you to Zlatomir for this useful post:

    Maluko_Da_Tola (18th July 2010)

  13. #11
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView

    hum... i'm not too sure about using Layouts... Could you indicate me some readings please?

    Thank you

  14. #12
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView

    Small tutorial, it has some links to others..., class reference
    But the best resource to understand quickly, is read a book (i'm reading "Foundations of Qt Development" by Johan Thelin, and it's an excellent book)

Similar Threads

  1. QGraphicsView
    By Yayati.Ekbote in forum Newbie
    Replies: 2
    Last Post: 4th March 2010, 15:10
  2. QGraphicsView
    By hgedek in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2007, 09:16
  3. QGraphicsView
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2007, 08:00
  4. help with QGraphicsView
    By Erlendhg in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2007, 20:26
  5. Regarding QGraphicsView
    By kiranraj in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2006, 04:59

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.