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. Code to launching a program
Qt 6.11 is out! See what's new in the release blog

Code to launching a program

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.3k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    Tyler1234
    wrote on last edited by
    #1

    I want to know how to make a pushbutton that when clicked will open a program or file(such as a text file or internet explorer). I do not need to read or write to it, just launch/open it.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      frfm
      wrote on last edited by
      #2

      Ok...First you need to create a SLOT and connect to QPushButton, this SLOT is called when a SIGNAL clicked() is emitted by QPushButton. A tip, "SIGNAL/SLOT":http://qt-project.org/doc/qt-5/signalsandslots.html is key of Qt, I suggest you read more about this.

      And to open a file or program you can use "QProcess":http://qt-project.org/doc/qt-5/QProcess.html.

      In practice it would be something like this:

      @

      QPushButton *button = new QPushButton("Button", this);
      connect(button, SIGNAL(clicked()), this, SLOT(openProgramFile()))

      @

      So you create a SLOT....

      Add this in your Header file (.h):

      @

      protected slot:

      void openProgramFile();

      @

      Add this in your Source file (.cpp):

      @

      void yourClass::openProgramFile(){

      QString programPath = "/path/to/program";
      QStringList arguments;
      arguments << "argument1" << "argument2";
      
      QProcess *myProcess = new QProcess(this&#41;;
      myProcess->start(programPath, arguments);
      

      }

      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thEClaw
        wrote on last edited by
        #3

        Place a QPushButton, use QObject::connect() to connect it to a method of your choosing, inside that method use QProcess to launch a different program. Quite easily done, actually!

        EDIT: I didn't notice that somebody else already wrote a much more detailed reply, hadn't refreshed the page after a little bit of waiting.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tyler1234
          wrote on last edited by
          #4

          Thanks for the help!

          I also have tried the system(" file directory "); and that works to.

          I would like to have the program be hidden and close after certain time. QProsses be able to do this to?

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            QProcess will help you to kill/terminate created process. You will not be able to hide it through QProcess

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            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