BUY
BUY

Reputation: 736

Send Signal From Child To Parent On Mouse Click Qt

I have a MainWindow, a DockWidget and a QGLWidget inside it. I am drawing circles inside the QGLWidget. I want to print the x and y coordinates of the place that I am clicking to the labels inside DockWidget.

I would be glad if someone could show me the way?

Upvotes: 0

Views: 663

Answers (1)

Lazy
Lazy

Reputation: 277

Override the QWidget::mousePressEvent(QMouseEvent *) or QWidget::mouseReleaseEvent(QMouseEvent *) of the QGLWidget and create a signal for sending the x and y. It means you have to create own class inherithing from QGLWidget. It could look like this:

void MyGLWidget::mouseReleaseEvent(QMouseEvent *event)
{
  emit printXY(event->pos());
}

and in the MyGLWidget.h:

signals:
void printXY(const QPointF& clickedPos);

then create a slot in the DockWidget:

public slots:
void onPrintXY(const QPointF& clickedPos);

and connect it in the place where you create the MyGLWidget. I suppose in some function of the DockWidget like:

void DockWidget::createGLWidget()
{
  MyGLWidget widget = new MyGLWidget;
  connect(widget, &MyGLWidget::printXY, this, &DockWidget::onPrintXY);
}

Upvotes: 3

Related Questions