Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    qt5 setplaintext string search variable
    2
    4
    768
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      vehicular last edited by

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

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        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()

        Qt has to stay free or it will die.

        V 1 Reply Last reply Reply Quote 3
        • V
          vehicular @Christian Ehrlicher last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

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

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 3
            • First post
              Last post