Skip to content
  • 0 Votes
    3 Posts
    581 Views
    Y

    @koahnig thank you.

  • 0 Votes
    3 Posts
    1k Views
    Michael RozshkoM

    You are right! It is my error, rebuild all project with full delete old directory and all work fine. Thanks for fast and good answer!

  • 0 Votes
    4 Posts
    922 Views
    G

    I tried some different options, and found a solution:

    void MainWindow::saveSettings() { name = (QCoreApplication::applicationDirPath() +"/logs/"+ui->lineEdit->text() +".txt"); QFile file (name) ; if (newfile == true){ if (file.exists()){ qDebug ()<<"already exists"; NewLog(); timer->stop(); } else { qDebug()<< "Does not exist"; file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append); QString c_time = QTime::currentTime().toString(); QString sText =QString::number(update) + ". "+ c_time + ": rpm = "+ rpm + ", load = "+ QString::number(load) + ", " + name1+ " = " + result1 + ", "+ name2+ " = " + result2 + ", " + name3+ " = " + result3 + ", "+ name4+ " = " + result4 + ", " + name5+ " = " + result5 + ", "+ name6+ " = " + result6 + ", " + name7+ " = " + result7 + ", "+ name8+ " = " + result8 + ", " + name9+ " = " + result9 + ", "+ name10+ " = " + result10; ui->label_log->setText(sText); update++; QTextStream out(&file); out << sText + "\n"; file.close(); newfile = false; }}else{ file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append); QString c_time = QTime::currentTime().toString(); QString sText =QString::number(update) + ". "+ c_time + ": rpm = "+ rpm + ", load = "+ QString::number(load) + ", " + name1+ " = " + result1 + ", "+ name2+ " = " + result2 + ", " + name3+ " = " + result3 + ", "+ name4+ " = " + result4 + ", " + name5+ " = " + result5 + ", "+ name6+ " = " + result6 + ", " + name7+ " = " + result7 + ", "+ name8+ " = " + result8 + ", " + name9+ " = " + result9 + ", "+ name10+ " = " + result10; ui->label_log->setText(sText); update++; QTextStream out(&file); out << sText + "\n"; file.close(); }}

    Maybe it can be easier, but i am still learning. Thanks for your input.
    If you have comments on how to change my code, please tell me.
    (newlog was only to test something else now)

  • 0 Votes
    3 Posts
    1k Views
    Y

    @SGaist Thank you for answer. From your suggestion, I need to create my own Dialog box.

  • 0 Votes
    6 Posts
    5k Views
    JonBJ

    @VRonin said in Save a text file with Windows/Dos line endings but while preserving encoding in QT.:

    if you save text use text mode on the device: if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) and if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) everything else comes for free

    I thought the whole point of his question is precisely that he wants to produce text files which are not native to the OS, specifically he wants Windows \r\n regardless of whether his app is running under Win or Linux. That was why I was saying don't use endl etc., and of course you mustn't use QIODevice::Text. Maybe I misunderstood... :)

  • 0 Votes
    3 Posts
    1k Views
    L

    @FloatFlower.Huang Thanx alot mate

  • QFile Read Binary blockwise

    Solved General and Desktop
    2
    0 Votes
    2 Posts
    2k Views
    J.HilkJ

    Never mind,
    @tobias-hunger in this thread 6 years ago is right.

    I changed:

    qiSend = file.read(readData,iSend);

    to

    QByteArray b = file.read(iSend);

    And now I'm getting all needed bytes.

  • 0 Votes
    3 Posts
    2k Views
    CybeXC

    @Chris-Kawa

    It would actually just make things easier.

    I would like to generalize this procedure to e.g.

    QFile::copy(":/my/file", r.OriginalFileName);

    Here Qresource::OriginalFileName() returns the original file name e.g. somefile.txt

    Now, I am required to (for each file):

    QString newLocation = QString(tempdir + tempdir.seperator() + QString("myNewFileName.txt")); QFile::copy(":/my/file", newLocation);

    It just seems tedious and not very efficient.

    But thanks for clearing it up though,

    *grabs coffee

  • 0 Votes
    7 Posts
    5k Views
    S
    //copy whole <from> directory to the folder <to> void copyDirectoryNested(QString from,QString to) { QDirIterator it(from, QDirIterator::Subdirectories); while (it.hasNext()) { QString file_in = it.next(); QFileInfo file_info = QFileInfo(file_in); QString file_out = file_in; file_out.replace(from,to); if(file_info.isFile()) { //is file copy qDebug() << QFile::copy(file_in, file_out); qDebug() << file_in << "<----" << file_out; } if(file_info.isDir()) { //dir mkdir QDir dir(file_out); if (!dir.exists()) qDebug() << "mkpath"<< dir.mkpath("."); } } }

    then use above function :

    copyDirectoryNested(":/<qrc_folder_location>",<writing_location>); copyDirectoryNested(":/DefaultAppData",app_data_location);
  • 0 Votes
    4 Posts
    3k Views
    m.sueM

    @mrjj said in Closing a QFile in use.:

    output a message to user that file is locked

    Yes, as the other app would most certainly crash if it suddenly works on a file handle that was closed from outside.
    -Michael.

  • 0 Votes
    2 Posts
    853 Views
    CharlieGC

    Hi @Mathan-M,

    With QML you can use Qt.openUrlExternally(/path/to/my/file/myFile) to open an file with the default program.
    More informations here.

    Bye

    Charlie

  • 0 Votes
    2 Posts
    1k Views
    VRoninV

    Can you try if specifying an absolute path fixes the problem? My guess is that it's a working directory problem.
    Change "test.wav" into something like QDir::tempPath() + "/test.wav" in both createAudioRecorder and endRecordAudio

  • QFile access and file deleted

    Solved General and Desktop
    4
    0 Votes
    4 Posts
    2k Views
    fra87F

    Ok, thank you both for the explanations and the links :)

  • 0 Votes
    8 Posts
    5k Views
    K

    @HenrikSt.

    You got a number of different possibilities suggested. Possibly you are overwhelmed by the different things. Sometimes it is hard for the people answering to know where the real problem was/is.

    I am not sure now if this might be too detailed for you. Anyway I move forward.

    I would suggest, if you have not done yet, The example from QFile as also listed by @jsulm and play a bit around. Just change the file name to somthing you need. E.g.

    QFile file("c:/data/myNewFolder/out.txt");

    That is a possibility to specify a complete (absolute) file name.

    If you are a beginner, I was in that stage before as all the others, there might be a problem with the not existing folder name, if your file not created. There is QDir::mkPath, which is slightly different from mkDir. It may help you in code to ensure that you actually can create the file.

    The other thing driving me personally nuts is the stupid folder separator for windows, which is a back slash '\'. However, in the mean time you can easily substitute with a forward slash also for most things in Windows (especially Qt). This also part of some of the suggestions.

    If you have difficulties sometimes it helps for others to post a short section of code and the error message.

  • 0 Votes
    9 Posts
    5k Views
    A

    Thanks to all of you.

    I have no time to dedicate close to a day to implement and test a method to copy files little by little (although there have to be loads of code parts already implemented I'm sure). By the moment, If the user is copying a large list of files and wants to interrupt the process, the process will finish when it finishes to copy the current file at that moment, and will erase the previous files. If the current file is huge, the user is going to wait until the end of the copy.

    We know now the behaviour of the Qt copy process, so in the future if it is a problem for somebody, we will have to implement the buffer...

    Again, thanks!

  • Dynamic Path QFile

    Solved General and Desktop
    3
    0 Votes
    3 Posts
    2k Views
    cxamC

    @mrjj said:

    "/home/pics/" + name"

    Indeed, it worked. Thank you :)

  • 0 Votes
    2 Posts
    2k Views
    kshegunovK

    @weedy
    Hello,

    I know there is a "seek" function in C++, however any examples or snippets on how to use it in QT would be helpful.

    Seek makes sense only for binary files (it operates in multiples of a byte), it has no notion of text, characters or lines, you can't use it. Consider what should happen if the text is encoded in an extensible format with changing character size (in bytes) as UTF-8 is, what do you seek then?
    The only reliable way I know of is simply to copy the file line by line, but just substitute the line you want to change.

    By the way, you possibly could use QSettings to store that data instead of maintaining your own text format.

  • 0 Votes
    5 Posts
    20k Views
    kshegunovK

    Hello,

    my app wasn't able to create an empty file due, for example, to insufficient permissions: shall I call file.close() anyway?

    No you're not required to.

    my app was able to create an empty file: shall I call file.close()

    Again, you're not required to. You can call it if you wish, but by default the QFile instance will close the file when it goes out of scope. Moreover calling close() on an unopened file is permitted, but it does nothing.

    The I want to add some text to the file, but I do not want to interact with it here: I pass the file name to a function which is taking care of it.

    Don't reopen the file on each write, it's a somewhat heavy operation. Have a member variable, open it once and write as much as you'd like.

    Now, because of all the previous checks, I shouldn't have to check if file exists or if I can open it, right?

    Wrong. No one is guaranteeing you that the file exists. On windows a file will (usually) be locked when opened, but on Linux the user can delete it even while you're writing onto it. So don't skip the checks. Also as I mentioned above, don't reopen the file on each write.

    Kind regards.