Qt: 创建List, 添加删除Item

#ifndef LISTWINDOW_H_
#define LISTWINDOW_H_

#include 
< QDialog >

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 
< QPushButton >
#include 
< QListWidget >
#include 
< QWidget >
#include 
< QHBoxLayout >
#include 
< QVBoxLayout >
#include 
< QGridLayout >
#include 
< QStringList >
#include 
< QListWidgetItem >

ListWindow::ListWindow(QWidget 
* parent) :
    QDialog(parent) 
{
    QGridLayout 
*gridLayout = new QGridLayout();
    gridLayout
->addWidget(leftList = new QListWidget(), 0031);
    gridLayout
->addWidget(moveLeftButton = new QPushButton("<<"), 0111);
    gridLayout
->addWidget(moveRightButton = new QPushButton(">>"), 1111);
    gridLayout
->addWidget(rightList = new QListWidget(), 0231);
    
    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);
}


http://www.cppblog.com/biao/archive/2008/03/22/45087.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值