Attach a dynamic button to a window

  • So basically I want to make the following: When a button is pushed, create a button at point X with size Y which has text which is extracted from a textEdit. This is the function I have made:

    @void MainWindow::on_pushButton_released()
    QString a = ui->textEdit->toPlainText();
    QPushButton* ptr = new QPushButton(a);
    addWidget(ptr); // I figured I should use this function, thing is I don't know what should call it
    ptr->setGeometry(260, 140, 40, 40);

    It works, however the window appears on the 260, 140 on my desktop. I want it to be 260,140 in the main window, wherever it is. Thanks in advance.

  • try this:
    @ QString a = ui->textEdit->toPlainText();
    QPushButton* ptr = new QPushButton(this);
    ptr->setGeometry(260, 140, 40, 40);@

  • Thank you, that worked ! I didn't attach the widget to the window and that is where the problem was.

  • You are welcome :)
    Glad to hear it.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.