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:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
mainWidget.setMinimumSize(300, 400);
videoWidget
->layout
()->addWidget
(new QPushButton("Video"));
static_cast<QVBoxLayout *>(videoWidget->layout())->insertStretch(-1);
stackedLayout->addWidget(videoWidget);
static_cast<QVBoxLayout *>(controlWidget->layout())->insertStretch(-1);
controlWidget
->layout
()->addWidget
(new QPushButton("Controls"));
stackedLayout->addWidget(controlWidget);
static_cast<QHBoxLayout *>(mainWidget.layout())->addLayout(stackedLayout);
mainWidget.show();
return a.exec();
}
In this program it's impossible to click on "Video" button.
Any suggestions?
Thanks in advance.
Re: Mouse events with StackAll QStackedLayout
see answer of Thierry Bastian for https://bugreports.qt.nokia.com/browse/QTBUG-3868
Quote:
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:
Code:
#include <QtGui>
{
public:
{
setMouseTracking(true);
v_layout->addWidget(m_video_but);
v_layout->addStretch();
stack->addWidget(video_widget);
v_layout->addStretch();
stack->addWidget(control_widget);
stack->setCurrentWidget(m_control_widget);
}
private:
{
if(m_video_but->rect().contains(ev->pos()))
qApp->sendEvent(m_video_but,ev);
}
void mousePressEvent
(QMouseEvent*ev
) { mouseEvent
(ev
);
} void mouseReleaseEvent
(QMouseEvent*ev
) { mouseEvent
(ev
);
} void mouseDoubleClickEvent
(QMouseEvent*ev
) { mouseEvent
(ev
);
} };
int main(int argc, char *argv[])
{
CMainWidget w;
w.show();
return a.exec();
}
The other way not use stack layout the following code illustrate it:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
mainWidget.setFixedSize(150,80);
h_layout->setMargin(10);
h_layout->setSpacing(10);
but_left->setFixedSize(60,60);
but_right->setFixedSize(but_left->size());
mainWidget.layout()->addWidget(but_left);
mainWidget.layout()->addWidget(but_right);
//NOTE: important that widget, which isn't in layout was a child of main widget
// also overlay shall be added to mainWidget child's list after it's layout
but_overlay->setFixedSize(40,20);
font.setPixelSize(6);
but_overlay->setFont(font);
but_overlay->move(55,30); //at center of main widget;
mainWidget.show();
return a.exec();
}
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...