Results 1 to 6 of 6

Thread: problem with application after it runs on start up

  1. #1
    Join Date
    Mar 2010
    Posts
    86
    Thanks
    11
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default problem with application after it runs on start up

    SO I wrote an application with Qt 4.6 in Windows 7, that uses a few .txt files for storing information. It runs just fine when I run it from the release folder.
    However, I implemented QSettings to add the application to the registry so that it runs on system startup. It does this just fine, BUT my problem is that it then does not open and read the text files. I'm guessing because its not in the actual directory when running from the startup. At first I realized that I was using a Relative path in the same directory as the application, so I grabbed the application Dir Path through the main when I call

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7.  
    8. QApplication a(argc, argv);
    9. QString exePath = a.applicationDirPath();
    10. MainWindow w;
    11. w.exePath = exePath;
    12. return a.exec();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    exePath is a Public QString in mainwindow.h

    so, when I open the file to read, I put the applicationDirPath in front of the file name and use the absolute path.
    with:

    Qt Code:
    1. QString thisDir = exePath;
    2. thisDir += "time.txt";
    3.  
    4. QFile fileOpen(thisDir);
    To copy to clipboard, switch view to plain text mode 

    but this still doesn't solve the problem. And nothing is opened when it's run from startup in the registry... Does anyone know why this is happening? Are startup programs forbidden to open text files, or is it just running from some different directory still? Some tips would be great, because every time I test this, I have to restart the machine after a new change/compile/and run. And this is starting to get tedious....

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with application after it runs on start up

    See to output:
    Qt Code:
    1. if (fileOpen.open( QIODevice::ReadWrite ))
    2. {
    3. }
    4. else
    5. qDebug( fileOpen.errorString() );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: problem with application after it runs on start up

    If by "store information" you mean write to these files then you should know that you cannot write to protected directories on Windows 7: this includes "Program Files" and subdirectories. Use QDesktopServices or Windows API calls to find a better location to store these files.

  4. #4
    Join Date
    Mar 2010
    Posts
    86
    Thanks
    11
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: problem with application after it runs on start up

    Thanks, that QDesktopServices tip totally worked and fixed my read file on startup problem and the future write to file problem I wasn't aware of as I haven't put the application into Program Files yet...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with application after it runs on start up

    I think your reads failed because you were missing a directory separator between applicationDirPath() and the file name.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem with application after it runs on start up

    ChrisW67's observation is mostly correct: in Win 7, applications are forbidden from creating files in protected directories. In our experience, if we try to create a file after installation of the app, this fails. However, if we use the installer (NSIS, in this case) to create an empty file at installation time, we find that we can then write to it afterward.

    Of course, Micro$oft wants you to use "Application Data" instead, but in our case, the file was required to be in the app startup folder. Took a while to figure out what was wrong and how to fix it.

Similar Threads

  1. QProcess::start() failed when application runs from sudo
    By alenyashka in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2010, 06:35
  2. How to start external application from QT?
    By TomASS in forum Newbie
    Replies: 10
    Last Post: 7th November 2009, 17:57
  3. PyQT application start
    By mejustme in forum Newbie
    Replies: 2
    Last Post: 6th June 2008, 18:15
  4. Replies: 2
    Last Post: 20th November 2007, 20:00
  5. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.