Qt: 创建List, 添加删除Item

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);
}


你可能感兴趣的:(Qt: 创建List, 添加删除Item)