Qt 图形场景鼠标事件传播
你好,我正在学习 qt,我正在执行以下操作,将一些小部件添加到图形场景中,
void MainWindow::addWidgets(QList<QWidget *> &list, int code)
{
if(code == CODE_INFO)
{
QWidget *layoutWidget = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
foreach(QWidget *w, list)
{
layout->addWidget(w);
this->connect(((ProductInfo*)w), SIGNAL(productClicked()), this, SLOT(getProductDetails()));
}
layoutWidget->setLayout(layout);
this->scene->addWidget(layoutWidget);
}
}
我的 ProductInfo 类处理鼠标释放并发出信号,
void ProductInfo::mouseReleaseEvent(QMouseEvent *e)
{
QWidget::mouseReleaseEvent(e);
emit productClicked();
}
问题是在将小部件添加到场景后,它们不再获得鼠标释放事件,并且不发出 ProductClicked 信号,但如果我将它们添加到主窗口(而不是场景),它们将按预期工作。我做错了什么?
hello i'm learning qt and i'm doing the folowing to add some widgets to a graphics scene
void MainWindow::addWidgets(QList<QWidget *> &list, int code)
{
if(code == CODE_INFO)
{
QWidget *layoutWidget = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
foreach(QWidget *w, list)
{
layout->addWidget(w);
this->connect(((ProductInfo*)w), SIGNAL(productClicked()), this, SLOT(getProductDetails()));
}
layoutWidget->setLayout(layout);
this->scene->addWidget(layoutWidget);
}
}
my ProductInfo class processes mouse release and emits a signal
void ProductInfo::mouseReleaseEvent(QMouseEvent *e)
{
QWidget::mouseReleaseEvent(e);
emit productClicked();
}
the problem is after adding the widgets to the scene they no longer get the mouse release event and don't emit productClicked signal but if i add them to the main window(not to the scene) they work as expected. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信如果添加 mousePressEvent 事件处理程序并为事件对象调用accept(),您应该能够通过 QGraphicsScene 将 mouseReleaseEvent 发送到您的小部件。史密斯。像这样:
希望这有帮助,问候
I believe you should be able to get mouseReleaseEvent sent to your widget by QGraphicsScene if would add mousePressEvent event handler and call accept() for the event object there. Smth. like this:
hope this helps, regards