Is this possible to know what's the QListWidgetItem which contains a widget being clicked?
-
Hi!
I made a QListWidget which contains custom QWidgets.
When I click on a widget of a QListWidgetItem, I'd like to know what's the QListWidgetItem which contains the clicked widget.Here's the piece of code of the custom QWidget:
ClickableLabel.h
#ifndef CLICKABLELABEL_H #define CLICKABLELABEL_H #include <QLabel> #include <QLineEdit> #include <QWidget> #include <Qt> 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_HClickableLabel.cpp
#include<clickablelabel.h> #include<QDebug> #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->setFont(QFont("Courier", 12)); lineEdit = new QLineEdit(this); lineEdit->hide(); prevText = label->text(); connect(lineEdit,SIGNAL(returnPressed()),this,SLOT(ChangeLabelText())); } ClickableLabel::~ClickableLabel() {} void ClickableLabel::mousePressEvent(QMouseEvent* event) { pressed = !pressed; if(pressed) { label->hide(); lineEdit->show(); lineEdit->setFocus(); if((pos().x() == 0 && pos().y() == 0) || (pos().x() == 105 && pos().y() == 0)) { lineEdit->setGeometry(0,0,100,18); } else { lineEdit->setGeometry(0,0,label->text().length()*10,18); } lineEdit->setPlaceholderText(label->text()); } else { ChangeLabelText(); } emit clicked(); } QLabel* ClickableLabel::GetLabel() const { return label; } QLineEdit* ClickableLabel::GetLineEdit() const { return lineEdit; } QSize ClickableLabel::sizeHint() const { QSize size(label->text().length()*10,18); return size; } void ClickableLabel::ChangeLabelText() { if(pressed) pressed = !pressed; if(lineEdit->text() != "") { label->setText(lineEdit->text()); prevText = lineEdit->text(); model->ModifyHero(objectName().toLocal8Bit().constData(), lineEdit->text().toLocal8Bit().constData()); } else label->setText(prevText); label->show(); lineEdit->hide(); }In order to obtain the wanted result, I thought that I should connect this 2 signals in this way:
connect(ClickableLabel,SIGNAL(clicked()),QListWidget,SIGNAL(itemClicked(QListWidgetItem*)));But of course, for many reasons, this can't be done.
That's why I am asking the if it's possible to know the QListWidgetItem which contains the clicked widget.
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?Thank you in advance!
-
It is possible. Look at the example at GIT
-
It is possible. Look at the example at GIT
Thank you very much for answering giving also a complete example!
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.
For now, thanks again :)
-
Super. You can move the issue to SOLVED state.
-
Super. You can move the issue to SOLVED state.
@dheerendra
Yeah as soon as I'll make everything work, I'll mark it as solved don't worry :) -
No body is worried. It helps others. Nothing more than that.
-
Here's how I made the whole thing "work" (I'm still having a few problems but the code here works perfectly) :
This is how to create a ClickableLabel and how to connect it to the slot
ClickableLabel* a = new ClickableLabel("value",parent); a->setObjectName("aLabel"); //to let ClickableLabel class understand which instance was clicked a->setProperty("myrow",itemCounter); //to instantiate labels with different ObjectName you need to make a counter connect(a,SIGNAL(clicked()),this,SLOT(ElementClicked()));This is the slot called when clicking any ClickableLabel (The code which was kindly provided by @dheerendra)
void MainWindow::ElementClicked() { ClickableLabel*clklbl = qobject_cast<ClickableLabel*>(sender()); int row = clklbl->property("myrow").toInt(); qList->setCurrentRow(row); //after experimenting a bit, it turned out that setCurrentRow() method is enough //model communication here }Thank you very much again @dheerendra!