Results 1 to 13 of 13

Thread: How to use QGraphicView?

  1. #1
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Exclamation How to use QGraphicView?

    Hi All. I am new to qt programming. Could someone tell how to use QGraphicView or sample programs...?

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    Could someone tell how to use QGraphicView or sample programs...?
    It is QGraphicsView, not QGraphicView.

    Graphichs View Example is the best place to start.
    Last edited by viulskiez; 26th April 2012 at 08:35. Reason: reformatted to look better
    ~ We are nothing in this universe ~

  3. #3
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: How to use QGraphicView?

    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..?
    Last edited by karthic; 26th April 2012 at 10:50.

  4. #4
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    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.
    ~ We are nothing in this universe ~

  5. #5
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: How to use QGraphicView?

    #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.

  6. #6
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    Ahh, of course! Because you are installing event filter to QGraphicsScene instance.
    You have to filter QGraphicsView::viewportthis is the example:
    Qt Code:
    1. // your code:
    2. // m_graphicsScene->installEventFilter ( this );
    3.  
    4. QWidget* viewport = ui->myGraphicsView->viewport();
    5. // viewport->setMouseTracking(true); uncomment if you want all mouse events to be tracked
    6. viewport->installEventFilter(this); // this is what you
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

  7. #7
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: How to use QGraphicView?

    Same output is coming. No change.

  8. #8
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    Same output is coming. No change.
    what do you mean?
    ~ We are nothing in this universe ~

  9. #9
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: How to use QGraphicView?

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

  10. #10
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    Please, check your code again.
    Qt Code:
    1. if( (watched == m_graphicsScene) ) { // who is being watched? grapichs scene or graphics view?
    2. qDebug()<<e->type();
    3. return true;
    4. }
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

  11. #11
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: How to use QGraphicView?

    m_graphicsScene = new QGraphicsScene(ui->myGraphicsView);

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

  12. #12
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: How to use QGraphicView?

    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.
    ~ We are nothing in this universe ~

  13. The following user says thank you to viulskiez for this useful post:

    karthic (27th April 2012)

  14. #13
    Join Date
    Apr 2012
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    1

    Thumbs up Re: How to use QGraphicView?

    Thanks for your response. Now its working after adding this line to my code.
    ui->myGraphicsView->setScene(m_graphicsScene);

Similar Threads

  1. Replies: 1
    Last Post: 2nd December 2010, 15:00
  2. Auto scroll in QGraphicview/QGraphicsecene?
    By jujose in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2010, 11:00
  3. Replies: 2
    Last Post: 6th April 2010, 15:00
  4. my QGraphicView problem
    By irmakci in forum Qt Programming
    Replies: 3
    Last Post: 19th July 2008, 18:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.