Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is this possible to know what's the QListWidgetItem which contains a widget being clicked?
Qt 6.12 LTS is out! See what's new in the release blog

Is this possible to know what's the QListWidgetItem which contains a widget being clicked?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cppStudent012C Offline
    cppStudent012C Offline
    cppStudent012
    wrote on last edited by
    #1

    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_H
    

    ClickableLabel.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!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      It is possible. Look at the example at GIT

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      cppStudent012C 1 Reply Last reply
      2
      • dheerendraD dheerendra

        It is possible. Look at the example at GIT

        cppStudent012C Offline
        cppStudent012C Offline
        cppStudent012
        wrote on last edited by
        #3

        @dheerendra

        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 :)

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #4

          Super. You can move the issue to SOLVED state.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          cppStudent012C 1 Reply Last reply
          0
          • dheerendraD dheerendra

            Super. You can move the issue to SOLVED state.

            cppStudent012C Offline
            cppStudent012C Offline
            cppStudent012
            wrote on last edited by
            #5

            @dheerendra
            Yeah as soon as I'll make everything work, I'll mark it as solved don't worry :)

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
              wrote on last edited by
              #6

              No body is worried. It helps others. Nothing more than that.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              https://www.pthinks.com

              1 Reply Last reply
              0
              • cppStudent012C Offline
                cppStudent012C Offline
                cppStudent012
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved