Search code examples
c++qtqlineeditqmainwindowqdialog

how to populate values in my Qdialogbox's linedits?


In my main window, I have a QlistWidget named employee_list and the items in this QListWidget are like this :

<FIRM-1> name="Anna Ken" age="25" job="QtMaster" ,
<FIRM-2> name="Sir Ron" age="50" job="QtSlave"

So when I click the items in employee_list QlistWidget, a dialogbox shows up with 3 fields name,age,job like this

Please click this link to open image

but when this dialog box shows up, i want the fields name,age,job to be populated , like this, How can i achieve this ?

Click to see example

so far i have tried this.

void MainWindow::on_employee_list_itemDoubleClicked(QListWidgetItem* item)
{
    QString test = item->text();             // getting the item text
    std::string test_s = test.toStdString();  //converting it to string from Qstring
    string name_part = "";                     //creating a variable in which name will be stored

        int name_pos = test_s.find("name=\"");
        for (int i = name_pos + 6; i < test_s.length();i++)
        {
            if (test_s[i] != '\"')
                name_part += test_s[i];
            else
                break;                         //extracting name in the item's text, after this the name_part variable value is Sir ron.  // similar code for extarcting age and job.
            
    if (test_s.find("<FIRM-1>") != std::string::npos)   //if item contains text <FIRM-1> then show this dialogue
    {
        GrpComd grpComd;   //creating instance of dialog
        grpComd.exec();    //showing it 
              
       grpComd.name_lineedit->settext(name_part);  // i tried this to populate name in name_linedit but getting error , access violation reading location
    }
}

Solution

  • you have to fill and then display i.e. setText() and after that show() or exec():

    if (test_s.find("<FIRM-1>") != std::string::npos)  
    {
        GrpComd grpComd;
        grpComd.setName(clickedName); 
        grpComd.setJob(clickedJob); 
        grpComd.setAge(clickedAge); 
        grpComd.exec();  
    }
    

    or even better, if not already defined, create a constructor with those fields.

    GrpComd::GrpComd(const QString& name,const QString& job, uint age);