c++qt-creatorqpushbuttonqframe

How to edit the name of a dynamically added pushbutton inside a qframe


I have added few pushbuttons dynamically inside a qframe,but I want to edit the names of the pushbuttons.

QPushButton* newButton = new QPushButton("new button " + QString::number(ui->frame->findChildren<QPushButton*>().count()));
newButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
newButton->setFixedSize(100, 50);
newButton->setParent(ui->frame_6);
int buttonWidth = newButton->width();
int buttonHeight = newButton->height();
int numRows = ui->frame_6->width() / (buttonWidth + 10);
int row = ui->frame_6->findChildren<QPushButton*>().count() / numRows;
int col = ui->frame_6->findChildren<QPushButton*>().count() % numRows;
QPoint buttonPos = QPoint(col * (buttonWidth + 10), row * (buttonHeight + 10));
newButton->move(buttonPos);
newButton->show();

I have used this code snippet to add pushbuttons each time I press the addnew button.Now I want help to edit the names of the newly added pushbuttons.


Solution

  • You either add buttons by editor to the form or you add them dynamically during runtime, you can't have both. Actually you can, but the latter won't affect the former. You should not try to edit generated .cpp files, they are generated from respective form files.

    When you use Designer editor to create .ui file, it creates an .xml file which is used by uic generator utility (UI compiler).

    In Designer Form editor, it's the objectName property and Designer would match objectName property value to variable name. The property can be set by QObjects setObjectName() setter. You can't dynamically add variables to code, can you? That would require some "generate code and compile" functionality in program.

    enter image description here