<?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[Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked?]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I made a QListWidget which contains custom QWidgets.<br />
When I click on a widget of a QListWidgetItem, I'd like to know what's the QListWidgetItem which contains the clicked widget.</p>
<p dir="auto">Here's the piece of code of the custom QWidget:</p>
<p dir="auto">ClickableLabel.h</p>
<pre><code>#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H

#include &lt;QLabel&gt;
#include &lt;QLineEdit&gt;
#include &lt;QWidget&gt;
#include &lt;Qt&gt;

class Model;

class ClickableLabel : public QWidget {
    Q_OBJECT

    Model*model;
    QLabel*label;
    QLineEdit*lineEdit;
    QString prevText;
    bool pressed;

public:
    explicit ClickableLabel(QString text, QWidget* parent=nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    ~ClickableLabel() override;
    void mousePressEvent(QMouseEvent* event) override;
    QLabel* GetLabel() const;
    QLineEdit* GetLineEdit() const;
    QSize sizeHint() const override;

signals:
    void clicked();

public slots:
    void ChangeLabelText();

};

#endif // CLICKABLELABEL_H
</code></pre>
<p dir="auto">ClickableLabel.cpp</p>
<pre><code>#include&lt;clickablelabel.h&gt;
#include&lt;QDebug&gt;
#include "model.h"

ClickableLabel::ClickableLabel(QString text, QWidget* parent, Qt::WindowFlags f) :
    QWidget(parent),
    model(new Model)
{
    pressed = false;

    label = new QLabel(text, this);
    label-&gt;setFont(QFont("Courier", 12));

    lineEdit = new QLineEdit(this);
    lineEdit-&gt;hide();

    prevText = label-&gt;text();

    connect(lineEdit,SIGNAL(returnPressed()),this,SLOT(ChangeLabelText()));
}

ClickableLabel::~ClickableLabel() {}

void ClickableLabel::mousePressEvent(QMouseEvent* event) {

    pressed = !pressed;

    if(pressed)
    {
        label-&gt;hide();
        lineEdit-&gt;show();
        lineEdit-&gt;setFocus();

        if((pos().x() == 0 &amp;&amp; pos().y() == 0) || (pos().x() == 105 &amp;&amp; pos().y() == 0))
        {
            lineEdit-&gt;setGeometry(0,0,100,18);
        }
        else
        {
            lineEdit-&gt;setGeometry(0,0,label-&gt;text().length()*10,18);
        }

        lineEdit-&gt;setPlaceholderText(label-&gt;text());
    }
    else
    {
        ChangeLabelText();
    }

    emit clicked();
}

QLabel* ClickableLabel::GetLabel() const { return label; }

QLineEdit* ClickableLabel::GetLineEdit() const { return lineEdit; }

QSize ClickableLabel::sizeHint() const {

    QSize size(label-&gt;text().length()*10,18);
    return size;
}

void ClickableLabel::ChangeLabelText() {

    if(pressed)
        pressed = !pressed;

    if(lineEdit-&gt;text() != "")
    {
        label-&gt;setText(lineEdit-&gt;text());
        prevText = lineEdit-&gt;text();

        model-&gt;ModifyHero(objectName().toLocal8Bit().constData(),
                          lineEdit-&gt;text().toLocal8Bit().constData());
    }
    else
        label-&gt;setText(prevText);

    label-&gt;show();
    lineEdit-&gt;hide();
}

</code></pre>
<p dir="auto">In order to obtain the wanted result, I thought that I should connect this 2 signals in this way:</p>
<pre><code>connect(ClickableLabel,SIGNAL(clicked()),QListWidget,SIGNAL(itemClicked(QListWidgetItem*)));
</code></pre>
<p dir="auto">But of course, for many reasons, this can't be done.</p>
<p dir="auto">That's why I am asking the if it's possible to know the QListWidgetItem which contains the clicked widget.<br />
Can someone please tell me if what I thought is the right way to obtain what I want or if there's another way to do that?</p>
<p dir="auto">Thank you in advance!</p>
]]></description><link>https://forum.qt.io/topic/99160/is-this-possible-to-know-what-s-the-qlistwidgetitem-which-contains-a-widget-being-clicked</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 08:55:27 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/99160.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Jan 2019 23:22:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 18:02:30 GMT]]></title><description><![CDATA[<p dir="auto">Here's how I made the whole thing "work" (I'm still having a few problems but the code here works perfectly) :</p>
<p dir="auto">This is how to create a ClickableLabel and how to connect it to the slot</p>
<pre><code>ClickableLabel* a = new ClickableLabel("value",parent);
a-&gt;setObjectName("aLabel"); //to let ClickableLabel class understand which instance was clicked
a-&gt;setProperty("myrow",itemCounter); //to instantiate labels with different ObjectName you need to make a counter
connect(a,SIGNAL(clicked()),this,SLOT(ElementClicked()));
</code></pre>
<p dir="auto">This is the slot called when clicking any ClickableLabel (The code which was kindly provided by <a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a>)</p>
<pre><code>void MainWindow::ElementClicked() {

    ClickableLabel*clklbl = qobject_cast&lt;ClickableLabel*&gt;(sender());
    int row = clklbl-&gt;property("myrow").toInt();
    qList-&gt;setCurrentRow(row); //after experimenting a bit, it turned out that setCurrentRow() method is enough
    //model communication here
}
</code></pre>
<p dir="auto">Thank you very much again <a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a>!</p>
]]></description><link>https://forum.qt.io/post/508702</link><guid isPermaLink="true">https://forum.qt.io/post/508702</guid><dc:creator><![CDATA[cppStudent012]]></dc:creator><pubDate>Thu, 31 Jan 2019 18:02:30 GMT</pubDate></item><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 13:32:38 GMT]]></title><description><![CDATA[<p dir="auto">No body is worried. It helps others. Nothing more than that.</p>
]]></description><link>https://forum.qt.io/post/508640</link><guid isPermaLink="true">https://forum.qt.io/post/508640</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Thu, 31 Jan 2019 13:32:38 GMT</pubDate></item><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 13:24:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a><br />
Yeah as soon as I'll make everything work, I'll mark it as solved don't worry :)</p>
]]></description><link>https://forum.qt.io/post/508639</link><guid isPermaLink="true">https://forum.qt.io/post/508639</guid><dc:creator><![CDATA[cppStudent012]]></dc:creator><pubDate>Thu, 31 Jan 2019 13:24:57 GMT</pubDate></item><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 12:39:34 GMT]]></title><description><![CDATA[<p dir="auto">Super. You can move the issue to SOLVED state.</p>
]]></description><link>https://forum.qt.io/post/508628</link><guid isPermaLink="true">https://forum.qt.io/post/508628</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Thu, 31 Jan 2019 12:39:34 GMT</pubDate></item><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 12:12:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a></p>
<p dir="auto">Thank you very much for answering giving also a complete example!</p>
<p dir="auto">I just finished to completely understand what your code does (had to surf the documentation a bit) and now I am gonna try to see if I can make it work.</p>
<p dir="auto">For now, thanks again :)</p>
]]></description><link>https://forum.qt.io/post/508620</link><guid isPermaLink="true">https://forum.qt.io/post/508620</guid><dc:creator><![CDATA[cppStudent012]]></dc:creator><pubDate>Thu, 31 Jan 2019 12:12:05 GMT</pubDate></item><item><title><![CDATA[Reply to Is this possible to know what&#x27;s the QListWidgetItem which contains a widget being clicked? on Thu, 31 Jan 2019 05:42:36 GMT]]></title><description><![CDATA[<p dir="auto">It is possible. Look at the example at <a href="https://github.com/QtDheeru/QtExamples/tree/master/Qt/ListWithWidget" target="_blank" rel="noopener noreferrer nofollow ugc">GIT</a></p>
]]></description><link>https://forum.qt.io/post/508498</link><guid isPermaLink="true">https://forum.qt.io/post/508498</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Thu, 31 Jan 2019 05:42:36 GMT</pubDate></item></channel></rss>