Qt之QListWidget实战

介绍

QListWidget是一个与QListView类似的列表视图类。
作为Qt常用的视图类,它提供了很多简便的函数,当项大于窗口高度时,会自动弹出滑动条。方便对列表中的项进行展示和操作。


1、以下是QListWidget的

添加项、删除项、清空列表、
重命名,右键菜单弹出,双击事件

等功能的实现。

重命名效果

所有代码

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QListWidget>
#include <QLineEdit>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void addListItem();
    void deleteListItem();
    void clearListItem();
    void changeListItemName();
    void finishEdit();
    void onCustomContextMenuRequested(const QPoint &pos);
    void onListWidgetItemDblClicked(QListWidgetItem *item);

private:
    int getCurrentIndexItem(QListWidgetItem * item);
    void startEdit(QListWidgetItem * item);
    bool checkItemSameName(const QString &name);

private:
    QListWidget *m_listWidget;
    QLineEdit *m_renameEdit;
    int m_cnt;
};
#endif // WIDGET_H

cpp文件

#include "widget.h"
#include <QMenu>
#include <QAction>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    m_listWidget = new QListWidget;
    m_renameEdit = new QLineEdit(m_listWidget);
    m_renameEdit->hide();

    QVBoxLayout *vLayout = new QVBoxLayout;

    vLayout->setSpacing(0);
    vLayout->setMargin(0);
    m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu);;
    vLayout->addWidget(m_listWidget);
    setLayout(vLayout);
    connect(m_listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
            this, SLOT(onListWidgetItemDblClicked(QListWidgetItem *)));
    connect(m_renameEdit, SIGNAL(editingFinished()), this, SLOT(finishEdit()));
    connect(m_listWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
            this, SLOT(onCustomContextMenuRequested(const QPoint &)));
    this->setMinimumSize(300,500);
}

Widget::~Widget()
{
}

void Widget::addListItem()
{
    QListWidgetItem *pItem = new QListWidgetItem;
    if (pItem) {
        m_cnt++;
        pItem->setText(QString::number(m_cnt));
         m_listWidget->addItem(pItem);
         m_listWidget->setCurrentItem(pItem);
    }
}
void Widget::deleteListItem()
{
    QListWidgetItem *curItem =  m_listWidget->currentItem();

    delete curItem;
    curItem = nullptr;
}
void Widget::clearListItem()
{
    m_cnt = 0;
    m_listWidget->clear();
     m_renameEdit->hide();
}
void Widget::changeListItemName()
{
    QListWidgetItem *curItem =  m_listWidget->currentItem();
    if (curItem)
    {
         m_renameEdit->setText(curItem->text());
    }
     m_renameEdit->show();
     m_renameEdit->selectAll();
     m_renameEdit->setFocus();
}
void Widget::finishEdit()
{
    QListWidgetItem *curItem =  m_listWidget->currentItem();
    QString oldName = curItem->text();
    if (!checkItemSameName(oldName))
    {
        curItem->setText( m_renameEdit->text());
         m_renameEdit->hide();
    }
    else if ( m_renameEdit->text() == oldName)
    {
         m_renameEdit->hide();
        return;
    }
    else
    {
        curItem->setText(oldName);
        if ( m_renameEdit->isVisible())
        {
             m_renameEdit->hide();
        }
    }
}
void Widget::onCustomContextMenuRequested(const QPoint &pos)
{
    QListWidgetItem *item =  m_listWidget->itemAt(pos);
    if (!item)
    {
        //action
        QMenu *menu = new QMenu(this);
        QAction *newAct = new QAction(tr("New"), this);
        menu->addAction(newAct);

        QAction *clearAct = new QAction(tr("Clear"), this);
        menu->addAction(clearAct);

        connect(clearAct, SIGNAL(triggered()), this, SLOT(clearListItem()));
        connect(newAct, SIGNAL(triggered()), this, SLOT(addListItem()));
        menu->exec(QCursor::pos());
        return;
    }

    startEdit(item);

    QMenu *menu = new QMenu(this);
    QAction *deleteAct = new QAction("Delete", this);
    menu->addAction(deleteAct);

    QAction *renameAct = new QAction("Rename", this);
    menu->addAction(renameAct);

    connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteListItem()));
    connect(renameAct, SIGNAL(triggered()), this, SLOT(changeListItemName()));

    menu->exec(QCursor::pos());
}

void Widget::onListWidgetItemDblClicked(QListWidgetItem *item)
{
    startEdit(item);
    changeListItemName();
}

int Widget::getCurrentIndexItem(QListWidgetItem * item)
{
    int curIndex = 0;
    for (int i = 0; i <  m_listWidget->count(); i++) {
        if (item == m_listWidget->item(i))
        {
            curIndex = i;
            break;
        }
    }
    return curIndex;
}
void Widget::startEdit(QListWidgetItem * item)
{
    if (nullptr == item) return;

    QRect rect =  m_listWidget->visualItemRect(item);
     m_renameEdit->setFixedHeight(rect.height());
     m_renameEdit->move(rect.x(), rect.y());
}
bool Widget::checkItemSameName(const QString &name)
{
    bool bIsSame = false;
    for (int i = 0; i <  m_listWidget->count(); i++)
    {
        if ( m_listWidget->item(i)->text() == name)
        {
            bIsSame = true;
            break;
        }
    }
    return bIsSame;
}

通过以上,可以扩展对应的需求,如在列表项贴图,显示视频,显示动图等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值