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 we can run Linux command from qt code?
Forum Update on Monday, May 27th 2025

How we can run Linux command from qt code?

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 2.5k 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.
  • W Offline
    W Offline
    Wasee
    wrote on 5 Jan 2022, 03:24 last edited by
    #1

    Hi everyone;
    I want to run the Linux command from qt code . I did following code to run the command but it is not executing my command.

    QString ab = "cd  /sys/bus/iio/devices/iio:device1 && echo 24250000 > out_altvoltage1_TX_LO_frequency"
    QProcess p;
    p.startDetached("/bin/bash", QStringList()<<"-c"<<ab);
    

    Thanks in advance!

    J 1 Reply Last reply 5 Jan 2022, 05:12
    0
    • W Wasee
      5 Jan 2022, 03:24

      Hi everyone;
      I want to run the Linux command from qt code . I did following code to run the command but it is not executing my command.

      QString ab = "cd  /sys/bus/iio/devices/iio:device1 && echo 24250000 > out_altvoltage1_TX_LO_frequency"
      QProcess p;
      p.startDetached("/bin/bash", QStringList()<<"-c"<<ab);
      

      Thanks in advance!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Jan 2022, 05:12 last edited by
      #2

      @Wasee Why do you want to use a process to read from a device file?
      Simply read that file using QFile like any other file.

      1 Reply Last reply
      3
      • W Offline
        W Offline
        Wasee
        wrote on 5 Jan 2022, 05:43 last edited by
        #3

        @jsulm Thanks for your reply!
        I want write this command on device from qt code. I code in following way.

        QString ab = "cd /sys/bus/iio/devices/iio\:device1 && echo  ";
        
        QString ad = " > out_altvoltage1_TX_LO_frequency";
                                      ab.append(24250000);
        QProcess on;
        on.start(ab.append(ad));
        on.waitForFinished(-1);
        qDebug() << "TX_LO ="<<ab;
        QProcess p;
        p.startDetached("/bin/bash", QStringList()<<"-c"<<ab);
        

        I made command successfully but not running on device through qt.

        J 1 Reply Last reply 5 Jan 2022, 06:26
        0
        • W Wasee
          5 Jan 2022, 05:43

          @jsulm Thanks for your reply!
          I want write this command on device from qt code. I code in following way.

          QString ab = "cd /sys/bus/iio/devices/iio\:device1 && echo  ";
          
          QString ad = " > out_altvoltage1_TX_LO_frequency";
                                        ab.append(24250000);
          QProcess on;
          on.start(ab.append(ad));
          on.waitForFinished(-1);
          qDebug() << "TX_LO ="<<ab;
          QProcess p;
          p.startDetached("/bin/bash", QStringList()<<"-c"<<ab);
          

          I made command successfully but not running on device through qt.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 5 Jan 2022, 06:26 last edited by
          #4

          @Wasee Can you please explain why you need to read the /sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency file using an addional process?
          As I told you already you can read this file in your Qt app using QFile...

          Also, why do you start TWO processes now?!

          1 Reply Last reply
          2
          • W Offline
            W Offline
            Wasee
            wrote on 5 Jan 2022, 06:47 last edited by
            #5

            @jsulm thanks;
            I passed the RX_LO_frequency to the device via bashscript using QProcess when I connect the device with a signal generator from the external at this time my device sense according to the value I passed in RX_LO_frequency. Now I want to pass the same value on TX_LO_frequency using QProcess this is the main reason I am using QProcess.
            thanks

            J 1 Reply Last reply 5 Jan 2022, 06:51
            0
            • W Wasee
              5 Jan 2022, 06:47

              @jsulm thanks;
              I passed the RX_LO_frequency to the device via bashscript using QProcess when I connect the device with a signal generator from the external at this time my device sense according to the value I passed in RX_LO_frequency. Now I want to pass the same value on TX_LO_frequency using QProcess this is the main reason I am using QProcess.
              thanks

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 5 Jan 2022, 06:51 last edited by
              #6

              @Wasee I still fail to understand the need for QProcess.
              You can do all this using QFile way more efficiently.
              But it is up to you...

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Wasee
                wrote on 5 Jan 2022, 06:58 last edited by
                #7

                @jsulm thanks;
                kindly give me any suitable example QFile device read.
                thanks

                J J 2 Replies Last reply 5 Jan 2022, 07:16
                0
                • W Wasee
                  5 Jan 2022, 06:58

                  @jsulm thanks;
                  kindly give me any suitable example QFile device read.
                  thanks

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 5 Jan 2022, 07:16 last edited by
                  #8

                  @Wasee said in How we can run Linux command from qt code?:

                  kindly give me any suitable example QFile device read

                  What example?
                  These device files are files, so you read/write them like any other files...

                  1 Reply Last reply
                  4
                  • W Wasee
                    5 Jan 2022, 06:58

                    @jsulm thanks;
                    kindly give me any suitable example QFile device read.
                    thanks

                    J Offline
                    J Offline
                    JonB
                    wrote on 5 Jan 2022, 07:22 last edited by JonB 1 May 2022, 07:36
                    #9

                    @Wasee
                    To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into a bash shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run it startDetached() instead of plain start(). You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has \: in it. That is not right for C++. Either \\: or just simple :. Maybe that is why it had problems.]

                    However, as @jsulm said this command simple writes a string to a file. So no need for a QProcess, what is wrong with:

                    QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                    if (f.open(QIODevice::WriteOnly))
                    {
                        qint64 count = f.write("24250000");
                        Q_ASSERT(count > 0);
                        f.close();
                    }
                    
                    W 2 Replies Last reply 5 Jan 2022, 09:41
                    5
                    • J JonB
                      5 Jan 2022, 07:22

                      @Wasee
                      To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into a bash shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run it startDetached() instead of plain start(). You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has \: in it. That is not right for C++. Either \\: or just simple :. Maybe that is why it had problems.]

                      However, as @jsulm said this command simple writes a string to a file. So no need for a QProcess, what is wrong with:

                      QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                      if (f.open(QIODevice::WriteOnly))
                      {
                          qint64 count = f.write("24250000");
                          Q_ASSERT(count > 0);
                          f.close();
                      }
                      
                      W Offline
                      W Offline
                      Wasee
                      wrote on 5 Jan 2022, 09:41 last edited by
                      #10

                      @JonB Hi;
                      I coded as you suggested above but still not changing TX_LO_frequency value. why?
                      thanks

                      J J 3 Replies Last reply 5 Jan 2022, 09:44
                      0
                      • W Wasee
                        5 Jan 2022, 09:41

                        @JonB Hi;
                        I coded as you suggested above but still not changing TX_LO_frequency value. why?
                        thanks

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 5 Jan 2022, 09:44 last edited by
                        #11

                        @Wasee Did you do what @JonB mentioned: "provided you have first typed that command into a bash shell to verify it works correctly"?
                        So, does it work if you do it in terminal?

                        1 Reply Last reply
                        1
                        • W Wasee
                          5 Jan 2022, 09:41

                          @JonB Hi;
                          I coded as you suggested above but still not changing TX_LO_frequency value. why?
                          thanks

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 5 Jan 2022, 09:46 last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • W Wasee
                            5 Jan 2022, 09:41

                            @JonB Hi;
                            I coded as you suggested above but still not changing TX_LO_frequency value. why?
                            thanks

                            J Offline
                            J Offline
                            JonB
                            wrote on 5 Jan 2022, 09:57 last edited by JonB 1 May 2022, 09:58
                            #13

                            @Wasee said in How we can run Linux command from qt code?:

                            but still not changing TX_LO_frequency value. why?

                            Did you even at least check the return result of the open() for yourself? For all I know you do not have permission to write to that file. This is part of what doing development involves....

                            @jsulm

                            Also I would not write a string but a number:

                            That's fine, but it's not what the OP's echo 24250000 > out_altvoltage1_TX_LO_frequency would do (treat anything as a number), which I was trying to copy. In fact I think Linux echo will append a newline, so the OP may need to do that too.

                            1 Reply Last reply
                            2
                            • W Offline
                              W Offline
                              Wasee
                              wrote on 5 Jan 2022, 10:25 last edited by
                              #14

                              @JonB said in How we can run Linux command from qt code?:
                              echo 24250000 > out_altvoltage1_TX_LO_frequency this command run in terminal and change the value on device. But not changing from qt code.

                              J 1 Reply Last reply 5 Jan 2022, 10:27
                              0
                              • W Wasee
                                5 Jan 2022, 10:25

                                @JonB said in How we can run Linux command from qt code?:
                                echo 24250000 > out_altvoltage1_TX_LO_frequency this command run in terminal and change the value on device. But not changing from qt code.

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 5 Jan 2022, 10:27 last edited by
                                #15

                                @Wasee said in How we can run Linux command from qt code?:

                                change the value on device

                                Without sudo?

                                Please add error handling to your code: print error message if f.open(...) fails.

                                W 1 Reply Last reply 5 Jan 2022, 10:42
                                0
                                • J jsulm
                                  5 Jan 2022, 10:27

                                  @Wasee said in How we can run Linux command from qt code?:

                                  change the value on device

                                  Without sudo?

                                  Please add error handling to your code: print error message if f.open(...) fails.

                                  W Offline
                                  W Offline
                                  Wasee
                                  wrote on 5 Jan 2022, 10:42 last edited by
                                  #16

                                  @jsulm Hi;
                                  Yes without sudo in terminal its changing value. No error of device open its not returning.
                                  thanks

                                  J 1 Reply Last reply 5 Jan 2022, 10:53
                                  0
                                  • W Wasee
                                    5 Jan 2022, 10:42

                                    @jsulm Hi;
                                    Yes without sudo in terminal its changing value. No error of device open its not returning.
                                    thanks

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 5 Jan 2022, 10:53 last edited by
                                    #17

                                    @Wasee

                                    • Did you add the error checking code I advised?
                                    • Did you try appending the newline I advised?
                                    1 Reply Last reply
                                    0
                                    • J JonB
                                      5 Jan 2022, 07:22

                                      @Wasee
                                      To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into a bash shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run it startDetached() instead of plain start(). You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has \: in it. That is not right for C++. Either \\: or just simple :. Maybe that is why it had problems.]

                                      However, as @jsulm said this command simple writes a string to a file. So no need for a QProcess, what is wrong with:

                                      QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                                      if (f.open(QIODevice::WriteOnly))
                                      {
                                          qint64 count = f.write("24250000");
                                          Q_ASSERT(count > 0);
                                          f.close();
                                      }
                                      
                                      W Offline
                                      W Offline
                                      Wasee
                                      wrote on 6 Jan 2022, 04:53 last edited by
                                      #18

                                      @JonB said in How we can run Linux command from qt code?:

                                      QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                                      if (f.open(QIODevice::WriteOnly))
                                      {
                                      qint64 count = f.write("24250000");
                                      Q_ASSERT(count > 0);
                                      f.close();
                                      }

                                      how I can pass variable instead of 24250000.

                                      1 Reply Last reply
                                      0
                                      • W Offline
                                        W Offline
                                        Wasee
                                        wrote on 6 Jan 2022, 05:18 last edited by
                                        #19

                                        @JonB Thanks for your appreciated help!
                                        Your suggestion of QFile is working fine now.

                                        QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                                        if (f.open(QIODevice::WriteOnly))
                                        {
                                            qint64 count = f.write("2425000000");
                                            Q_ASSERT(count > 0);
                                            f.close();
                                        }
                                        

                                        The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
                                        thanks

                                        KroMignonK J 2 Replies Last reply 6 Jan 2022, 08:13
                                        0
                                        • W Wasee
                                          6 Jan 2022, 05:18

                                          @JonB Thanks for your appreciated help!
                                          Your suggestion of QFile is working fine now.

                                          QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                                          if (f.open(QIODevice::WriteOnly))
                                          {
                                              qint64 count = f.write("2425000000");
                                              Q_ASSERT(count > 0);
                                              f.close();
                                          }
                                          

                                          The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
                                          thanks

                                          KroMignonK Offline
                                          KroMignonK Offline
                                          KroMignon
                                          wrote on 6 Jan 2022, 08:13 last edited by
                                          #20

                                          @Wasee said in How we can run Linux command from qt code?:

                                          The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
                                          thanks

                                          Are you joking???
                                          Do you really don't have any idea how to do this simple stuff?
                                          It is so complicated to create a string and write it to a file?
                                          What did you try to do?

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          1 Reply Last reply
                                          0

                                          5/21

                                          5 Jan 2022, 06:47

                                          topic:navigator.unread, 16
                                          • Login

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