Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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_listqlistwidget, a dialogbox shows up with 3 fields name,age,job like this
    c2c67c9c-d2e2-4eb2-b45d-11c831ab3ef0-image.png

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

    06cbe3b5-a89f-48ef-9119-ad0a09ec13c1-image.png

    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
        }
    }
    


  • @learnist Did you check is their any string coming from Item->text(); ?


  • Lifetime Qt Champion

    @learnist Did you actually debug your code to see what happens?



  • @shashikumar Yes, i am getting string, only problem is propulating that into dialog box.



  • @learnist

            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
    

    The second line compiles, does it? With settext ? Your code compiles with the {s & }s you show, right? OK, I see you've done some editing of the code you have to shorten.

    Did you try doing the second line before the first one, if you want to put the text in before you showed the user the modal dialog?


  • Lifetime Qt Champion

    Hi,

    exec is a blocking call so basically you modify your dialog content after you dismiss it.