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. Read console output into a string
Forum Updated to NodeBB v4.3 + New Features

Read console output into a string

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.6k 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #1

    I have a gui applicatioon from which I start a console application, and I want to read the data from the console application into a QString variable, this is what I have for now. What is wrong with the code?

    QProcess *process = new QProcess(this);
    QString program = "nvidia-smi.exe";
    QStringList l;
    l<<"-d";
    process->start(program,l);
    qDebug()<<process->state();
    QString y(process->readAllStandardOutput());
    process->close();
    qDebug()<<y;

    Mandruk1331

    jsulmJ 1 Reply Last reply
    0
    • mandruk1331M mandruk1331

      I have a gui applicatioon from which I start a console application, and I want to read the data from the console application into a QString variable, this is what I have for now. What is wrong with the code?

      QProcess *process = new QProcess(this);
      QString program = "nvidia-smi.exe";
      QStringList l;
      l<<"-d";
      process->start(program,l);
      qDebug()<<process->state();
      QString y(process->readAllStandardOutput());
      process->close();
      qDebug()<<y;

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @mandruk1331 What is wrong: you need to wait for the console application to output anything - if you read immediately after starting it there is simply nothing to read yet. You should connect a slot to http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput signal (and one to http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError) and call readAllStandardOutput() in that slot.
      Usually there is no need to call close() if the console application terminates after it is done.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mandruk1331M 1 Reply Last reply
      1
      • jsulmJ jsulm

        @mandruk1331 What is wrong: you need to wait for the console application to output anything - if you read immediately after starting it there is simply nothing to read yet. You should connect a slot to http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput signal (and one to http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError) and call readAllStandardOutput() in that slot.
        Usually there is no need to call close() if the console application terminates after it is done.

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by
        #3

        @jsulm Ok, I will give it a try

        Mandruk1331

        1 Reply Last reply
        0

        • Login

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