Results 1 to 16 of 16

Thread: QScrollarea events transferring to scrollbar

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Question QScrollarea events transferring to scrollbar

    hi
    i have subclassed QScrollArea to receive mouse move events .
    now when i move the mouse the scrollbars should be moved accordingly(up and down movements for vertical scrollbar and left and right movements for horizontal scrollbar).

    how can i transfer the mouse move event of scrollarea to scrollbar

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    Quote Originally Posted by babu198649 View Post
    i have subclassed QScrollArea to receive mouse move events .
    now when i move the mouse the scrollbars should be moved accordingly(up and down movements for vertical scrollbar and left and right movements for horizontal scrollbar).

    how can i transfer the mouse move event of scrollarea to scrollbar
    Why not use QScrollBar::setValue() to set the value of the scrollbar?

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    yes i have tried it but it makes exection slower.

    i have tried like this

    Qt Code:
    1. void iv_scrollarea::mouseMoveEvent ( QMouseEvent * event )
    2. {
    3. if(this->underMouse())
    4. {
    5. if(old_position.x() > event->pos().x())//old position is qpoint it remembers last position
    6. this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() + 1);
    7.  
    8. if(old_position.x() < event->pos().x())
    9. this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - 1);
    10.  
    11. if(old_position.y() > event->pos().y())
    12. this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() + 1);
    13.  
    14. if(old_position.y() < event->pos().y())
    15. this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - 1);
    16. }
    17. old_position = event->pos();
    18. }
    To copy to clipboard, switch view to plain text mode 

    the above code takes long movement of the mouse to move the label inside the QScrollArea. because at every pixel moved the movement of label does not happen

    and when i set values with higher than setValue(1) (i.e. this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - 5))

    the flickering occurs


    when i use the move() function of label inside scrollarea mouse move event it works fine but scrollbars are not updated

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    Ok, what effect are you trying to achieve anyway? Why do you add "1" instead of computing the actual movement?

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    Ok, what effect are you trying to achieve anyway?
    i want to move the qlabel inside the qscrollarea using mouse cursor (i.e. click and move the label) simultaneously the scrollbars should get updated.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    Doesn't something like this work?
    Qt Code:
    1. #include <QScrollArea>
    2. #include <QLabel>
    3. #include <QApplication>
    4.  
    5. class ScrollArea : public QScrollArea {
    6. public:
    7. ScrollArea(const QString &path) : QScrollArea(0){
    8. QLabel *l = new QLabel(this);
    9. setWidget(l);
    10. setWidgetResizable(true);
    11. l->setPixmap(QPixmap(path));
    12. }
    13. };
    14.  
    15.  
    16. int main(int argc, char **argv){
    17. QApplication app(argc, argv);
    18. ScrollArea sa(argv[1]);
    19. sa.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    The scrolling works for me out of the box.

  7. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    the problem is not with scrolling
    when i move the qlabel with the mouse(clicked and move ) the label moves a distance with respect to the distance moved by the mouse .now the scrollbar should update this movement of the label.


    QLabel *l = new QLabel(this);
    setWidget(l);

    is this keyword nessary

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollarea events transferring to scrollbar

    Why don't you use built-in components, QMdiArea or QGraphicsView (depending on your needs) as already suggested in another thread?
    J-P Nurmi

  9. #9
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    hi
    i have found in the documentation that the QGraphicsView is for maintaining large number of items . but i have only one label so i thought it will not suit.

    and QMdiArea class documentation says

    The QMdiArea widget provides an area in which MDI windows are displayed.

    but i dont have more than one window and also i need time to know what MDI windows are . so i thought that i can do these things with qscrollarea itself.

    do u think the problem can not be solved with QScrollarea

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    You should really use QMdiArea or QGraphicsView as suggested, but if you insist on using the scroll area directly then this works for me:
    Qt Code:
    1. #include <QScrollArea>
    2. #include <QLabel>
    3. #include <QApplication>
    4. #include <QMouseEvent>
    5. #include <QScrollBar>
    6.  
    7.  
    8. class ScrollArea : public QScrollArea {
    9. public:
    10. ScrollArea(const QString &path) : QScrollArea(0){
    11. l = new QLabel(this);
    12. setWidget(l);
    13. setWidgetResizable(true);
    14. l->setPixmap(QPixmap(path));
    15. l->installEventFilter(this);
    16. }
    17. bool eventFilter(QObject *o, QEvent *e){
    18. if(o!=l)
    19. return QScrollArea::eventFilter(o,e);
    20. if(e->type()==QEvent::MouseButtonPress){
    21. m_prev = ((QMouseEvent*)e)->pos();
    22. return true;
    23. }
    24. if(e->type()==QEvent::MouseMove){
    25. QPoint pt = ((QMouseEvent*)e)->pos();
    26. QPoint diff = pt-m_prev;
    27. m_prev = pt;
    28. horizontalScrollBar()->setValue(horizontalScrollBar()->value()+diff.x());
    29. verticalScrollBar()->setValue(verticalScrollBar()->value()+diff.y());
    30. return true;
    31. }
    32. return QScrollArea::eventFilter(o,e);
    33. }
    34. protected:
    35. QLabel *l;
    36. QPoint m_prev;
    37. };
    38.  
    39.  
    40. int main(int argc, char **argv){
    41. QApplication app(argc, argv);
    42. ScrollArea sa(argv[1]);
    43. sa.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to wysota for this useful post:

    babu198649 (27th November 2007)

  12. #11
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    Thank u very much wysota
    it really works great .
    thanks for pain taking job of coding entirely in a short perid for me

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    You would have saved me the pain if you used the right component for the job in the first place.

  14. #13
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    yes
    but i followed the image viewer example

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollarea events transferring to scrollbar

    I advise you to follow JPN's hints next time. Your use case seemed a perfect candidate for QGraphicsView, especially if you intend to do something more with it (like introduce zooming).

  16. #15
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QScrollarea events transferring to scrollbar

    sure, thank u

  17. #16
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollarea events transferring to scrollbar

    Quote Originally Posted by wysota View Post
    You should really use QMdiArea or QGraphicsView as suggested, but if you insist on using the scroll area directly then this works for me:
    Qt Code:
    1. #include <QScrollArea>
    2. #include <QLabel>
    3. #include <QApplication>
    4. #include <QMouseEvent>
    5. #include <QScrollBar>
    6.  
    7.  
    8. class ScrollArea : public QScrollArea {
    9. public:
    10. ScrollArea(const QString &path) : QScrollArea(0){
    11. l = new QLabel(this);
    12. setWidget(l);
    13. setWidgetResizable(true);
    14. l->setPixmap(QPixmap(path));
    15. l->installEventFilter(this);
    16. }
    17. bool eventFilter(QObject *o, QEvent *e){
    18. if(o!=l)
    19. return QScrollArea::eventFilter(o,e);
    20. if(e->type()==QEvent::MouseButtonPress){
    21. m_prev = ((QMouseEvent*)e)->pos();
    22. return true;
    23. }
    24. if(e->type()==QEvent::MouseMove){
    25. QPoint pt = ((QMouseEvent*)e)->pos();
    26. QPoint diff = pt-m_prev;
    27. m_prev = pt;
    28. horizontalScrollBar()->setValue(horizontalScrollBar()->value()+diff.x());
    29. verticalScrollBar()->setValue(verticalScrollBar()->value()+diff.y());
    30. return true;
    31. }
    32. return QScrollArea::eventFilter(o,e);
    33. }
    34. protected:
    35. QLabel *l;
    36. QPoint m_prev;
    37. };
    38.  
    39.  
    40. int main(int argc, char **argv){
    41. QApplication app(argc, argv);
    42. ScrollArea sa(argv[1]);
    43. sa.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    It makes something wrong when you drag it more than one pixel in one direction. Is accumulative. I got one solution for it:
    Qt Code:
    1. #include <QScrollArea>
    2. #include <QLabel>
    3. #include <QApplication>
    4. #include <QMouseEvent>
    5. #include <QScrollBar>
    6. #include<QDebug>
    7.  
    8. class ScrollArea : public QScrollArea {
    9. public:
    10. ScrollArea() : QScrollArea()
    11. {
    12. this->n=3;
    13. this->prev_diff.setX(0);
    14. this->prev_diff.setY(0);
    15. l = new QLabel(this);
    16. QImage imagen("C:/donostia.jpg");
    17. l->setPixmap(QPixmap::fromImage(imagen));
    18. setWidget(l);
    19. setWidgetResizable(true);
    20. //l->setPixmap(QPixmap(path));
    21. l->installEventFilter(this);
    22. }
    23. bool eventFilter(QObject *o, QEvent *e)
    24. {
    25. if(o!=l)
    26. return QScrollArea::eventFilter(o,e);
    27. if(e->type()==QEvent::MouseButtonPress)
    28. {
    29. m_prev = ((QMouseEvent*)e)->pos();
    30. this->prev_diff.setX(0);
    31. this->prev_diff.setY(0);
    32. return true;
    33. }
    34. if(e->type()==QEvent::MouseMove)
    35. {
    36. QPoint pt = ((QMouseEvent*)e)->pos();
    37. this->diff = pt-m_prev-prev_diff*n;
    38. m_prev = pt;
    39. horizontalScrollBar()->setValue(horizontalScrollBar()->value()+diff.x()*n);
    40. verticalScrollBar()->setValue(verticalScrollBar()->value()+diff.y()*n);
    41. qDebug()<<"("+QString::number(diff.x())+","+QString::number(diff.y())+")";
    42. this->prev_diff=diff;
    43. return true;
    44. }
    45. return QScrollArea::eventFilter(o,e);
    46. }
    47. protected:
    48. QLabel *l;
    49. QPoint m_prev;
    50. QPoint diff;
    51. QPoint prev_diff;
    52. int n;
    53. };
    54.  
    55.  
    56. int main(int argc, char **argv)
    57. {
    58. QApplication app(argc, argv);
    59. ScrollArea sa;
    60. sa.show();
    61. return app.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Sirganor; 6th September 2013 at 23:36. Reason: updated contents

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.