Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on the weird use of 'a', I'm guessing that there's a bunch of other code that you didn't post. As it stands now, the loop is equivalent to <code>for (int i = 0; i &lt; 2; ++i)</code>, and thus only iterates through i=0 and i=1. On the first pass, it creates a new QString, assigns the address to the bugname pointer, then attempts (incorrectly) to assign a string to it. You can change the second line to <code>*BugName = QString::number(i);</code> and it should work. Even better, you can use one of the QString constructors and change the first line to <code>QString* BugName = new QString(QString::number(i))</code> and eliminate the second line altogether.</p> <p>Now for the second problem, your memory leak. After the first pass, the loop resets and you create a second qstring, overwritting bugname with the new address. Unfortunately, this orphans the first qstring you made causing a leak. Add the line <code>delete BugName;</code> just before the end of the loop to fix. If you're actually trying to create an array of 'BugNames' for use after the loop, you probably want to use an array instead: Put <code>QString BugNames[2]</code> before the loop, then use the loop to iterate through and initialize them individually via <code>BugNames[i] = QString::number[i]</code>. Since arrays are automatic variables, you won't need the <code>delete</code> part for this latter case.</p> <p><strong>Edit</strong> Looking at your revised code, I would do it this way (old way still valid though):</p> <pre><code>for(int i=0; i &lt; bugModel-&gt;rowCount(); i++){ qDebug() &lt;&lt; i; QString BugName = QString::number(i); setting.beginGroup(BugName.toAscii()); bugModel-&gt;setData(bugModel-&gt;index(i,0), setting.value("theBugName", "A Bug!").toString()); setting.endGroup(); } </code></pre> <p>Note that your use of 'a' is redundant, since it (and indeed the loop itself) would likely be removed by the compiler. Now that I see what you're doing, I'd ditch the pointer-strings altogether and just go with a single automatic variable. As for the problem of it not working, qDebug should be outputting "0 1", yes? You can try <code>qDebug() &lt;&lt; BugName;</code> after the <code>::number</code> assignment, but I suspect that will output "0 1" as well. That would mean that your real issue lies somewhere in the <code>begin/endGroup()</code> functions, and not in this loop. If the argument to <code>beginGroup</code> gets assigned to something static, you might be overwritting a value with the second pass.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload