jeudi 21 juin 2018

Connect QPushButton between two classes

I have a QListWidget that is in a QGridLayout in my constructor of the Draw class.QGridLAyout contains other elements.

Draw::Draw(QWidget *parent):QDialog(parent){

gridlayout=new QGridLayout;
gridlayout->addLayout(hbox,0,0);
gridlayout->addLayout(hbox1,1,0);
gridlayout->addLayout(hbox2,2,0);
gridlayout->addWidget(listWidget,3,0);
gridlayout->addLayout(hbox4,4,0);
this->setLayout(gridlayout);

}

When clicking on a QPushButton. I'm calling a slot that filled the QListWidget.

//SLOT

void Draw::fillListWidget(){
//item is QListWidgetItem
item=new QListWidgetItem;
item->setSizeHint(QSize(0,50));
listWidget->addItem(item);
//WidgetfillListWidget is an other class with parameters for the QListWidget
listWidget->setItemWidget(item,new WidgetfillListWidget(label1,path));

}

In my other class I build the new QWidget to fill the QlistWidget. The QListWidget contains a label, a string and a QPushButton.

WidgetfillListWidget::WidgetfillListWidget(QString label, QString p){

instanceDraw=new Draw(this);
QString name = label;
QString path = p;

name1=new QLabel(name,this);
path1=new QString(path,this);
remove=new QPushButton("remove",this);

hboxlist=new QHBoxLayout;
hboxlist->addWidget(name1);
hboxlist->addWidget(path1);
hboxlist->addWidget(remove);

setLayout(hboxlist);
connect(remove,SIGNAL(clicked()),instanceDraw,SLOT(removeItem()));
}

In the Draw class I have the slot that has to delete an item but it does not work

void Draw::removeItem(){
listWidget->takeItem(listWidget->row(item));
}

Deleting the item does not work. I think it comes from my Draw object in the connect. But I do not understand how to solve the problem. Does someone have an idea?

Aucun commentaire:

Enregistrer un commentaire