×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    scrolling in QGraphicsView

    Hi,

    I am trying a simple test to draw a line in a qgraphicsscene which goes beyond the co-ordinates of the display.
    For example Im trying to draw a line with co-ordinates ( 20,40,1000,40 ) on a maemo device(N900) that has a display of 800x480.

    Im setting the scene in a qgraphicsview and I do see the scrolls appear but Im not able to scroll it when I try to.
    Code:
    int main(int argc, char* argv[])
    {
     QApplication app(argc, argv);
      
    
     QGraphicsScene scene;
     scene.addLine(20,40,1000,40, QPen(Qt::yellow, Qt::SolidLine));
    
     QGraphicsView view(&scene);
     view.setScene(&scene);
     view.setSceneRect(0,0,1600,1400);
     
     view.setBackgroundBrush(QBrush(Qt::black));
     view.setTransformationAnchor(QGraphicsView::NoAnchor);
     view.show();
    
     app.exec()
    }
    does mousepressevent need to be implemented ? I thought qgrahicsview updateScene() slot got automatically connected to the qgraphicsscene changed() signal on doing setScene() and the scrolling would work out of box.

    is there something else that needs to be done to make my view scroll by dragging the view using my finger so I can see the entire line that is drawn ?


    thanks for the time.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: scrolling in QGraphicsView

    what Im trying to do is create a drawing( a simple line graph) that scrolls horizontally since it can grow larger than the display size.

    Im confused if I really need to use QGraphicsView for this or can I just draw it on a QWidget using the paint(). Not really clear about this.
    Also how can I make it scroll horizontally if I were to use either ?

    thanks

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: scrolling in QGraphicsView

    Quote Originally Posted by rkurvakat View Post
    what Im trying to do is create a drawing( a simple line graph) that scrolls horizontally since it can grow larger than the display size.
    Im confused if I really need to use QGraphicsView for this or can I just draw it on a QWidget using the paint(). Not really clear about this.
    Also how can I make it scroll horizontally if I were to use either ?
    thanks
    Ive answered the above query myself with the below mentioned solutions for the benefit of others and also for the gurus to comment.
    Now if I draw something more than the viewing area I get a scroll.

    What I dont understand is , which one do I use, QGraphicsView or QWidget:: paintEvent() for drawing simple 2D graphics, for example a simple line graph with x and y axis.
    What is the advantage/disadvantage of both ?

    QWIDGET SOLUTION
    ================
    Code:
    class MyWidget : public QWidget
    {
        public:
            MyWidget() { }
      protected:
        void paintEvent(QPaintEvent *event)
        {
              QPen pen(Qt::yellow,Qt::SolidLine);
    
                QPainter painter(this);
    
                  painter.setPen(pen);
                    //painter.drawLine(20, 40, 1250, 40);
                    painter.drawRect(20, 40, 1250, 580);
        }
    
    };
    
    int main(int argc, char* argv[])
    {
        QMainWindow mw;
        QScrollArea s;
        MyWidget w;
        s.setWidget(&w);
    
    // NONE OF THESE LINES MADE IT WORK. WHAT COULD BE THE REASON ?
        //w.setBaseSize(1000,400);
        //w.setMaximumSize(1000,400);
        //w.setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
        //s.setWidgetResizable(true);
    
        mw.setCentralWidget(&s);
    
    // THIS LINE DID THE TRICK, why not the above setBaseSize or MaximumSize ?
        w.resize(1000,400);
    
        mw.show();
      
        app.exec();
    }
    QGRAPHICSVIEW/SCENE SOLUTION
    ============================

    Code:
    int main(int argc, char*argv[])
    {
     QGraphicsScene scene;
     scene.addLine(50,40,1750,40, QPen(Qt::yellow, Qt::SolidLine));
    
     QGraphicsView view(&scene);
     view.setScene(&scene);
     view.setBackgroundBrush(QBrush(Qt::black));
     view.setInteractive(true);
     view.setSceneRect(0,0,2000,400);
     view.setDragMode(QGraphicsView::ScrollHandDrag);
     view.show();
    
     app.exec();
    }

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: scrolling in QGraphicsView

    Quote Originally Posted by rkurvakat View Post
    What I dont understand is , which one do I use, QGraphicsView or QWidget:: paintEvent() for drawing simple 2D graphics, for example a simple line graph with x and y axis.
    What is the advantage/disadvantage of both ?
    QGraphicsView of course. Disadvantage of manual implementation of paint is obvious, it's heavy, difficult to implement and maintain.

    You should set sizeHint to widget to set specific size.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: scrolling in QGraphicsView

    Quote Originally Posted by divanov View Post
    QGraphicsView of course. Disadvantage of manual implementation of paint is obvious, it's heavy, difficult to implement and maintain.

    You should set sizeHint to widget to set specific size.
    sorry Daniil, it doesnt look that obvious to me :-( since Ive just begun my programming in graphics.
    Ive spent past few days reading a lot about this but nothing seemed good enough for a start to get a hang of this.
    could you direct me to some simple example or illustrate the advantage of QGraphicsView over the Qwidget for a simple 2D graphic like a line graph(x and y axis), what do you mean by "manual implementation of paint", heavy etc, with graphicsview I still need to do almost the same looks to me.

    thanks for the time.

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: scrolling in QGraphicsView

    Notice that code you used in QWidget is larger comparing to the same code QGraphicsView, and you've used only one graphical primitive. Then think what will happen if you will have hundreds of graphical primitives.

Similar Threads

  1. problem with qt4.6 QwebView kinetic scrolling
    By mismael in forum Nokia N9
    Replies: 13
    Last Post: 2010-05-17, 08:32
  2. Kinetic Scrolling problems with CustomItem
    By SOLIPSIST in forum Mobile Java General
    Replies: 0
    Last Post: 2010-02-16, 21:05
  3. How to implement kinetic scrolling?
    By svdwal in forum Symbian User Interface
    Replies: 8
    Last Post: 2010-01-28, 14:36
  4. Qt 4.6 QGraphicsView + Finger scroll + Mouse press
    By kuba.janda in forum Nokia N9
    Replies: 7
    Last Post: 2009-12-07, 13:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×