PDA

View Full Version : How to use QGraphicView?



karthic
26th April 2012, 07:49
Hi All. I am new to qt programming. Could someone tell how to use QGraphicView or sample programs...?

viulskiez
26th April 2012, 08:35
Could someone tell how to use QGraphicView or sample programs...?
It is QGraphicsView, not QGraphicView.

Graphichs View Example (http://qt-project.org/doc/qt-4.8/examples-graphicsview.html) is the best place to start.

karthic
26th April 2012, 10:50
Thanks for correcting me. I have gone thru those examples but they are too complex and i cant learn basics thru it.

Added after 14 minutes:

Now i am trying to get mouse pointer position inside QGraphicsView using mouse press event. But this func gets called only in the form window and not when is click inside my QGraphicsView control. Any ideas..?

viulskiez
26th April 2012, 11:18
But this func gets called only in the form window and not when is click inside my QGraphicsView control
How you do that? Provide the source code to see what you've done.

karthic
26th April 2012, 14:04
#include <QMainWindow>
#include <QGraphicsView>

Header file

namespace Ui { class ApplicationWindow; }

class ApplicationWindow : public QMainWindow
{
Q_OBJECT

public:

ApplicationWindow( QWidget *parent = 0 );
~ApplicationWindow();
QGraphicsScene *m_graphicsScene;

public slots:
//

protected:
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);

protected slots:
bool eventFilter ( QObject *, QEvent * );

private:
Ui::ApplicationWindow* ui;

};


#include "application_window.h"
#include "ui_application_window.h"


#include <QFileDialog>
#include <QMessageBox>
#include <QMimeData>
#include <QDebug>


ApplicationWindow::ApplicationWindow( QWidget* inParent ) :
QMainWindow( inParent ),
ui( new Ui::ApplicationWindow )
{

ui->setupUi( this );
m_graphicsScene = new QGraphicsScene(ui->myGraphicsView);
m_graphicsScene->installEventFilter ( this );
this->installEventFilter ( this );
this->show();
}

ApplicationWindow::~ApplicationWindow()
{
}

bool ApplicationWindow::eventFilter( QObject * watched, QEvent * e )
{
qDebug()<<watched;
if( (watched == m_graphicsScene) ) {
qDebug()<<e->type();
return true;
}
}
return false;
}

The dDebug output is always showing as ApplicationWindow even if i clicked the mouse inside QGraphicsView control.

viulskiez
26th April 2012, 14:38
Ahh, of course! Because you are installing event filter to QGraphicsScene instance.
You have to filter QGraphicsView::viewport
this is the example:


// your code:
// m_graphicsScene->installEventFilter ( this );

QWidget* viewport = ui->myGraphicsView->viewport();
// viewport->setMouseTracking(true); uncomment if you want all mouse events to be tracked
viewport->installEventFilter(this); // this is what you

karthic
27th April 2012, 07:14
Same output is coming. No change.

viulskiez
27th April 2012, 07:24
Same output is coming. No change.
what do you mean?

karthic
27th April 2012, 07:33
The dDebug output is always showing as ApplicationWindow even if i clicked the mouse inside QGraphicsView control.

viulskiez
27th April 2012, 07:39
Please, check your code again.


if( (watched == m_graphicsScene) ) { // who is being watched? grapichs scene or graphics view?
qDebug()<<e->type();
return true;
}

karthic
27th April 2012, 07:42
m_graphicsScene = new QGraphicsScene(ui->myGraphicsView);

I have created QGraphicsScene object for ui->myGraphicsView control. Hope you have got an idea..!!

viulskiez
27th April 2012, 07:49
Please understand the concept of QGraphicScene and QGraphicsView if you are new to Qt. Read the documentation and understand how the architecture work. Here's the link: Qt Graphics View Framework (http://qt-project.org/doc/qt-4.8/graphicsview.html).

karthic
27th April 2012, 08:06
Thanks for your response. Now its working after adding this line to my code.
ui->myGraphicsView->setScene(m_graphicsScene);