fs_tigre
27th April 2012, 14: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.
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.