Convert std::string to QLineEdit



  • Hi all,

    So I have a situation...

    The following code allows me to set the text in a QLineEdit called cores using a map called parameterMap:

    "advancedsettings->getUi()->cores->setText(QString::fromStdString(parameterMap["cores"]));"

    I have a list called allParameters, the entries of which are identical to the keys of parameterMap and the QLineEdit's in advancedsettings. I would like to iterate over allParameters to change the text in every QLineEdit in advancedsettings like so:

    "for(list<string>::iterator it = allParameters.begin(); it != allParameters.end(); ++it){
    advancedsettings->getUi()->*it->setText(QString::fromStdString(parameterMap[*it]));
    }"

    However, the above code produces:
    error: 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' has no member named 'setText'

    and:

    "string myString = "cores";
    advancedsettings->getUi()->myString->setText(QString::fromStdString(parameterMap[myString]));
    "

    results in:
    'class Ui::AdvancedSettings' has no member named 'myString'

    How can I achieve what I want to? I must need to convert a std::string to some QT data type but none of my attempts have worked.

    Many thanks,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    lineEdit->setText(QString::fromStdString(yourStdString));
    

    Is the correct form.

    Do you have as much QLineEdit in your UI to enter all the strings from your parameterMap ?



  • Hi!

    Many thanks for your help.

    To answer your question I have more strings in parameterMap than QLineEdit's in the UI (advancedsettings).

    I'd like to iterate through all the QLineEdit's in advancedsettings rather than manually having to do something like:

    advancedsettings->getUi()->firstQLineEditInadvancedsettings->setText(QString::fromStdString(myStdString))

    advancedsettings->getUi()->secondQLineEditInadvancedsettings->setText(QString::fromStdString(myStdString))

    ...

    advancedsettings->getUi()->lastQLineEditInadvancedsettings->setText(QString::fromStdString(myStdString))

    Is there a way I can achieve this?

    Man thanks,


  • Lifetime Qt Champion

    You can create the QLineEdits dynamically


Log in to reply
 

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