Search code examples
c++qtqlistwidget

Detecting if an item is clicked at at some row in a QlistWidget


I Have been given this simple task ,

I have this list where i instert items whenever ok is clicked,void Form::ok() handle that event is supposed to add new list items to the list.

Now what am not able to do is to detect the if an item is clicked at at some row then do something according to that, this is my code..

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"
#include<QScrollArea>
#include<QScrollBar>

//#include <QgeoPositioninfo.h>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

}
Form::~Form()
{
    delete ui;
}

void Form::ok()
{
    QIcon  mypix  (":/karim/test.png");

    QListWidgetItem* newItem = new QListWidgetItem;
    newItem->setText("pixmix");
    newItem->setIcon(mypix);

    int row = ui->listWidget->row(ui->listWidget->currentItem());
    this->ui->listWidget->insertItem(row, newItem);

    //if(item at row x is clicked)
     {
     //do something
     }
}

Please be specific in yout answer i will appreciate that


Solution

  • Something as below :

    connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemClickedSlot(QListWidgetItem *)));
    
    void Form::itemClickedSlot (QListWidgetItem * itemClicked)
    {
    //Do something with clicked item
    }