Search code examples
c++qtqlineedit

Change x and y position of a QLineEdit


I would like to change the position of the lineEdit (or even a PushButton if it's not possible with the lineEdit) from my Qt application, according to the input given. So let's say that I want the x position to be 150 pixels, then I would insert 150 into the lineEdit. Is there any way to do this?

I've already tried this:

void DrawTest::on_lineEdit_returnPressed()
{
    QString x = ui->lineEdit->text();
    qDebug() << "x: " << x;
    QString style = "QLineEdit {"
                    ":" +ui->lineEdit->text()+ "px;"
                    "background-color: #FF00FF;"
                    "};";
    qDebug() << "Style: " << style;
    ui->lineEdit->setStyleSheet(style);
}


Solution

  • It depends on how the QLineEdit is initially positioned. Is it placed within a layout? If so, you won't be able to place it at an absolute position.

    But if it does not belong to any layout, you can just use the move method:

    ui->lineEdit->move(x, y);
    

    Here's the docs.