Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to split a string and set as variable then show in plaintext editor?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5setplaintextstringsearchvariable
4 Posts 2 Posters 1.3k Views
  • 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 Offline
    V Offline
    vehicular
    wrote on last edited by
    #1

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

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

        V Offline
        V Offline
        vehicular
        wrote on last edited by
        #3

        @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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved