Olorin
Olorin

Reputation: 415

Qt Graphics Scene mouse event propagation

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?

Upvotes: 1

Views: 4125

Answers (1)

serge_gubenko
serge_gubenko

Reputation: 20482

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

Upvotes: 1

Related Questions