Qt学习笔记 ListWidget的增删改

学习了一下ListWidget控件的使用,做一个小功能增删改

先把代码分解最后给出完整代码

在窗体上添加一个ListWidget 一个Horizontal Specer和  三个PushButton

效果如下

Qt学习笔记 ListWidget的增删改

因为构造函数中的SetupUi(this)会自动将符合on_objectName_signalName()命名

的任意槽与相应的objectName的signalName()信号连接到一起

我就直接在.h文件里添加槽的声明 

private slots:

    void on_btn_AddEvent_clicked();

    void on_btn_Del_clicked();

    void on_btn_Modify_clicked();

    void on_lvWid_Source_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);

先在给ListWidget控件添加一些元素

MyElementsView::MyElementsView(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::MyElementsView)

{

    ui->setupUi(this);



    for(int i=0;i<5;i++)

    {

        ui->lvWid_Source->addItem(tr("item : ") + QString::number(i));

    }

}

这样ListWidget就有5个元素了

我们再看一下它的curretnItemChanged事件

void on_lvWid_Source_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);

在这里我们把选中行的字体变为蓝色行背景变为红色

在.cpp里实现 

void MyElementsView::on_lvWid_Source_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)

{

    if(current==Q_NULLPTR)return;

    if(previous!=Q_NULLPTR)

    {

        previous->setBackgroundColor(Qt::transparent);

        previous->setTextColor(Qt::black);

    }

    current->setBackgroundColor(Qt::red);

    current->setTextColor(Qt::blue);

}

效果如下

Qt学习笔记 ListWidget的增删改

添加事件比较简单

在cpp实现

void MyElementsView::on_btn_AddEvent_clicked()

{

    ui->lvWid_Source->addItem(tr("new item : ") +QString::number(ui->lvWid_Source->count()));

}

直接用它自己的addItem方法就可以 

删除有些麻烦

void  MyElementsView::on_btn_Del_clicked()

{

    if(ui->lvWid_Source->currentItem()!=Q_NULLPTR)

    {

        QListWidgetItem * item = ui->lvWid_Source->takeItem(ui->lvWid_Source->currentRow());

        delete item;

    }

}

删除要先把选中的widget取出来再进行删除

修改实现也比较简单

void  MyElementsView::on_btn_Modify_clicked()

{

    if(ui->lvWid_Source->currentItem()!=Q_NULLPTR)

    {

        ui->lvWid_Source->currentItem()->setText(tr("Modify :Item ") +QString::number(ui->lvWid_Source->count()));

    }

}

看一下效果

Qt学习笔记 ListWidget的增删改

完整代码如下

.h

#ifndef MYELEMENTSVIEW_H

#define MYELEMENTSVIEW_H



#include <QDialog>

#include<QListWidgetItem>

namespace Ui {

class MyElementsView;

}



class MyElementsView : public QDialog

{

    Q_OBJECT



public:

    explicit MyElementsView(QWidget *parent = 0);

    ~MyElementsView();



private:

    Ui::MyElementsView *ui;

private slots:

    void on_btn_AddEvent_clicked();

    void on_btn_Del_clicked();

    void on_btn_Modify_clicked();

    void on_lvWid_Source_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);

};



#endif // MYELEMENTSVIEW_H

.cpp完整代码

#include "myelementsview.h"

#include "ui_myelementsview.h"

#include <QToolBar>

#include<QHBoxLayout>

#include<QMainWindow>

#include<QMessageBox>

#include<QList>



MyElementsView::MyElementsView(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::MyElementsView)

{

    ui->setupUi(this);



    for(int i=0;i<5;i++)

    {

        ui->lvWid_Source->addItem(tr("item : ") + QString::number(i));

    }

}





void MyElementsView::on_btn_AddEvent_clicked()

{

    ui->lvWid_Source->addItem(tr("new item : ") +QString::number(ui->lvWid_Source->count()));



    //多选

//    const QList<QListWidgetItem *>   items = ui->lvWid_Source->selectedItems();

//    foreach (QListWidgetItem * item, items) {

//        item->text();

//        item->setTextColor(Qt::blue);

//    }

}



void  MyElementsView::on_btn_Del_clicked()

{

    if(ui->lvWid_Source->currentItem()!=Q_NULLPTR)

    {

        QListWidgetItem * item = ui->lvWid_Source->takeItem(ui->lvWid_Source->currentRow());

        delete item;

    }

}



void  MyElementsView::on_btn_Modify_clicked()

{

    if(ui->lvWid_Source->currentItem()!=Q_NULLPTR)

    {

        ui->lvWid_Source->currentItem()->setText(tr("Modify :Item ") +QString::number(ui->lvWid_Source->count()));

    }

}



MyElementsView::~MyElementsView()

{

    delete ui;

}



void MyElementsView::on_lvWid_Source_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)

{

    if(current==Q_NULLPTR)return;

    if(previous!=Q_NULLPTR)

    {

        previous->setBackgroundColor(Qt::transparent);

        previous->setTextColor(Qt::black);

    }

    current->setBackgroundColor(Qt::red);

    current->setTextColor(Qt::blue);

}

 

 

你可能感兴趣的:(widget)