开发者

Qt Graphics Scene mouse event propagation

开发者 https://www.devze.com 2022-12-26 04:36 出处:网络
hello i\'m learning qt andi\'m doing the folowing to add some widgets to a graphics scene void MainWindow::addWidgets(QList<QWidget *> &list, int code)

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?


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:

void ProductInfo::mousePressEvent(QMouseEvent* event)
{
    QWidget::mousePressEvent(event);
    event->accept();
}

hope this helps, regards

0

精彩评论

暂无评论...
验证码 换一张
取 消