views:

360

answers:

1

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?

A: 

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

serge_gubenko
thanks that was spot on just one question how did you came to this conclusion so next time i can do it on my own. thanks again brother.
Olorin
I took a look into QGraphicsProxyWidgetPrivate::sendWidgetMouseEvent source code to see how mouse events are processed by the graphics scene widget
serge_gubenko
thanks again brother you rock :)
Olorin