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

how to split a string and set as variable then show in plaintext editor?



  • Hello I search a line and find it and show in plaintext editor.

    But I wantto split the line and show only some parts.
    file look like this ;
    prm1 ;456
    prm2;235 and it goes on.

    When it finds prm1 it should set prm1=456;
    and then I should show it on plain text editor. Tried split() but after split I can not show it on the plaintext editor.

    Please help me to do it.

    ifstream input;
    size_t pos;
    string line;

    input.open("D:/qt/project/QFileDemo/myfile.txt");
    if(input.is_open())
    {
    while(getline(input,line))
    {
    pos = line.find("<Prm1>");
    if(pos!=string::npos) // string::npos is returned if string is not found
    {

     QFile file("D:/qt/project/QFileDemo/myfile.txt");
     if(!file.open(QFile::ReadOnly | QFile::Text))
     {
         QMessageBox::warning(this,"title","file not open");
    
     }
    
     QTextStream in(&file);
     //QString text=in.readAll();
     QString text=in.readLine();
    
     ui->plainTextEdit->setPlainText(text);
    
     file.close();
    

    break;
    }
    }
    }


  • Qt Champions 2019

    Why do you mix std and Qt here? Why do you need ifstream at all?
    Splitting strings can be done with e.g. QString::split()



  • @christian-ehrlicher I need to find a specific line, there will be hundreds of lines. code is done in c++ thats why ifstream. You can suggest other methods.
    Tried splitting, maybe Splitting fine but how to set them as variables and show them in the plaintexteditor ? thats the actual question.
    Tried many things but could not show them in the plaintext editor.


  • Qt Champions 2019

    You already use QTextStream::readLine() - so why do you read it with ifstream before? You simply read it twice for no reason.