<?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[Use QTreeWidget with non-static data.]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I want to use the QTreeWidget with dynamic data (ie. Insert elements when a button is pressed).<br />
I make this functionality but I haven't had results.<br />
I create a button with the signal, when it's pressed, that creates and inserts a new QTreeWidgetItem into the tree. No results, I even called Update() &amp; Repaint().<br />
Any solution?</p>
]]></description><link>https://forum.qt.io/topic/84122/use-qtreewidget-with-non-static-data</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 18:24:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/84122.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Oct 2017 03:19:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 22:03:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a><br />
Ooh but you found it \o/ \o/ \o/ \o/</p>
<p dir="auto">On first post, in Topic Tool button, you can mark solved</p>
]]></description><link>https://forum.qt.io/post/422117</link><guid isPermaLink="true">https://forum.qt.io/post/422117</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 21 Oct 2017 22:03:54 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 22:00:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> thanks to you I found the problem,<br />
The QWidget paint was not complete, in my QTreeWidget, that was cause errors displaying the content, it only displays the content inserted via Constructor, all after this won't be shown. I was playing with the bad framebuffer....<br />
Sorry about this, and really thank you!<br />
How can I mark this thread as Solved?</p>
]]></description><link>https://forum.qt.io/post/422116</link><guid isPermaLink="true">https://forum.qt.io/post/422116</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 21 Oct 2017 22:00:19 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 21:07:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a><br />
Ok and you are 1000% sure TreePointer points to the UI one ?</p>
<p dir="auto">I cant explain it. seems not right. Also if it triggers the signal and goes into the<br />
insertRow function then unless its wrong model or wrong Tree, it should<br />
work.<br />
Have you tried add a line in Designer and check if the Tree you talk to have 1 item?</p>
]]></description><link>https://forum.qt.io/post/422109</link><guid isPermaLink="true">https://forum.qt.io/post/422109</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 21 Oct 2017 21:07:13 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 20:52:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> my program is single threaded, yes, and the problem is not the Q_OBJECT, that was already added.<br />
I try to emit a signal from PaintGL() and it still doesn't work, is amazing but the <strong>insertRow function is called if I emit a signal, but the QTreeWidget is not updated with the row!!!!! That was my problem at the start of the thread</strong>.<br />
Why cannot I use the insertRow without signals and slots?<br />
If you have a Pointer to the QTreeWidget in the InterfaceRender class this still doesn't work:</p>
<pre><code>class InterfaceRender : public QOpenglFunctions, public QOpenglWidget {
Q_OBJECT
public:
//The pointer to the TreeWidget
QTreeWidget* TreePointer;

//The update function
void PainGL()override {
  //And here insert one row per frame (only to test my case) with my own data
  //Cast the user data to anything, I don't care
    QAbstractItemModel* const mdl= TreePointer-&gt;model();
     const int rowCount =mdl-&gt;rowCount();
     if(mdl-&gt;insertRow(rowCount)){
        qDebug("Row Inserted!");
        if(mdl-&gt;setData(mdl-&gt;index(rowCount,0), UserData-&gt;name))
           qDebug("name Written");
        if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),UserData-&gt;description))
           qDebug("description Written");
     }
  }
}
</code></pre>
<p dir="auto">Explain me, why I can't use this? Imagine that the pointer exists (is set at any point of the program). If you try something like this, the QTreeWidget won't show ANYTHING, it seems that is empty... That was the main problem.</p>
]]></description><link>https://forum.qt.io/post/422108</link><guid isPermaLink="true">https://forum.qt.io/post/422108</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 21 Oct 2017 20:52:26 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 20:25:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a><br />
One question. this is single threaded correct? None of it runs in other thread?<br />
All part of UI ? Its forbidden to alter UI stuff from other treads unless using signals and slots.</p>
<p dir="auto">Also when you say  AFTER the app.exec(), we agree it just means<br />
when app is running ?</p>
<p dir="auto">Why cant u just emit signal from PaintGL() ?<br />
Why must it be function call?</p>
<p dir="auto">But unless there are multiple threads involved, calling the function directly should do it.</p>
<p dir="auto">since InterfaceRender is also a widget, if u add Q_OBJECT it can emit signals.</p>
<p dir="auto">¨so</p>
<pre><code>class InterfaceRender : public QOpenglFunctions, public QOpenglWidget {
Q_OBJECT
public:
void PainGL()override {
  //And here insert one row per frame (only to test my case) with my own data
  emit insertRow(&amp;cubeWithParallaxMapping);
}
</code></pre>
<p dir="auto">just define it as a signals in .h</p>
<p dir="auto">and in</p>
<pre><code>class TreeNodesManager : public QWidget {
Q_OBJECT
public slots:
  void InsertRow(void* UserData)   {...]
</code></pre>
<p dir="auto">then  connect them and it should work.<br />
connect(InterfaceRender, SIGNAL(InsertRow(void* )),TreeNodesManagerPTR, SLOT(InsertRow(void* )), <strong>Qt::QueuedConnection</strong> );</p>
<p dir="auto">Note there might be issue with the cubeWithParallaxMapping since its a void * as QueuedConnection<br />
wants to be able to copy it</p>
]]></description><link>https://forum.qt.io/post/422103</link><guid isPermaLink="true">https://forum.qt.io/post/422103</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 21 Oct 2017 20:25:43 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 19:55:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> ok, I am trying to create a custom Graphic Engine (like Unreal 4), at this moment I have a big structure to use all the OpenGL functionality without the knowledge about it (user interface communicates with the Opengl class).<br />
I have one class that inherits from QOpenGLWidget &amp; QOpenglFunctions.<br />
<strong>The real problem is that I have to insert rows AFTER the app.exec() call</strong>, that means if the user wants to create a simple cube and drag it to the "scene"(the render widget), it has to appear in the QTreeWidget. That is my problem, I don't want the functionality inside this button CreateCube, I want, for example: If the user clicks on the CreateCube button,<em><em>inside the class QOpenglFunction that I have, create a simple function that receives a simple insertRow(void</em> userdata)</em>*.<br />
That seems impossible, the function won't work. And I repeat, I don't pause the updates with an infinite loop.</p>
<p dir="auto">At this moment of the discussion, I have a TreeNodesManager.ui with the correspondent .h that has the function InsertRow(void* UserData), and inside the ui file I have the QTreeWidget pointer.<br />
In the other side I have my InterfaceRender.h that inherits from QOpenglWidget &amp; OpenGL and if I want to test something: I override the PaintGL() function and put this</p>
<pre><code>class InterfaceRender : public QOpenglFunctions, public QOpenglWidget {
public:
 //THE CLASS IS NOT COMPLETE
void PainGL()override {
  //And here insert one row per frame (only to test my case) with my own data
  PointerTo TreeNodesManager-&gt;insertRow(&amp;cubeWithParallaxMapping);
}

}
</code></pre>
<p dir="auto">And here my class that manages the QTreeWidget, keep in mind that the QTreeWidget is in the UI file:</p>
<pre><code>class TreeNodesManager : public QWidget {
public:
  
 TreeNodesManager(QWidget* parent), QWidget(parent) {
    ui-&gt;setupUi(this);
    ui-&gt;treeWidget-&gt;setColumnCount(2);
    ui-&gt;treeWidget-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
    
  }
  //And the fantastic function
  void InsertRow(void* UserData) {
    //Cast the user data to anything, I don't care
    QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
     const int rowCount =mdl-&gt;rowCount();
     if(mdl-&gt;insertRow(rowCount)){
        qDebug("Row Inserted!");
        if(mdl-&gt;setData(mdl-&gt;index(rowCount,0), UserData-&gt;name))
           qDebug("name Written");
        if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),UserData-&gt;description))
           qDebug("description Written");
     }
  }
}
</code></pre>
<p dir="auto"><strong>Look the last function insertData, ALL THE qDEBUG is called</strong>.<br />
Please explain me how can I insert data AFTER the app.exec() is called (at runtime). I try this in the PainGL() function that is called one time per frame and it doesn't work</p>
]]></description><link>https://forum.qt.io/post/422101</link><guid isPermaLink="true">https://forum.qt.io/post/422101</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 21 Oct 2017 19:55:14 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 08:47:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a></p>
<p dir="auto">Hi<br />
The button clicked() is a signal ánd is called internally by<br />
emit Clicked()<br />
one dont call like a normal functions as its a signal and emit must be use.</p>
<p dir="auto"><s>Not sure you can emit it from outside and why would you ?</s><br />
<s>I think its possible with meta calls but i never tried.</s><br />
You can call it like</p>
<pre><code>QMetaObject::invokeMethod(addTreeBtn, "clicked",
   Qt::QueuedConnection,
   Q_ARG(QString, "p1"),
   Q_ARG(QString, "p2"));
</code></pre>
<p dir="auto">But it will say<br />
<strong>QMetaObject::invokeMethod: No such method QPushButton::clicked(QString,QString)<br />
Candidates are:<br />
clicked(bool)<br />
clicked()</strong></p>
<p dir="auto">Only<br />
QMetaObject::invokeMethod(addTreeBtn, "clicked",  Qt::QueuedConnection );<br />
will work.</p>
<p dir="auto">Clicked() do not have parameters so you cannot add new ones as then<br />
the buttons real clicked() could not work. How would it get the parameters?</p>
<p dir="auto">Can I ask what you are trying ? it seems sort of reverse.</p>
<p dir="auto">If you want to be able to call it both from a function and use with a button, simply use<br />
a normal slot function and not a lambda. Then you can both call the slot or let a button do it.<br />
But i repeat. The clicked() signal has no parameters and you cannot just add some.</p>
<p dir="auto">so the void insertRow(QAbstractItemModel* const mdl) seems like a good idea.</p>
<p dir="auto">Also, if you want to use slot &amp; Signals, the main is not a good place as moc only runs on .h files.<br />
(hax included, see last line)</p>
<p dir="auto">But since it causing you so much issues there must be something going on we don't see.</p>
<p dir="auto">i also wondered about</p>
<ul>
<li>if you don't insert the elements using the Signal/Slot this does not work</li>
</ul>
<p dir="auto">So in what way are you trying then ?</p>
<p dir="auto">Also all ways are possible</p>
<pre><code>
void insertRow(QAbstractItemModel* const mdl, const QString&amp; ColData1, const QString&amp; ColData2) {
  const int rowCount = mdl-&gt;rowCount();
  if(mdl-&gt;insertRow(rowCount)) {
    qDebug("Row Inserted!");
    mdl-&gt;setData(mdl-&gt;index(rowCount, 0), ColData1);
    mdl-&gt;setData(mdl-&gt;index(rowCount, 1), ColData2);
  }
}

class SlotHolder : public QObject {
  Q_OBJECT
public slots:
  void AddRowsWithParam(QAbstractItemModel* const mdl, const QString&amp; ColData1, const QString&amp; ColData2) {
    insertRow(mdl, ColData1, ColData2);
  }
};

int main(int argc, char* argv[]) {
  QApplication app(argc, argv);
  QWidget wid;
  QPushButton* addTreeBtn = new QPushButton("Add Row", &amp;wid);
  QTreeWidget* treeWid = new QTreeWidget(&amp;wid);
  treeWid-&gt;setColumnCount(2);
  treeWid-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
  QObject::connect(addTreeBtn, &amp;QPushButton::clicked, treeWid, [treeWid]() {
    insertRow(treeWid-&gt;model(), "From Lambda", "From Lambda");

  });

  insertRow(treeWid-&gt;model(), "FROM MAIN as function", "NO SLOT");

  SlotHolder dummy;
  dummy.AddRowsWithParam(treeWid-&gt;model(), "As function call via slot", "YEAH");

  QVBoxLayout* widlay = new QVBoxLayout(&amp;wid);
  widlay-&gt;addWidget(addTreeBtn);
  widlay-&gt;addWidget(treeWid);
  wid.show();
  return app.exec();
}

#include "main.moc" // HAX TO MAKE MOC RUN ON . CPP run qmake manually

</code></pre>
<p dir="auto"><img src="http://imagizer.imageshack.com/img924/848/OY8HcZ.png" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/422032</link><guid isPermaLink="true">https://forum.qt.io/post/422032</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 21 Oct 2017 08:47:59 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 21 Oct 2017 04:03:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> no, i am not blocking the update. One question, with the <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> code, is there a possibilitty to call via c++ the pushButton-&gt;clicked() function? If you connect the click function to the lambda and then u call PushButton-&gt;clicked() nothing will appear in the tree (supposedly it would have to appear one row).<br />
If this can be, how can I pass user data throw the function, as a parameter? Thank you!!!!</p>
]]></description><link>https://forum.qt.io/post/422029</link><guid isPermaLink="true">https://forum.qt.io/post/422029</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 21 Oct 2017 04:03:21 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Thu, 19 Oct 2017 07:19:22 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">in the main loop of the render for example.</p>
</blockquote>
<p dir="auto">What render do you mean ?</p>
<p dir="auto">Qt is controlled by events which are distributed with this function<br />
return app.exec(); &lt;&lt; loops around here<br />
<a href="http://doc.qt.io/qt-5/qapplication.html#exec" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qapplication.html#exec</a></p>
<p dir="auto">So if you mean it only works if you let the the event loop run, you are correct.<br />
if you make a tight for loop and never leave it, it cannot work.</p>
<p dir="auto">If you create a button</p>
<pre><code>void MainWindow::on_pushButton_clicked()
{
    for(;;);

}

</code></pre>
<p dir="auto">You will have 100% killed the program and it cant move or draw itself.</p>
<p dir="auto">Also its perfectly fine to show the widget first then add Items.</p>
<pre><code>int main(int argc, char *argv[])
{
        QApplication app(argc,argv);
        QWidget wid;
        wid.show(); // show first

        QTreeWidget* treeWid=new QTreeWidget(&amp;wid);
        treeWid-&gt;setColumnCount(2);
        treeWid-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
       for(int i=0;i&lt;10;i++){
            insertRow(treeWid-&gt;model());
        }
        QVBoxLayout* widlay=new QVBoxLayout(&amp;wid);
        widlay-&gt;addWidget(treeWid);

        return app.exec(); // first here it will be drawn anyway
}
</code></pre>
<p dir="auto">Also a slot function is a normal c++ function.<br />
There is no difference.</p>
<p dir="auto">So if you call it insertRow from somewhere and its not working, its either a<br />
plain bug OR you are blocking the event loop by use for/while statement that<br />
never terminates. ( as VRonin says)</p>
<p dir="auto">Inserting rows works outside constructors and member functions used as slot.</p>
<p dir="auto">If you see otherwise, please post the whole code where you call<br />
insertRow and we can try help spot what is going on.</p>
]]></description><link>https://forum.qt.io/post/421679</link><guid isPermaLink="true">https://forum.qt.io/post/421679</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Thu, 19 Oct 2017 07:19:22 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Thu, 19 Oct 2017 00:23:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> take a care that all u did is before the show() function or is inside the Signal/Slot button function that you created.</p>
]]></description><link>https://forum.qt.io/post/421661</link><guid isPermaLink="true">https://forum.qt.io/post/421661</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Thu, 19 Oct 2017 00:23:43 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Wed, 18 Oct 2017 22:59:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> all of this is before the update/draw happens (in the wid.show() function), if you do this while the program is running, in the UPDATE function of the program, this function "insertRow()" won't work. Ok, I will try to explain myself.<br />
Try to call this function in the main loop of the render for example. I don't know if the class MainWindow has an update function and u can override it.</p>
]]></description><link>https://forum.qt.io/post/421657</link><guid isPermaLink="true">https://forum.qt.io/post/421657</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Wed, 18 Oct 2017 22:59:40 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Wed, 18 Oct 2017 18:46:27 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">update function</p>
</blockquote>
<p dir="auto">That's too generic.</p>
<blockquote>
<p dir="auto">You only can update the TreeWidget if you insert rows inside Slot/Signal function</p>
</blockquote>
<p dir="auto">Signals and slots are just terms used by <code>moc</code> that then converts them into pure C++. there is no difference between a slot and a normal method/function:</p>
<pre><code class="language-cpp">#include &lt;QApplication&gt;
#include &lt;QWidget&gt;
#include &lt;QTreeWidget&gt;
#include &lt;QAbstractItemModel&gt;
#include &lt;QVBoxLayout&gt;
void insertRow(QAbstractItemModel* const mdl){
            const int rowCount =mdl-&gt;rowCount();
            if(mdl-&gt;insertRow(rowCount)){
                qDebug("Row Inserted!");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
                    qDebug("Pepedfsdf Written");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
                    qDebug("Descdfsdfsdf Written");
            }
}
int main(int argc, char *argv[])
{
        QApplication app(argc,argv);
        QWidget wid;
        QTreeWidget* treeWid=new QTreeWidget(&amp;wid);
        treeWid-&gt;setColumnCount(2);
        treeWid-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
       for(int i=0;i&lt;10;i++){
            insertRow(treeWid-&gt;model());
        }
        QVBoxLayout* widlay=new QVBoxLayout(&amp;wid);
        widlay-&gt;addWidget(addTreeBtn);
        widlay-&gt;addWidget(treeWid);
        wid.show();
        return app.exec();
}
</code></pre>
<hr />
<p dir="auto">My guess is that you are blocking the event loop somehow</p>
]]></description><link>https://forum.qt.io/post/421633</link><guid isPermaLink="true">https://forum.qt.io/post/421633</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Wed, 18 Oct 2017 18:46:27 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Wed, 18 Oct 2017 18:40:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> from the update function (for example), try it, remember my code (I posted it in this discussion), it does not work. You only can update the TreeWidget if you insert rows inside Slot/Signal function.... try it, really</p>
]]></description><link>https://forum.qt.io/post/421632</link><guid isPermaLink="true">https://forum.qt.io/post/421632</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Wed, 18 Oct 2017 18:40:21 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Wed, 18 Oct 2017 06:38:19 GMT]]></title><description><![CDATA[<p dir="auto">Where do you call <code>InsertElement</code> from?</p>
]]></description><link>https://forum.qt.io/post/421493</link><guid isPermaLink="true">https://forum.qt.io/post/421493</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Wed, 18 Oct 2017 06:38:19 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Tue, 17 Oct 2017 20:12:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> if you try to create a class with the functions</p>
<pre><code>void InsertElement() {
   QAbstractItemModel* const mdl= treeWid-&gt;model();
            const int rowCount =mdl-&gt;rowCount();
            if(mdl-&gt;insertRow(rowCount)){
                qDebug("Row Inserted!");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
                    qDebug("Pepedfsdf Written");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
                    qDebug("Descdfsdfsdf Written");
            }
}
</code></pre>
<p dir="auto">Insert this in any class with an UI file (with the TreeWidget), if you don't insert the elements using the Signal/Slot this does not work</p>
]]></description><link>https://forum.qt.io/post/421411</link><guid isPermaLink="true">https://forum.qt.io/post/421411</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Tue, 17 Oct 2017 20:12:21 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Mon, 16 Oct 2017 07:19:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a> said in <a href="/post/420697">Use QTreeWidget with non-static data.</a>:</p>
<blockquote>
<p dir="auto">If you try to do this for example in the update</p>
</blockquote>
<p dir="auto">That's just an infinite loop.</p>
<p dir="auto">Update inserts a row -&gt; the view detects the row change -&gt; the view calls update -&gt; loop</p>
]]></description><link>https://forum.qt.io/post/421025</link><guid isPermaLink="true">https://forum.qt.io/post/421025</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Mon, 16 Oct 2017 07:19:17 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 14 Oct 2017 01:02:06 GMT]]></title><description><![CDATA[<p dir="auto">It's incredible, it only displays the info if the info is called with any Signal/Slot function. If you try to do this for example in the update of your program (call a function that inserts a new Row) it will not work...  Any idea why? Or any idea doing calling a function in the update for example? Thank you :D</p>
]]></description><link>https://forum.qt.io/post/420697</link><guid isPermaLink="true">https://forum.qt.io/post/420697</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 14 Oct 2017 01:02:06 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Sat, 14 Oct 2017 00:37:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> that code works perfectly, maybe the cause is the Signal &amp; Slot event? I don't know... Anyone can explain to me what's the difference between the <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> code (the insert method) and mine, and why doesn't work the code?<br />
Really thank you <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> :)</p>
]]></description><link>https://forum.qt.io/post/420696</link><guid isPermaLink="true">https://forum.qt.io/post/420696</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Sat, 14 Oct 2017 00:37:13 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 14:54:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ziketcpp">@<bdi>ZiketCPP</bdi></a> said in <a href="/post/420605">Use QTreeWidget with non-static data.</a>:</p>
<blockquote>
<p dir="auto">mix the APIs?</p>
</blockquote>
<p dir="auto">You were using the QTreeItem API, I used the QAbstractItemModel one.</p>
<blockquote>
<p dir="auto">I have the conclusion that the QTreeWidget only admit new rows in the constructor</p>
</blockquote>
<p dir="auto">this clearly can't be. QTreeWidget doesn't even know you are in a constructor.</p>
<p dir="auto">can you try this minimal example?</p>
<pre><code class="language-cpp">
#include &lt;QApplication&gt;
#include &lt;QWidget&gt;
#include &lt;QTreeWidget&gt;
#include &lt;QAbstractItemModel&gt;
#include &lt;QVBoxLayout&gt;
int main(int argc, char *argv[])
{
        QApplication app(argc,argv);
        QWidget wid;
        QPushButton* addTreeBtn=new QPushButton("Add Row",&amp;wid);
        QTreeWidget* treeWid=new QTreeWidget(&amp;wid);
        treeWid-&gt;setColumnCount(2);
        treeWid-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
        QObject::connect(addTreeBtn,&amp;QPushButton::clicked,treeWid,[treeWid](){
            QAbstractItemModel* const mdl= treeWid-&gt;model();
            const int rowCount =mdl-&gt;rowCount();
            if(mdl-&gt;insertRow(rowCount)){
                qDebug("Row Inserted!");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
                    qDebug("Pepedfsdf Written");
                if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
                    qDebug("Descdfsdfsdf Written");
            }
        });
        QVBoxLayout* widlay=new QVBoxLayout(&amp;wid);
        widlay-&gt;addWidget(addTreeBtn);
        widlay-&gt;addWidget(treeWid);
        wid.show();
        return app.exec();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/420606</link><guid isPermaLink="true">https://forum.qt.io/post/420606</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 13 Oct 2017 14:54:06 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 14:44:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> said in <a href="/post/420591">Use QTreeWidget with non-static data.</a>:</p>
<blockquote>
<p dir="auto">Don't mix the APIs</p>
</blockquote>
<p dir="auto">I don't really understand what I did wrong, mix the APIs? Where? I copy-paste your code and still doesn't work. What should I add in addTreeChild to work? <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a>, you put the same code as me. I have the conclusion that the QTreeWidget only admit new rows in the constructor, all after this will not display. So if you have any simple code or project to add items (i.e. when you click a button inserts a new row) please explain me how</p>
]]></description><link>https://forum.qt.io/post/420605</link><guid isPermaLink="true">https://forum.qt.io/post/420605</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Fri, 13 Oct 2017 14:44:31 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 13:30:34 GMT]]></title><description><![CDATA[<p dir="auto">Don't mix the APIs</p>
<pre><code class="language-cpp">#include "treenodesui.h"
#include "ui_treenodesui.h"
#include &lt;QStringList&gt;
#include &lt;QAbstractItemModel&gt;


TreeNodesUI::TreeNodesUI(QWidget *parent) :
   QWidget(parent),
   ui(new Ui::TreeNodesUI)
{
   ui-&gt;setupUi(this);
   ui-&gt;treeWidget-&gt;setColumnCount(2);
   ui-&gt;treeWidget-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
  
   //This is working only in the constructor
   QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
   const int rowCount =mdl-&gt;rowCount();
   if(mdl-&gt;insertRow(rowCount)){
      qDebug("Row Inserted!");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
         qDebug("Pepedfsdf Written");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
         qDebug("Descdfsdfsdf Written");
   }
}

TreeNodesUI::~TreeNodesUI()
{
   delete ui;
}
//Take a look at the comments
//IGNORE THE INPUT PARAMETERS---------------------------------------------------
void TreeNodesUI::addTreeChild(Node *node_to_add, QString name, QString description)
{
   QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
   const int rowCount =mdl-&gt;rowCount();
   if(mdl-&gt;insertRow(rowCount)){
      qDebug("Row Inserted!");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
         qDebug("Pepedfsdf Written");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
         qDebug("Descdfsdfsdf Written");
   }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/420591</link><guid isPermaLink="true">https://forum.qt.io/post/420591</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 13 Oct 2017 13:30:34 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 13:20:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
As I suppose, this code only works in the constructor of my class. I will post the code with the changes done.</p>
<p dir="auto">Header.h</p>
<pre><code>#ifndef TREENODESUI_H
#define TREENODESUI_H

#include &lt;QWidget&gt;
#include &lt;QTreeWidgetItem&gt;
namespace Ui {
   class TreeNodesUI;
}

class TreeNodesUI: public QWidget
{
      Q_OBJECT

   public:
      explicit TreeNodesUI(QWidget *parent = 0);
      ~TreeNodesUI();
      void addTreeChild(Node* node_to_add, QString name, QString description);

   private:
      Ui::TreeNodesUI *ui;
};

#endif // TREENODESUI_H
</code></pre>
<p dir="auto">Source.cpp</p>
<pre><code>#include "treenodesui.h"
#include "ui_treenodesui.h"
#include &lt;QStringList&gt;
#include &lt;QAbstractItemModel&gt;


TreeNodesUI::TreeNodesUI(QWidget *parent) :
   QWidget(parent),
   ui(new Ui::TreeNodesUI)
{
   ui-&gt;setupUi(this);
   ui-&gt;treeWidget-&gt;setColumnCount(2);
   ui-&gt;treeWidget-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
   ui-&gt;treeWidget-&gt;setUpdatesEnabled(true);
   //This is working only in the constructor
   QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
   const int rowCount =mdl-&gt;rowCount();
   if(mdl-&gt;insertRow(rowCount)){
      qDebug("Row Inserted!");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
         qDebug("Pepedfsdf Written");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
         qDebug("Descdfsdfsdf Written");
   }
  //This is working only in the constructor
   QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui-&gt;treeWidget);
   treeItem-&gt;setText(0, "Pepedfsdf");
   treeItem-&gt;setText(1, "Descdfsdfsdf");
}

TreeNodesUI::~TreeNodesUI()
{
   delete ui;
}
//Take a look at the comments
//IGNORE THE INPUT PARAMETERS---------------------------------------------------
void TreeNodesUI::addTreeChild(Node *node_to_add, QString name, QString description)
{
  //ALL INSERTED HERE WILL NOT WORK (its called in some point of the program, I debug this)
   QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
   const int rowCount =mdl-&gt;rowCount();
   if(mdl-&gt;insertRow(rowCount)){
      qDebug("Row Inserted!");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
         qDebug("Pepedfsdf Written");
      if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
         qDebug("Descdfsdfsdf Written");
   }
  
  //Another test
   QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui-&gt;treeWidget);
   treeItem-&gt;setText(0, "Pepedfsdf");
   treeItem-&gt;setText(1, "Descdfsdfsdf");
  //With some probes calling updates functions
   ui-&gt;treeWidget-&gt;repaint();
   QWidget::update();
   repaint();
   return;
}

</code></pre>
]]></description><link>https://forum.qt.io/post/420586</link><guid isPermaLink="true">https://forum.qt.io/post/420586</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Fri, 13 Oct 2017 13:20:40 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 12:20:28 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">QAbstractItemModel* const mdl= ui-&gt;treeWidget-&gt;model();
const int rowCount =mdl-&gt;rowCount();
if(mdl-&gt;insertRow(rowCount))){
qDebug("Row Inserted!");
if(mdl-&gt;setData(mdl-&gt;index(rowCount,0),"Pepedfsdf"))
qDebug("Pepedfsdf Written");
if(mdl-&gt;setData(mdl-&gt;index(rowCount,1),"Descdfsdfsdf"))
qDebug("Descdfsdfsdf Written");
}
</code></pre>
]]></description><link>https://forum.qt.io/post/420574</link><guid isPermaLink="true">https://forum.qt.io/post/420574</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 13 Oct 2017 12:20:28 GMT</pubDate></item><item><title><![CDATA[Reply to Use QTreeWidget with non-static data. on Fri, 13 Oct 2017 12:00:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a><br />
Yes, in the constructor I use this<br />
CONTRUCTOR:</p>
<pre><code>ui-&gt;setupUi(this);
ui-&gt;treeWidget-&gt;setColumnCount(2);
ui-&gt;treeWidget-&gt;setHeaderLabels(QStringList() &lt;&lt; "Name" &lt;&lt; "Type");
</code></pre>
<p dir="auto">Function Insert item</p>
<pre><code>//I set this item as header in the tree widget
QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui-&gt;treeWidget);
treeItem-&gt;setText(0, "Pepedfsdf");
treeItem-&gt;setText(1, "Descdfsdfsdf");
</code></pre>
<p dir="auto">The real question is if I can insert, in any time, elements into QTreeWidget and display it, because it seems that I can't. Maybe I have to call a special function to update the tree or anything, i don't know...</p>
]]></description><link>https://forum.qt.io/post/420573</link><guid isPermaLink="true">https://forum.qt.io/post/420573</guid><dc:creator><![CDATA[ZiketCPP]]></dc:creator><pubDate>Fri, 13 Oct 2017 12:00:08 GMT</pubDate></item></channel></rss>