PDA

View Full Version : QGraphics View



Vivek1982
17th December 2013, 06:55
Hi to all..I'm creating a application,object QMainWindow. I have created application.in UI form I have dragged QGraphicsView. I have created Graphic scene also. I'm posting the code.i'm able to get scene but it by deafult coming at topleft corner. other than coming inside QGraphicsView.suggest me,where I might have gone wrong.


#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}




#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QBrush>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
};




#include "mainwindow.h"
#include "ui_mainwindow.h"

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

QGraphicsView *view = new QGraphicsView(this);
QGraphicsScene *scene=new QGraphicsScene();
scene->setBackgroundBrush(Qt::red);
view->setScene(scene);
}

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

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}




I'm insering the image also.9859

wysota
17th December 2013, 07:42
You didn't put the view in your layout so it pops up at (0,0) coordinates of its parent. Usually you'd create the view in the UI file and attach the scene to it in CPP code.

Vivek1982
17th December 2013, 08:52
ui->setupUi(this);

view=new QGraphicsView(this);
view=ui->graphicsView;

scene=new QGraphicsScene(view);
view->setScene(scene);
view->setFixedSize(500,500);

QPixmap pix("/:pin30_red.png");
scene->addPixmap(pix);
ui->graphicsView->setScene(scene);


I tried it fix it. Still same image is coming

anda_skoa
17th December 2013, 09:05
Remove the line where you create the QGraphicsView

And make sure your Designer UI is properly layouted and remove the setFixedSize() call

Cheers,
_

Vivek1982
17th December 2013, 09:48
Thanks for the reply. I tried as per your suggestion but still same image exists in the output.im posting full code in zip format.plz look into it.I request ammend if its wrong.


I'm finding difficult to resolve this So I have posted full code in zip format.

aamer4yu
17th December 2013, 10:18
You had incorrectly used the qrc image path...
Also the view is unnecessary as you have ui->graphicsView.

Use the code below in ctor and delete uses of view variable -


{
ui->setupUi(this);

scene=new QGraphicsScene(ui->graphicsView);
QPixmap pix(":/pin30_red.png");
scene->addPixmap(pix);
ui->graphicsView->setScene(scene);

}

Vivek1982
18th December 2013, 05:33
thanks alot for reply. I got the output.. Now working on to move the pixmap on known co-ordinates of graphics View,by using Itemat or pos methods.(GraphicsPixmapItem) let me check.. thanks alot

Vivek1982
19th December 2013, 11:22
hi.. Im trying to move the image (pixmap loaded in graphisscene). To do this I have defined size of Graphicsscene intially.then I'm adding the pixmap via scene.addPixmap(image1). To get the pointer i'm calling graphicsItem. QGraphicsItem item=scene.addpixmap(image1).Then atlast Im setPixmap on the GraphicsView.In this correct? plz suggest. Pixmap will be moving on the scene on Qtimer by setpixmap on scene.

anda_skoa
19th December 2013, 15:28
You don't have to call anything on the view. Once you've added the pixmap by creating a pixmap item (which you do when you call scene.addPixmap()) then it will become visible in all views working with that scene.

If you want to move the pixmap, you set a new position on its graphicsitem

Cheers,
_

Vivek1982
20th December 2013, 04:52
I did the same, I have added QGraphicsItem also tried QGraphicsPixmap also to move. While moving the images(setPos). I'm getting twice images and If I increase the pos. Images are moving far on the view rather than same created image should move.


scene=new QGraphicsScene(0,0,500,300);
scene=new QGraphicsScene(ui->graphicsView);
QPixmap pix(":/pin30_red.png");
scene->addPixmap(pix);
QGraphicsItem *pix1=scene->addPixmap(pix);
//pix1->setPixmap(pix);
pix1->setPos(350,100);
ui->graphicsView->setScene(scene);

wysota
20th December 2013, 06:49
You are calling addPixmap twice so you get two items. Seems pretty obvious...

Vivek1982
20th December 2013, 08:42
Ya.. but If i try to just give QGraphicsItem *pix1= pix; pass the pointer. Im getting "error: cannot convert 'QPixmap' to 'QGraphicsItem*' in initialization" or if i try to comment on scene.addPixmap(pix). by default image will appear on horizontal bottom left.whether,I need to type cast in QGraphicsItem line?

anda_skoa
20th December 2013, 10:05
Ya.. but If i try to just give QGraphicsItem *pix1= pix; pass the pointer. Im getting "error: cannot convert 'QPixmap' to 'QGraphicsItem*' in initialization"

Well, if you look at the class declaration of QPixmap you will see that it does not inherit from QGraphicsItem.

But the QGraphicsPixmapItem that you get from QGraphicsScene::addPixmap() does.

Btw, your code snippet also creates an additional QGraphicsScene and then throws loses the pointer to it.

Cheers,
_

Vivek1982
20th December 2013, 11:26
Finally I got it.. The error was setting of images on the co-ordinates and in UI form also..

Thanks alot for all.. But this forum has helped alot and thought me many things about Qt programming.