Results 1 to 3 of 3

Thread: Mouse events with StackAll QStackedLayout

  1. #1
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question Mouse events with StackAll QStackedLayout

    Hi, all.

    In my video player I'm trying to have widget with controls on top of video widget. Using QStackedWidget or QStackedLayout with stackMode set to StackAll allowed me to place controls over video, but in this case i cannot handle mouse clicks in video widget. Here's simple test program to demonstrate my problem:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. QWidget mainWidget;
    7.  
    8. mainWidget.setMinimumSize(300, 400);
    9.  
    10. QStackedLayout* stackedLayout = new QStackedLayout();
    11. stackedLayout->setStackingMode(QStackedLayout::StackAll);
    12.  
    13. QWidget *videoWidget = new QWidget();
    14. videoWidget->setLayout(new QVBoxLayout());
    15. videoWidget->layout()->addWidget(new QPushButton("Video"));
    16. static_cast<QVBoxLayout *>(videoWidget->layout())->insertStretch(-1);
    17. stackedLayout->addWidget(videoWidget);
    18.  
    19. QWidget *controlWidget = new QWidget();
    20. controlWidget->setLayout(new QVBoxLayout());
    21. static_cast<QVBoxLayout *>(controlWidget->layout())->insertStretch(-1);
    22. controlWidget->layout()->addWidget(new QPushButton("Controls"));
    23. stackedLayout->addWidget(controlWidget);
    24.  
    25. mainWidget.setLayout(new QHBoxLayout());
    26. static_cast<QHBoxLayout *>(mainWidget.layout())->addLayout(stackedLayout);
    27. mainWidget.show();
    28.  
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    In this program it's impossible to click on "Video" button.

    Any suggestions?

    Thanks in advance.
    Oleg Shparber

  2. #2
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse events with StackAll QStackedLayout

    see answer of Thierry Bastian for https://bugreports.qt.nokia.com/browse/QTBUG-3868
    Only the current page is active. The button on the other page just appears there because your current page is not opaque. That doesn't mean that it is transparent to mouse events.
    So you can capture mouse events at mainWidget and send it to needed control (according with mouse position) - but this way assume that you have pointer to all needed control at all stack. The following code show example:
    Qt Code:
    1. #include <QtGui>
    2. class CMainWidget : public QWidget
    3. {
    4. QPushButton*m_video_but;
    5. public:
    6. CMainWidget(QWidget*parent=0):QWidget(parent)
    7. {
    8. setMouseTracking(true);
    9. QStackedLayout*stack = new QStackedLayout(this);
    10. stack->setStackingMode(QStackedLayout::StackAll);
    11. QWidget*video_widget = new QWidget;
    12. QVBoxLayout*v_layout = new QVBoxLayout(video_widget);
    13. m_video_but = new QPushButton("Video");
    14. v_layout->addWidget(m_video_but);
    15. v_layout->addStretch();
    16. stack->addWidget(video_widget);
    17. QWidget*control_widget = new QWidget;
    18. v_layout = new QVBoxLayout(control_widget);
    19. v_layout->addStretch();
    20. v_layout->addWidget(new QPushButton("Controls"));
    21. stack->addWidget(control_widget);
    22. stack->setCurrentWidget(m_control_widget);
    23. }
    24. private:
    25. void mouseEvent(QMouseEvent*ev)
    26. {
    27. if(m_video_but->rect().contains(ev->pos()))
    28. qApp->sendEvent(m_video_but,ev);
    29. }
    30. void mousePressEvent(QMouseEvent*ev) { mouseEvent(ev); }
    31. void mouseReleaseEvent(QMouseEvent*ev) { mouseEvent(ev); }
    32. void mouseDoubleClickEvent(QMouseEvent*ev) { mouseEvent(ev); }
    33. };
    34.  
    35. int main(int argc, char *argv[])
    36. {
    37. QApplication a(argc, argv);
    38. CMainWidget w;
    39. w.show();
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    The other way not use stack layout the following code illustrate it:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QWidget mainWidget;
    8. mainWidget.setFixedSize(150,80);
    9.  
    10. QHBoxLayout*h_layout = new QHBoxLayout(&mainWidget);
    11. h_layout->setMargin(10);
    12. h_layout->setSpacing(10);
    13.  
    14. QPushButton*but_left = new QPushButton("LEFT");
    15. but_left->setFixedSize(60,60);
    16. QPushButton*but_right = new QPushButton("RIGHT");
    17. but_right->setFixedSize(but_left->size());
    18. mainWidget.layout()->addWidget(but_left);
    19. mainWidget.layout()->addWidget(but_right);
    20.  
    21. //NOTE: important that widget, which isn't in layout was a child of main widget
    22. // also overlay shall be added to mainWidget child's list after it's layout
    23. QPushButton*but_overlay = new QPushButton("overlay",&mainWidget);
    24. but_overlay->setFixedSize(40,20);
    25. QFont font;
    26. font.setPixelSize(6);
    27. but_overlay->setFont(font);
    28. but_overlay->move(55,30); //at center of main widget;
    29.  
    30. mainWidget.show();
    31. return a.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by grin; 15th November 2011 at 00:24.

  3. The following user says thank you to grin for this useful post:

    Oleg (15th November 2011)

  4. #3
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Mouse events with StackAll QStackedLayout

    Thanks, grin.
    The second way is exactly the same I was doing before trying QStackedLayout. I wanted to avoid manual positioning of overlay using layouts, but it seems to be inevitably...
    Oleg Shparber

Similar Threads

  1. Replies: 3
    Last Post: 8th October 2011, 10:46
  2. Replies: 6
    Last Post: 9th September 2008, 21:43
  3. Mouse Events
    By daviddoria in forum Qt Programming
    Replies: 6
    Last Post: 13th May 2008, 12:55
  4. mouse events
    By xyzt in forum Newbie
    Replies: 3
    Last Post: 23rd March 2008, 12:14
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13

Tags for this Thread

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.