PDA

View Full Version : Understanding QGraphics



fs_tigre
27th April 2012, 15:24
Hi,

I'm practicing with QGraphics and it works ok when placed in the main but onces I try
to use the same code in a main window when a button is clicked it doesn't work.

This works...


#include <QtGui>
#include <QtCore>

int main( int argc, char **argv )
{
QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
rectItem->setBrush( Qt::green );

QGraphicsView view;
view.setScene( &scene );
view.show();
}

This doesn't work...



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtCore>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_Draw_clicked()
{
QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
rectItem->setBrush( Qt::green );

QGraphicsView view;
view.setScene( &scene );
view.show();
}


Can someone tell me why the second example doen't work?

Thanks a lot.

d_stranz
27th April 2012, 22:13
void MainWindow::on_pushButton_Draw_clicked()
{
QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
rectItem->setBrush( Qt::green );

QGraphicsView view;
view.setScene( &scene );
view.show();
}


Here's a hint: What happens to your local variables "scene" and "view" after the slot exits? How is the lifetime of these variables different from those in your first example?

fs_tigre
27th April 2012, 23:45
I do understand variable scopes (sort of) I'm just not too familiar with classes, what goes in the .h file etc. I do understand the problem with the variable dieing after the function finish executing I just dont know how to break it down.

I tried adding the following two declaration to the .h file but nothing ... I know I know I'm an Id!@%^.

QGraphicsScene scene;
QGraphicsView view;

Thanks a lot for your help and sorry about the silly questions.

sedi
28th April 2012, 01:59
As Amleto told you -->here<-- (http://www.qtforum.org/article/37827/drawing-with-qgraphics.html), it would be sensible to provide all the .h file. You should make scene and view a member of MainWindow. If this->scene works, then you have made that correctly.

And you should decide where you ask your question. Doing that in different forums at around the same time (aka "cross posting") is frowned upon for a reason :-). I wait at least 24 hours for an answer before I try somewhere else.

fs_tigre
28th April 2012, 04:21
And you should decide where you ask your question. Doing that in different forums at around the same time (aka "cross posting") is frowned upon for a reason :-). I wait at least 24 hours for an answer before I try somewhere else.

I apologize, I just wanted different opinions to have a better understanding, thats all. From now on I will not do this, sorry again.

Added after 1 17 minutes:

This is how I did it...

.h file

private:
Ui::MainWindow *ui;
QGraphicsScene * sheet;
QGraphicsRectItem *rectangle;


cpp file:


void MainWindow::on_pushButton_Calculate_clicked()
{
calculate();
sheet = new QGraphicsScene (this);
ui->graphicsView->setScene(sheet);

QBrush redBrush(Qt::red);
QBrush blueBrush(Qt::blue);

QPen blackpen(Qt::black);
blackpen.setWidth(6);

rectangle = sheet->addRect(10,10,100,100, blackpen, redBrush);
}

Thank you all very much for your help.

amleto
28th April 2012, 13:15
memory leak (only cleaned up when your mainwondow object is disposed of).
http://www.qtforum.org/post/118523/drawing-with-qgraphics.html#post118523