QTカスタムQLIstWidgetItemクリックして信号を出し、どのitemか知っています

2561 ワード

 
 
       :
     QListWidget  QListWidgetItem                    item list       ;
QListWidgetItem          

   

/*     list  */
void ImageProcessor::showLeftDown()
{
	
	clearResult(0);
	for (int i=0;isetIndex(i);
		QListWidgetItem *listItem1 = new QListWidgetItem();
		
		listItem1->setSizeHint(QSize(150, 215));
		
		ui.listWidgetLeftBottom->addItem(listItem1);
		ui.listWidgetLeftBottom->setItemWidget(listItem1, pItem);//      widget
		connect(pItem, SIGNAL(listItemClicked(int)), this, SLOT(clickedLeftItem(int)));//               ;  
	}
}



/*   list item         */
void ImageProcessor::clickedLeftItem(int index)
{
 
   Operator selectedoper=operatorList.at(index);
   selectItem = operatorList.at(index);
   showLeftUp(selectedoper);
   showRightTop(selectedoper);


}

 
 

 
MyListWidgetItem.cpp

#include "mylistwidgetitem.hpp"

MyListWidgetItem::MyListWidgetItem(QImage image, QString string)
{
	ui.setupUi(this);
	ui.ImageLabel->setFixedHeight(200);
	ui.ImageLabel->setFixedWidth(150);
	ui.textLabel->setFixedWidth(150);
	ui.textLabel->setFixedHeight(15);
	ui.ImageLabel->setPixmap(QPixmap::fromImage(image).scaled(QSize(150, 200)));
	ui.textLabel->setText(string);
	ui.ImageLabel->setAlignment(Qt::AlignTop);
	ui.ImageLabel->setAlignment(Qt::AlignLeft);

	
	
}

void MyListWidgetItem::setIndex(int ind)
{
	this->index = ind;
}
int MyListWidgetItem::getIndex()
{
	return index; 
}


void MyListWidgetItem::mousePressEvent(QMouseEvent *event)
{

	if (event->button() == Qt::LeftButton)
	{
		emit this->listItemClicked(index);
	}

	


}


MyListWidgetItem::~MyListWidgetItem() 
{
	
}
MyListWidgetItem .hpp:
#ifndef MYLISTWIDGETITEM_H
#define MYLISTWIDGETITEM_H
#include 
#include 
#include 
#include 
#include 
#include 
#include "ui_MyListWidgetItem.h"
class MyListWidgetItem : public QWidget
 {
Q_OBJECT
public:MyListWidgetItem(QImage image, QString string);
~MyListWidgetItem();
void setIndex(int ind);
int getIndex();
void mousePressEvent(QMouseEvent *event);
signals:void listItemClicked(int);
private:Ui::MyListWidgetItem ui;
int index;
};
#endif