PDA

View Full Version : QGraphicsView



Maluko_Da_Tola
18th July 2010, 21:01
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!

tbscope
18th July 2010, 21:28
http://doc.qt.nokia.com/4.6/graphicsview.html

Maluko_Da_Tola
18th July 2010, 22:07
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!

tbscope
18th July 2010, 22:21
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.

Maluko_Da_Tola
18th July 2010, 22:33
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

tbscope
18th July 2010, 22:38
Replace


graphicsView.show();

MainWindow w;

with


MainWindow w;

w.setCentralWidget(&graphicsView);

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

Maluko_Da_Tola
18th July 2010, 22:52
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

tbscope
18th July 2010, 22:54
Use a layout

Maluko_Da_Tola
18th July 2010, 23:08
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

Zlatomir
18th July 2010, 23:12
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:


MainWindow w;
QLabel label = new QLabel; //this will have w as a parent after it's added to the v_layout
//add more widgets to layout
//...
QVBoxLayout *v_layout = new QVBoxLayout(&w);
v_layout->addWidget(label);
//...
w.show(); //you need to show just w


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:


MainWindow w;
QWidget *container = new QWidget;
QLabel label = new QLabel; //this will have w as a parent after it's added to the v_layout
//add more widgets to layout
//...
QVBoxLayout *v_layout = new QVBoxLayout(container);
v_layout->addWidget(label);
//...
w.setCentralWidget(container);
w.show(); //you need to show just w

Maluko_Da_Tola
18th July 2010, 23:59
hum... i'm not too sure about using Layouts... Could you indicate me some readings please?

Thank you

Zlatomir
19th July 2010, 08:15
Small tutorial, it has some links to others... (http://doc.qt.nokia.com/4.6/tutorials-widgets-windowlayout.html), class reference (http://doc.trolltech.com/4.6/qlayout.html)
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)