A QList<QVariant> that consists of a QString that contains the name of the ui elements and, accordingly, the settings arrive in the function from the database.

Suppose: checkBox , setChecked(true) .

Question number 1: How to wrap these lines to get ui->checkBox->setChecked(true);

To me as a beginner, this solution seems to be a crutch, but it’s not a matter of mending a lot of if .

Question number 2: How to perform this task more elegantly?

Thank.

  • Have you considered using scripting languages ​​like Lua for this? - Kromster
  • What you want is called reflection, I really don’t know if you can throw links on YouTube, but here youtube.com/watch?v=_oBgonj_E7Q - Vyacheslav Savchenko

2 answers 2

There are two checkboxes on the form: checkBox, checkBox_2

 QWidget *checkBox1 = findChild<QWidget*>("checkBox"); checkBox1->setProperty("checked", true); QWidget *checkBox2 = findChild<QWidget*>("checkBox_2"); checkBox2->setProperty("checked", true); 

the name of the widget, the name and value of the properties to be set can be used from the QList<QVariant> from the base

    As a more flexible solution, try to look in the direction of QJSEngine , it allows you to execute script code inside your application.

    http://doc.qt.io/qt-5/qtjavascript.html