PDA

View Full Version : Mapping Coordinates of a Widget in a QHorizontal Layout to central Widget



blaise.segbeaya
28th March 2013, 01:22
Suppose I have the following codes:

.... // extract of codes generated by QDesigner
QMainWindow *main = new QMainWindow();
main->resize(935, 638);

// create central widget
QWidget *centralW = new QWidget(main); // set the central Window

// and layout to contains some widgets
QWidget *layoutWidget = new QWidget(centralW);
layoutWidget->setGeometry(10, 100, 219, 22);

// construct a horizontal layout that will contain one label and one lineEdit widgets
QHBoxLayout *horizontalLayout = new QHBoxLayout(layoutWidget);
horizontalLayout->setSpacing(6);

// create the label and lineEdit widgets to add to the horizontal layout
QLabel *label = new QLabel(layoutWidget);
horizontalLayout->addWidget(label);
QLineEdit *lineEdit = new QLineEdit(layoutWidget);
horizontalLayout->addWidget(lineEdit);

// now if P(x, y) is the relative of origin of LineEdit in its coordinate system, what are its coordinates in centralW?

QPoint p = QPoint(lineEdit->frameGeometry().x(), lineEdit->frameGeometry().y());
QPoint p2 = lineEdit->mapTo(centralW, p);

QLineEdit *l2 = new QLineEdit(centralW);

// the following should line up l2 with lineEdit
l2->move(p2.x(), p2.y() + 70); // the widht of lineEdit is < 70
l2->update();

main->setCentralWidget(centralW);

// Result of the position of l2: the Y coordinate seems to be translated by 70 OK, but the X coordinate system leaves l2 hanging on the right hand side of LineEdit.

Anybody has any idea why it is not working?

Thank you.