How to get qlistwidget index and item text in one variable?
I have two list widgets , one has file lists.. and other will show the selected list from first widget when >> button is pressed and it removes the moved item from first list. Now I would like to multi select the files and move it to the second list. Also, there index should be saved with text in a variable. When the selected item
is removed from second list when << button is pressed. It should place the items to the particular index where the files were originally present in first list before moving to second list..
Re: How to get qlistwidget index and item text in one variable?
Storing the index is not a good idea and is discouraged to use. Instead hide() the selected items and show() them back and when required.
Edit: The API for hide/show item is setHidden()
Re: How to get qlistwidget index and item text in one variable?
Could you explain my scenario with some codes?
Re: How to get qlistwidget index and item text in one variable?
Look at this
Code:
{
Q_OBJECT
public:
, leftList(left)
, rightList(right){}
public slots:
void addItems()
{
QList<QListWidgetItem*> items = leftList->selectedItems();
for(int i = 0; i < items.count(); i++)
{
item->setHidden(true);
QList<QListWidgetItem*> rItems = rightList->findItems(item->text(), Qt::MatchExactly);
for(int j = 0; j < rItems.count(); j++)
rItems[j]->setHidden(false);
}
}
void remItems()
{
QList<QListWidgetItem*> items = rightList->selectedItems();
for(int i = 0; i < items.count(); i++)
{
item->setHidden(true);
QList<QListWidgetItem*> lItems = leftList->findItems(item->text(), Qt::MatchExactly);
for(int j = 0; j < lItems.count(); j++)
lItems[j]->setHidden(false);
}
}
private:
};
int main(int argc, char **argv)
{
Worker * worker = new Worker(leftList, rightList, &w);
leftList->setSelectionMode(leftList->ExtendedSelection);
rightList->setSelectionMode(leftList->ExtendedSelection);
layout->addWidget(leftList, 0, 0, 4, 1);
layout->addWidget(addButton, 1, 1, 1, 1);
layout->addWidget(remButton, 2, 1, 1, 1);
layout->addWidget(rightList, 0, 2, 4, 1);
// Add Sample Items
for(int i = 0; i < 15; i++)
{
leftList->addItem(lItem);
rightList->addItem(rItem);
lItem->setHidden(false);
rItem->setHidden(true);
}
worker->connect(addButton, SIGNAL(clicked()), SLOT(addItems()));
worker->connect(remButton, SIGNAL(clicked()), SLOT(remItems()));
w.setLayout(layout);
w.show();
return app.exec();
}
#include "Main.moc"
Re: How to get qlistwidget index and item text in one variable?
Thank you so much.. Got confused with the index values.. This works perfect..