-1

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.

Neocoder_1
  • 49
  • 1
  • 13

1 Answers1

1

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

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • when I am adding a pushbutton dynamically , I want to edit the text displayed on the button during run time itself , wherein the name of the button gets stored in my database and the next time I want to edit , the same name should be displayed for me to edit , can this be possible during run time itself ? – Neocoder_1 Feb 28 '23 at 06:27
  • 1
    @SrinathS displayed text is 'setText', it's not a name of Button. What database you talk about? your own that you maintain in code? Sure, that can be done, serialize all properties values you want to save. Essentially I would make serialization procedures for that. What you do is re-creating Designer's functionality, it uses an XML domain document of .ui file format as a database. – Swift - Friday Pie Feb 28 '23 at 06:42