开发者

Problem in creating text file(Qt application) in installation directory (using CMake to install)

开发者 https://www.devze.com 2023-02-22 23:59 出处:网络
I am new to programming. I am creating a small word jumble game to practice qt programming. In this application I am creating a text file (score.txt) to keep score of player. I have done this by:

I am new to programming. I am creating a small word jumble game to practice qt programming. In this application I am creating a text file (score.txt) to keep score of player. I have done this by:

    QFile scoreFile("score.txt");
    if (QFile::exists("score.txt"))
    {
        scoreFile.open(QIODevice::ReadWrite | QIODevice::Text)
        // and update the score.
    }
    else
    {
        scoreFile.open(QIODevice::ReadWrite | QIODevice::Text);//create score file
        //and write the score to it.
    }

this code is working good here. Now I am using CMake to build and install generated binary (I am working on Ubuntu) using this code:

#set project name, version and build code here.

install(TARGETS wordJumbleGame DESTINATION bin)

I build project in /home/myname/project/build/

My source code is in /home/myname/project/src/

CMakeLists.txt is in /home/myname/project/CMakeLists.txt

I installed program using make install. Till here all things working fine. But now problem is that when I run this program (I run it from terminal giving command wordJumbleGame) It creates score.txt in /home/myname/project/build directory. It is not being created in installation dir bin.

So please help me out, what am I doing wrong. And please also tell me how do 开发者_Python百科I make my program to appear in application->game lists so I can run it from there not from command prompt.


Unless you prefix it with a slash (on unix) or a drive path (Windows), QFile's constructor parameter is a relative path - relative to the current working directory. score.txt is created in the build/ directory because that's probably where you're executing the binary from.

You can't store score.txt in the /usr/bin directory because, typically, you can't write there without root privileges.

What you want to do is get a path to some directory where you can store your score.txt file. To do that, you can use the QDesktopServices class. That will give you directory information on a per-user basis. Here's an example:

#include <QDesktopServices>

// this would go in main(), probably
QCoreApplication::setApplicationName( "word jumble game" );

// now when you want to read/write the scores file:
QString dataPath = QDesktopService::storageLocation( QDesktopService::DataLocation );
QFile scoreFile( dataPath + "score.txt" );

// on my system, this produces: "/home/adam/.local/share/data/word jumble game/score.txt"
// it will produce something similar for Windows and Mac too

You should set your appication name via QCoreApplication::setApplicationName before getting path information to keep the user data directory nice and organised.

As for getting your application in the games list, you'll need to create a menu entry that follows the freedesktop.org spec. I can't help you more with that, but this is a good starting point. Somebody else might have more info for you.

You need to create a .desktop entry file and install it using xdg-desktop-menu install. Here are two resources for you: freedesktop.org menu spec and adding .desktop files using CMake

0

精彩评论

暂无评论...
验证码 换一张
取 消