PDA

View Full Version : How to get qlistwidget index and item text in one variable?



Gokulnathvc
25th January 2013, 07:03
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..

Santosh Reddy
25th January 2013, 07:23
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()

Gokulnathvc
25th January 2013, 07:35
Could you explain my scenario with some codes?

Santosh Reddy
25th January 2013, 08:05
Look at this


class Worker : public QObject
{
Q_OBJECT
public:
Worker(QListWidget * left, QListWidget * right, QObject *parent)
: QObject(parent)
, leftList(left)
, rightList(right){}

public slots:
void addItems()
{
QList<QListWidgetItem*> items = leftList->selectedItems();
for(int i = 0; i < items.count(); i++)
{
QListWidgetItem *item = items[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++)
{
QListWidgetItem *item = items[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:
QListWidget * leftList;
QListWidget * rightList;
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QWidget w;

QGridLayout * layout = new QGridLayout(&w);

QListWidget * leftList = new QListWidget(&w);
QListWidget * rightList = new QListWidget(&w);
QPushButton * addButton = new QPushButton(">>", &w);
QPushButton * remButton = new QPushButton("<<", &w);
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++)
{
const QString text = QString("Item - %1").arg(i);
QListWidgetItem * lItem = new QListWidgetItem(text);
QListWidgetItem * rItem = new QListWidgetItem(text);
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"

Gokulnathvc
25th January 2013, 10:14
Thank you so much.. Got confused with the index values.. This works perfect..