<?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[Can&#x27;t derive from a QListWidgetItem so I can extend it&#x27;s capabilities.]]></title><description><![CDATA[<p dir="auto">Hello!<br />
I am a QT beginner so my question is rather simple.<br />
Please let me paste in the code first and then I'll describe what my problem is.<br />
So this is my mainwindow.cpp:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    ui-&gt;splitter-&gt;setStretchFactor(0, 3);
    ui-&gt;splitter-&gt;setStretchFactor(1, 7);
    TabSelect item("tab");
    ui-&gt;tabsList-&gt;addItem(item);
}

MainWindow::~MainWindow()
{
    delete ui;
}
</code></pre>
<p dir="auto">and this is my tabselect.h:</p>
<pre><code>#ifndef TABSELECT_H
#define TABSELECT_H

#include &lt;QListWidgetItem&gt;

class TabSelect : public QListWidgetItem
{
    Q_OBJECT
public:
    std::string tabName;
    int participants;
    std::string leadingClub;
    TabSelect(std::string tabName);
    void update();
};

#endif // TABSELECT_H
</code></pre>
<p dir="auto">and tabselect.cpp:</p>
<pre><code>#include "tabselect.h"
#include &lt;string&gt;

TabSelect::TabSelect(std::string tabName)
{
    this-&gt;tabName = tabName;
    this-&gt;participants = 0;
    this-&gt;leadingClub = "";
    this-&gt;update();
}


void TabSelect::update()
{
    std::string content;
    content = "&lt;b&gt;tabName&lt;/b&gt;\n";
    content +="&lt;i&gt;Uczestnicy: " + std::to_string(participants) + "&lt;/i&gt;";
    content += "\n&lt;i&gt;Klub prowadzący: " + leadingClub + "&lt;/i&gt;";
    this-&gt;QListWidgetItem::setText(QString::fromStdString(content));
}

</code></pre>
<p dir="auto">So my problem is, I can not do this line:</p>
<pre><code>ui-&gt;tabsList-&gt;addItem(item);
</code></pre>
<p dir="auto">because QListWidget expects QListWidgetItem, not my derived class. Is there a way to make it work as I would like?</p>
]]></description><link>https://forum.qt.io/topic/80166/can-t-derive-from-a-qlistwidgetitem-so-i-can-extend-it-s-capabilities</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 14:58:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/80166.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Jun 2017 12:06:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Can&#x27;t derive from a QListWidgetItem so I can extend it&#x27;s capabilities. on Sat, 10 Jun 2017 20:11:33 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">There's no problem in using QListWidgetItem subclasses. Your current problem is that you are trying to pass an object of your TabSelect class, take a look again at the signature of the function, the item are allocated on the heap not on the stack.</p>
]]></description><link>https://forum.qt.io/post/398693</link><guid isPermaLink="true">https://forum.qt.io/post/398693</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 10 Jun 2017 20:11:33 GMT</pubDate></item></channel></rss>