<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Qt creator Update text file with input data]]></title><description><![CDATA[<p dir="auto">Hello all so I have a text file that I can upload to a listWidget along that Im able to add an item and delete an item. My problem is to have the selected row be updated with extra string data, say I click on mayo and i want to add 10 (mayo 10) and update it to my text file.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/882623b4-6e4f-4052-a236-aa65178acb78.png" alt="Screenshot 2024-07-25 104756.png" class=" img-fluid img-markdown" /></p>
<pre><code>void all::on_pushButton_clicked()
{

//update listwidget
    QFile file3("C:\\Users\\morta\\QT Projects\\add_delete\\prac.txt");
    file3.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
    QTextStream in3(&amp;file3);

    QString s;

    //Detele item
    foreach (QListWidgetItem *NAME, ui-&gt;listWidget-&gt;selectedItems())
    {
         ui-&gt;listWidget-&gt;takeItem(ui-&gt;listWidget-&gt;row(NAME)); //grabbed the line of text i need
        QString line2 = ui-&gt;extraline-&gt;text();   //the number of items text
        qDebug()&lt;&lt;line2;
    }

    //Update the text file
    auto numRows = ui-&gt;listWidget-&gt;count();
    for(int row = 0; row &lt; numRows; ++row)
    {
        auto item = ui-&gt;listWidget-&gt;item(row);
        s = item-&gt;text();
        file3.resize(0);   // Deletes all data from the textfile
        in3&lt;&lt;s;            //adds row to text file
        in3&lt;&lt;"\n";       //adds a line break
    }
    file3.close();
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/157909/qt-creator-update-text-file-with-input-data</link><generator>RSS for Node</generator><lastBuildDate>Sun, 18 Oct 2026 07:58:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157909.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Jul 2024 16:50:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 11:44:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a><br />
You (attempt to) "truncate" the file you are writing to as you write each line to it!</p>
<p dir="auto">You have a stream named <code>in3</code> which is only used for output.</p>
<p dir="auto">If there are no lines now in the widget you fail to truncate/clear out any lines which were in the file (e.g. after deleting).</p>
<p dir="auto">Etc.</p>
<p dir="auto">It makes no sense.  I leave it to you.</p>
]]></description><link>https://forum.qt.io/post/805988</link><guid isPermaLink="true">https://forum.qt.io/post/805988</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 28 Jul 2024 11:44:51 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 10:43:24 GMT]]></title><description><![CDATA[<p dir="auto">Yes i can assure you this mini project works 100%, i save every row in to QString s, then delete the entire text file and add s back row by row. Append is a bit confusing for me but reading old post (forgot to save) I read its best to add it.</p>
<p dir="auto">(<a href="https://ddgobkiprc33d.cloudfront.net/6e977f62-7100-4a2e-93c0-bbea3b949e80.png" target="_blank" rel="noopener noreferrer nofollow ugc">https://ddgobkiprc33d.cloudfront.net/6e977f62-7100-4a2e-93c0-bbea3b949e80.png</a>) <img src="https://ddgobkiprc33d.cloudfront.net/e912bc85-bfb7-4174-8bb0-f8d9d82d2f03.png" alt="Screenshot 2024-07-28 043538.png" class=" img-fluid img-markdown" /> <img src="https://ddgobkiprc33d.cloudfront.net/117a7d99-9fb6-4545-b985-66bd0aa2317a.png" alt="Screenshot 2024-07-28 043557.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/805983</link><guid isPermaLink="true">https://forum.qt.io/post/805983</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Sun, 28 Jul 2024 10:43:24 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 10:36:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a><br />
Is that supposed to be an answer to me?  Have you verified it works when there are multiple items and you save to file?  Whatever you see or find that <code>QFile::resize(0)</code> does why in the world do you call it for each line/row written??  And I find it very confusing that you open <code>QIODevice::ReadWrite | QIODevice::Append</code>: why open for append if you don't intend to append but rather overwrite the whole file?  Never seen code written like this.</p>
]]></description><link>https://forum.qt.io/post/805981</link><guid isPermaLink="true">https://forum.qt.io/post/805981</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 28 Jul 2024 10:36:01 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 10:32:07 GMT]]></title><description><![CDATA[<p dir="auto">not sure if you need the other button to add an item but here:</p>
<pre><code>void MainWindow::on_add_clicked()
{
    QString fname1st = ("prac3.txt");
    QFile file1st(fname1st);
    if(file1st.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
    {
        QTextStream stream(&amp;file1st);
        QString file1st = ui-&gt;lineEdit_2-&gt;text();
        stream &lt;&lt; file1st &lt;&lt;"\n";
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/805980</link><guid isPermaLink="true">https://forum.qt.io/post/805980</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Sun, 28 Jul 2024 10:32:07 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 10:22:54 GMT]]></title><description><![CDATA[<p dir="auto">sure so in my header i created a pointer (int *amount).</p>
<p dir="auto">on my code i used:</p>
<pre><code>void MainWindow::on_pushButton_2_clicked()
{
//update listwidget
        QFile file3("prac3.txt");
        file3.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
        QTextStream in3(&amp;file3);

        QString line1 = ui-&gt;comboBox-&gt;currentText();
        QString amount = ui-&gt;lineEdit-&gt;text();   //number of item text
//detele old item data
        QString search(line1);
        QList&lt;QListWidgetItem *&gt; list = ui-&gt;listWidget-&gt;findItems(line1, Qt::MatchContains);
        for ( QListWidgetItem *item : list )
        delete item;
//add updated item with amount
        QStringList stringList;
        stringList &lt;&lt; line1+"\t"+amount;
        ui-&gt;listWidget-&gt;addItems(stringList);
//Update the text file
        auto numRows = ui-&gt;listWidget-&gt;count();
        for(int row = 0; row &lt; numRows; ++row)
        {
            auto item = ui-&gt;listWidget-&gt;item(row);
            QString s;
            s = item-&gt;text();
            file3.resize(0);   // Delets all data from the textfile
            in3&lt;&lt;s;            //adds row to text file
            in3&lt;&lt;"\n";       //adds a line break
        }
        file3.close();
 }
</code></pre>
]]></description><link>https://forum.qt.io/post/805978</link><guid isPermaLink="true">https://forum.qt.io/post/805978</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Sun, 28 Jul 2024 10:22:54 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 07:44:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a><br />
I'd love to know how your code correctly writes all the lines/rows to a file, considering the placement of your <code>file3.resize(0); // Delets all data from the textfile</code>.</p>
]]></description><link>https://forum.qt.io/post/805970</link><guid isPermaLink="true">https://forum.qt.io/post/805970</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 28 Jul 2024 07:44:01 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sun, 28 Jul 2024 01:56:05 GMT]]></title><description><![CDATA[<p dir="auto">solved it myself:<br />
foreach (QListWidgetItem *NAME, ui-&gt;listWidget-&gt;selectedItems())<br />
{<br />
delete ui-&gt;listWidget-&gt;takeItem(ui-&gt;listWidget-&gt;row(NAME));<br />
QStringList stringList;<br />
stringList &lt;&lt; NAME-&gt;text()+"\t"+line2;<br />
ui-&gt;listWidget-&gt;addItems(stringList);<br />
}</p>
<p dir="auto">//Update the text file<br />
auto numRows = ui-&gt;listWidget-&gt;count();<br />
for(int row = 0; row &lt; numRows; ++row)<br />
{<br />
auto item = ui-&gt;listWidget-&gt;item(row);<br />
s = item-&gt;text();<br />
file3.resize(0);   // Delets all data from the textfile<br />
in3&lt;&lt;s;            //adds row to text file<br />
in3&lt;&lt;"\n";       //adds a line break<br />
}<br />
file3.close();</p>
]]></description><link>https://forum.qt.io/post/805963</link><guid isPermaLink="true">https://forum.qt.io/post/805963</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Sun, 28 Jul 2024 01:56:05 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Sat, 27 Jul 2024 19:31:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> , thanks for helping i had modified the code and i can edit but its not what im trying for, i forgot to show on my end that i have a line editbox where i need to type a number and after pressing the button it will add on to the selected item</p>
]]></description><link>https://forum.qt.io/post/805949</link><guid isPermaLink="true">https://forum.qt.io/post/805949</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Sat, 27 Jul 2024 19:31:27 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Thu, 25 Jul 2024 20:50:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a> I have answered and given you the link.  You set flags on the items in the list widget, not the list widget itself.</p>
]]></description><link>https://forum.qt.io/post/805808</link><guid isPermaLink="true">https://forum.qt.io/post/805808</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2024 20:50:43 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Thu, 25 Jul 2024 20:20:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> yes sorry i was in a rush to head to my class but i tried both edit and enable but eitherway listWidgets does not have a setFlags</p>
]]></description><link>https://forum.qt.io/post/805806</link><guid isPermaLink="true">https://forum.qt.io/post/805806</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Thu, 25 Jul 2024 20:20:40 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Thu, 25 Jul 2024 20:07:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a><br />
Why did you try <code>ItemIsEnabled</code> when you need <code>ItemIsEditable</code>?<br />
Per the documentation, which you need to consult, and the example you set flags on individual items via <a href="https://doc.qt.io/qt-6/qlistwidgetitem.html#setFlags" target="_blank" rel="noopener noreferrer nofollow ugc">void QListWidgetItem::setFlags(Qt::ItemFlags flags)</a>.</p>
]]></description><link>https://forum.qt.io/post/805805</link><guid isPermaLink="true">https://forum.qt.io/post/805805</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2024 20:07:47 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Thu, 25 Jul 2024 19:54:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
I tried: ui-&gt;listWidget-&gt;setFlags(Qt::ItemFlag::ItemIsEnabled); but Class QListWidgets has no setFlags in it.</p>
]]></description><link>https://forum.qt.io/post/805804</link><guid isPermaLink="true">https://forum.qt.io/post/805804</guid><dc:creator><![CDATA[Ruben12313]]></dc:creator><pubDate>Thu, 25 Jul 2024 19:54:10 GMT</pubDate></item><item><title><![CDATA[Reply to Qt creator Update text file with input data on Thu, 25 Jul 2024 17:37:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ruben12313">@<bdi>Ruben12313</bdi></a><br />
<a href="https://doc.qt.io/qt-6/qlistwidget.html#editItem" target="_blank" rel="noopener noreferrer nofollow ugc">QListWidget::editItem(QListWidgetItem *item)</a> is a call to edit (the text of) an item.  To edit an item "in place" you need to set its flags to include <code>ItemIsEditable</code>.There's an example in Python at <a href="https://stackoverflow.com/questions/47962839/how-to-make-qlistwidget-items-editable" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/47962839/how-to-make-qlistwidget-items-editable</a>, doubtless there are others in C++, like old forum post here <a href="https://forum.qt.io/topic/8928/solved-making-qlistwidget-item-as-editable-from-qlistwidget-context-menu">https://forum.qt.io/topic/8928/solved-making-qlistwidget-item-as-editable-from-qlistwidget-context-menu</a>.</p>
<p dir="auto">Whether you add, delete or modify, to update the text file you need to rewrite the whole thing, whenever you wish to do so.  You need to get rid of whatever you are trying to do in your code with <code>resize()</code>.  You need to close your file after reading it in, and open (with truncate/overwrite) any time you want to write all the current data out (and of course close it when done writing it).  That is how text files are updated.</p>
]]></description><link>https://forum.qt.io/post/805794</link><guid isPermaLink="true">https://forum.qt.io/post/805794</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2024 17:37:05 GMT</pubDate></item></channel></rss>