Skip to content
  • 0 Votes
    4 Posts
    176 Views
    JonBJ

    @Joachim-W
    I looked at your progress here but have not had an opportunity to examine it further. But here are a couple of points which strike me:

    readLine() should read one line from input. It should not matter whether that comes from terminal (with or without pasting) or redirection. The fact that you seem to say sometimes it reads/returns one line and sometimes multiple lines should not happen. A line is a line. The QTextStream may (or may not) buffer further lines which are available (e.g. from redirection) but readLine() should only return a single line.

    When reading from redirection you can/should be able to read till "end of file", indicating the end of input. But when reading from terminal there is no "end", user could type more at any time, so no "end of file".

    You should not need a timer. One QSocketNotifier notification should be received when there is one or more lines/input available. Process all lines which are available (however you test for that) then exit. That should be it for the redirection case. For the terminal case at some future point a new QSocketNotifier notification should arrive at which point you re-enter and process whatever is available anew. You will need a readLine() call which returns with "empty" when no more input is currently available, or a test which says whether there is any further input available at this point before calling a blocking readLine(). It may be better to call readAll(): that should return all data which is available at that point, I believe, but will not block like readLine() when no further data is there. This will work for redirection too.

  • 0 Votes
    14 Posts
    1k Views
    Christian EhrlicherC

    @Aramir said in QRegExp returning incorrect position due to codec ?:

    ve seen many SD cards/emmc die in ereaders (devices I'm aiming for) therefore I'm trying to lower the write operations on it in order to not reduce their lifetime. That's it I think it's worth the trouble to figure out a solution to this problem instead of creating more and more e-waste.

    Again a useless optimization due to a feeling. Optimize only when you can prove it's a problem and needs to be optimzied.

    You SD-Card has likely a sector size of 512 or 1024 bytes. On top of this your filesystem may use a block size up to 64kb (NTFS, ext4 use 4kb ). So when you even change single byte in your file which is less than the sector size or block size it will write the whole sector/block.
    It's just plain stupid and an over-complicating of things for nothing.

  • 0 Votes
    3 Posts
    672 Views
    Y

    @raven-worx Thank you. I will use UTF-8.

  • 0 Votes
    17 Posts
    3k Views
    M

    Finally I changed reading mode to read directly from the data file. It's most faster and just I was care to encode data when reading to keep special charcatars.

    Thanks all for your ideas

  • 0 Votes
    4 Posts
    964 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
    A

    @Johnx8664 Learning Qt4 at this point would be a waste of your time. Like @SGaist said it's long past it's end of life. Qt5 is 12 major releases into it's life cycle now and 5 years old now.

    I would grab a book on Qt5, or I find it's pretty easy to learn Qt just from the docs that come with it. If you know C++ well (and you should if you are tackling Qt), then you should be fine learning from the Qt Assistant docs. There are a lot of examples and tutorials in there as well.

  • 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

  • 0 Votes
    8 Posts
    2k Views
    SGaistS

    Or, since it's a text file:

    QFile inputFile("C:/Programming/Projects/Folkfriends/combo.txt"); if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) return; while (!inputFile.atEnd()) { QByteArray material = inputFile.readLine(); Material_Combo->addItem (material) ; }

    ?

  • 0 Votes
    4 Posts
    1k Views
    SGaistS

    You're welcome !

    Since it's all good now, please mark the thread as solved using the "Topic Tool" button so other forum users may know a solution has been found :)

    Also, while browsing the forum, consider up-voting the answers that you find useful, that will make them easier to find for other people :)

  • 0 Votes
    2 Posts
    919 Views
    JKSHJ

    Hi,

    Why not use QSettings?

    Anyway, if you want to implement this yourself, you first need to learn about file pointers and how to read and write to the same file at the same time:

    http://www.cplusplus.com/forum/beginner/66306/ http://stackoverflow.com/questions/17536570/reading-and-writing-to-the-same-file-using-the-same-fstream
  • 1 Votes
    7 Posts
    5k Views
    Chris KawaC

    It's a stream. Once you read data the subsequent reads don't "go back".

    ui->textBrowserHTML->setText(in.readAll()); //you read all so the stream is "empty" now ... myString = in.readLine(); //there's nothing in the stream at this point

    Btw. If you just want to read the file as a whole without any parsing or chunking you can skip the stream and readAll() directly from the QFile.