Qt 创建List, 添加删除Item

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#ifndef LISTWINDOW_H_

#define LISTWINDOW_H_

#include

class QPushButton;

class QListWidget;

class QWidget;

class ListWindow : public QDialog {

Q_OBJECT

public:

ListWindow(QWidget *parent = 0);

virtual ~ListWindow();

public slots:

void moveLeft();

void moveRight();

private:

QPushButton *moveLeftButton;

QPushButton *moveRightButton;

QListWidget *leftList;

QListWidget *rightList;

};

#endif/**//*LISTWINDOW_H_*/

#include"ListWindow.h"

#include

#include

#include

#include

#include

#include

#include

#include

ListWindow::ListWindow(QWidget *parent) :

QDialog(parent) {

QGridLayout *gridLayout = new QGridLayout();

gridLayout->addWidget(leftList = new QListWidget(), 0, 0, 3, 1);

gridLayout->addWidget(moveLeftButton = new QPushButton("<<"), 0, 1, 1, 1);

gridLayout->addWidget(moveRightButton = new QPushButton(">>"), 1, 1, 1, 1);

gridLayout->addWidget(rightList = new QListWidget(), 0, 2, 3, 1);

QObject::connect(moveLeftButton, SIGNAL(clicked()), this, SLOT(moveLeft()));

QObject::connect(moveRightButton, SIGNAL(clicked()), this, SLOT(moveRight()));

QStringList items;

items << "One" << "Two" << "Three" << "Four" << "Five" << "Sechs"

<< "Sieben" << "One" << "Two" << "Three" << "Four" << "Five"

<< "Sechs" << "Sieben";

leftList->addItems(items);

this->setLayout(gridLayout);

}

ListWindow::~ListWindow() {

}

void ListWindow::moveLeft() {

// if (rightList->currentItem() == 0) {

// return;

// }

//

// QString text = rightList->currentItem()->text();

// delete rightList->currentItem();

// leftList->addItem(text);

// The takeItem() method is used to remove an Item form one list without // having to delete it. TakeItem() no deleting or no creating, more efficient.

if (rightList->selectedItems().count() != 1) {

return;

}

QListWidgetItem *item = rightList->takeItem(rightList->currentRow());

leftList->addItem(item);

}

void ListWindow::moveRight() {

// if (leftList->currentItem() == 0) {

// return;

// }

//

// QString text = leftList->currentItem()->text();

// delete leftList->currentItem();

// rightList->addItem(text);

if (leftList->selectedItems().count() != 1) {

return;

}

QListWidgetItem *item = leftList->takeItem(leftList->currentRow());

rightList->addItem(item);

}

相关文档
最新文档