How to fetch coordinates of a Widget in QDockWidget?
-
I am trying to fetch coordinates of Visualization Panel B in QDockWidget (Panel A) by creating a QLabel.
On hovering of mouse in Panel B, respective coordinates should display in Panel A. I've separate cpp files for both the panels. Panel A class contains QDockWidget and QLabel and Panel B class has Visualization Widget. Panel B class has mouseMoveEvent() defined in it.
Let's say Panel B class has below code:void panelB::mouseMoveEvent(QMouseEvent *eventMove) { QString txt; txt = QString("(X:%1,Y:%2)").arg(eventMove->pos().x()).arg(eventMove->pos().y()); //qDebug() << txt; }
Let's say Panel A class has below code:
QWidget *panelA::getData(QString name) { QGroupBox *data = new QGroupBox; data->setTitle("Mouse Coordinates"); QLabel *x_y = new QLabel(data); x_y->setGeometry(QRect(110,20,100,20)); //x_y->setText(txt); }
How do I configure both the functions with the current structure to display the coordinates in Panel A. I've checked with qDebug() and it's working fine. Also how difficult is to change the current coordinate systems?
-
@rohit1729 Emit a signal in panelB::mouseMoveEvent with the coordinates as parameters and connect a slot to this signal in panelA.
-
@rohit1729 You should read this first: https://doc.qt.io/qt-5/signalsandslots.html
void panelB::mouseMoveEvent(QMouseEvent *eventMove) { QString txt; txt = QString("(X:%1,Y:%2)").arg(eventMove->pos().x()).arg(eventMove->pos().y()); emit coordinatesChanged(eventMove->pos().x(), eventMove->pos().y()); //qDebug() << txt; }
coordinatesChanged would be the signal.