{
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"
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"
To copy to clipboard, switch view to plain text mode
Bookmarks