Qt之QListWidget实战

介绍

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


1、以下是QListWidget的

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

等功能的实现。

重命名效果

Qt之QListWidget实战_第1张图片

所有代码

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

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 
#include 
#include 

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

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

你可能感兴趣的:(Qt,qt,c++,开发语言)