Results 1 to 4 of 4

Thread: QPrintPreviewWidget mouse wheel events

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default QPrintPreviewWidget mouse wheel events

    G'day all,

    Perhaps I am suffering brain fade or caffeine withdrawal but I am sure someone can help.

    I have a QPrintPreviewWidget displaying a multi-page document. The mouse wheel scrolls up and down through the pages quite happily. I'd like to trap mouse wheel events when the Control key is pressed to zoom in and out.

    My first approach was to subclass QPrintPreviewWidget and override wheelEvent(). Unfortunately the new wheelEvent() does not get called unless the preview scroll area has moved all the way to the top (bottom) of the document. The vertical scroll bar is consuming the event before I can see it, and stop consuming it if it can no longer scroll. The result is the Ctrl-Wheel only zooms at top or bottom of the document.

    My second approach was to install an event filter hoping to see the wheel events before the scroll bars consumed them. This suffers the same problem.

    Both approaches shown below. Similar result Qt4.8.4 and 5.1.0 on Linux.

    How do I get these events before the scroll area/bars?

    Cheers,
    Chris

    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QPainter>
    4. #include <QPrinter>
    5. #include <QPrintPreviewWidget>
    6. #include <QVBoxLayout>
    7. #include <QWheelEvent>
    8. #include <QWidget>
    9. #include <cmath>
    10.  
    11. class PreviewWidget: public QPrintPreviewWidget {
    12. Q_OBJECT
    13. public:
    14. PreviewWidget(QWidget *parent = 0): QPrintPreviewWidget(parent) { }
    15.  
    16. protected:
    17. void wheelEvent(QWheelEvent *event) {
    18. if (event->orientation() == Qt::Vertical && (event->modifiers() & Qt::ControlModifier)) {
    19. double numDegrees = event->delta() / 8.0;
    20. double numSteps = numDegrees / 15.0;
    21. double factor = std::pow(1.125, numSteps);
    22. zoomIn(factor);
    23. event->accept();
    24. }
    25. event->ignore();
    26. }
    27. };
    28.  
    29. class Widget : public QWidget {
    30. Q_OBJECT
    31.  
    32. public:
    33. explicit Widget(QWidget *parent = 0): QWidget(parent) {
    34. m_printPreview = new PreviewWidget(this);
    35. QVBoxLayout *layout = new QVBoxLayout(this);
    36. layout->addWidget(m_printPreview);
    37. connect(m_printPreview, SIGNAL(paintRequested(QPrinter*)), SLOT(paintRequested(QPrinter*)));
    38. setLayout(layout);
    39. resize(400, 800);
    40. // m_printPreview->installEventFilter(this);
    41. }
    42.  
    43. public slots:
    44. void paintRequested(QPrinter *printer) {
    45. QPainter p(printer);
    46. QFont big("Arial", 200);
    47. p.setFont(big);
    48. for (int i = 0; i < 4; ++i) {
    49. p.drawText(300, 200, QString::number(i));
    50. printer->newPage();
    51. }
    52. }
    53.  
    54. protected:
    55. bool eventFilter(QObject *obj, QEvent *event) {
    56. if (obj == m_printPreview && event->type() == QEvent::Wheel) {
    57. QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
    58. if (wheelEvent->orientation() == Qt::Vertical && (wheelEvent->modifiers() & Qt::ControlModifier)) {
    59. double numDegrees = wheelEvent->delta() / 8.0;
    60. double numSteps = numDegrees / 15.0;
    61. double factor = std::pow(1.125, numSteps);
    62. m_printPreview->zoomIn(factor);
    63. // wheelEvent->accept();
    64. return true;
    65. }
    66. // wheelEvent->ignore();
    67. return false;
    68. }
    69. else {
    70. // standard event processing
    71. return QObject::eventFilter(obj, event);
    72. }
    73. }
    74.  
    75. private:
    76. PreviewWidget *m_printPreview;
    77. };
    78.  
    79.  
    80. int main(int argc, char *argv[]) {
    81. QApplication a(argc, argv);
    82. Widget w;
    83. w.show();
    84. return a.exec();
    85. }
    86. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPrintPreviewWidget mouse wheel events

    You can always install event filter on qApp and filter the event out only if m_printPreview is underMouse().

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrintPreviewWidget mouse wheel events

    It's a bit like using a sledgehammer to crack a walnut though

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrintPreviewWidget mouse wheel events

    Looks like it is the sledgehammer approach or nothing. The scroll bars belong to a private QGraphicsView and the events are not routed via QPrintPreviewWidget in any way I can see.

    On the plus side, I solved another problem by "accident" while looking at this one.

Similar Threads

  1. QCalendarWidget - how to stop wheel events processing?
    By googie in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2013, 22:06
  2. Replies: 0
    Last Post: 6th March 2012, 10:56
  3. Can usb mouse wheel work in Qt/E ?
    By earth in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 26th August 2011, 11:00
  4. Scrolling on Mouse Wheel
    By penny in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2011, 07:30
  5. combining wheel events
    By mcarter in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2010, 05:59

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.