美文网首页
Qt学习笔记(十三)列表(QListWidget)

Qt学习笔记(十三)列表(QListWidget)

作者: 行走行囊 | 来源:发表于2018-04-23 22:02 被阅读0次

    1、简单使用 - 增加、删除、插入

    效果图


    效果图

    链接信号和槽

    connect(ui.addBtn, SIGNAL(clicked()), this, SLOT(AddItem()));
    connect(ui.deleteBtn, SIGNAL(clicked()), this, SLOT(DeleteItem()));
    connect(ui.insertBtn, SIGNAL(clicked()), this, SLOT(InsertItem()));
    connect(ui.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(ItemDoubleClicked(QListWidgetItem*)));
    

    槽实现

    void MyListWindow::AddItem() {
        QString text = ui.lineEdit->text();
        QListWidgetItem *item = new QListWidgetItem;
        item->setText(text);
        if (text != NULL) {
            ui.listWidget->addItem(item);
            ui.lineEdit->clear();
        }
    }
    void MyListWindow::DeleteItem() {
        QListWidgetItem *currentItem = ui.listWidget->currentItem();
        delete currentItem;
    }
    void MyListWindow::InsertItem() {
        int index = ui.listWidget->currentRow();
        QString text = ui.lineEdit->text();
        QListWidgetItem *item = new QListWidgetItem;
        item->setText(text);
        if (text != NULL) {
            ui.listWidget->insertItem(index + 1, item);
            ui.lineEdit->clear();
        }   
    }
    void MyListWindow::ItemDoubleClicked(QListWidgetItem* item) {
        ui.plainTextEdit->clear();
        QString text = item->text();
        ui.plainTextEdit->setPlainText(text);
    }
    

    2、右键添加Menu

    设置QListWidget策略,并关联右键信号和槽事件,customContextMenuRequested是继承自QWidget的信号。

    ui.listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui.listWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ItemMenu(const QPoint&)));
    

    在QtCreater中添加action


    image.png

    关联action信号和槽事件

    connect(ui.actionAdd, SIGNAL(triggered()), this, SLOT(OnMenuAdd()));
    connect(ui.actionRemove, SIGNAL(triggered()), this, SLOT(OnMenuRemove()));
    

    实现槽事件

    // 右键菜单
    void MyListWindow::ItemMenu(const QPoint& pt) {
        QListWidgetItem *selectedItem = ui.listWidget->itemAt(pt);
        QMenu menu;
        menu.addAction(ui.actionAdd);
        if (selectedItem) {
            menu.addAction(ui.actionRemove);
        }   
        menu.exec(ui.listWidget->mapToGlobal(pt));
    }
    // 右键菜单-添加
    void MyListWindow::OnMenuAdd() {
        QListWidgetItem *newitem = new QListWidgetItem("New Item");
        ui.listWidget->addItem(newitem);
    }
    // 右键菜单-删除
    void MyListWindow::OnMenuRemove() {
        QListWidgetItem *item = ui.listWidget->currentItem();
        if (item) {
            delete item;
        }
    }
    

    3、QListWidgetItem 双击可编辑

    链接信号和槽

    connect(ui.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(OnItemDoubleClicked(QListWidgetItem *)));
    

    实现槽事件

    void MyListWindow::OnItemDoubleClicked(QListWidgetItem *item) {
        item->setFlags(item->flags() | Qt::ItemIsEditable);
    }
    

    4、QListWidgetItem 拖拽

    自定义QListWidget派生类MyListWidget。

    // MyListWidget.h
    #include <QListWidget>
    #include <QWidgetItem>
    #include <QPoint>
    
    class MyListWidget : public QListWidget
    {
        Q_OBJECT
    
    private:
        QPoint m_dragPoint;// 开始拖拽的点
        QListWidgetItem  *m_dragItem;// 拖拽的项
    
    protected:
        //重写下面两个函数来完成从该部件中拖拽出去的操作.
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        //重写下面三个函数来完成接受外来拖拽的操作.
        void dragEnterEvent(QDragEnterEvent *event);
        void dragMoveEvent(QDragMoveEvent *event);
        void dropEvent(QDropEvent *event);
    
    public:
        MyListWidget(QWidget *parent);
        ~MyListWidget();
    };
    
    // MyListWidget.cpp
    #include "MyListWidget.h"
    #include <QMouseEvent>
    #include <QApplication>
    #include <QDrag>
    #include <QMimeData>
    
    MyListWidget::MyListWidget(QWidget *parent)
        : QListWidget(parent)
    {
        // 设置可拖拽
        this->setAcceptDrops(true);
    }
    
    MyListWidget::~MyListWidget()
    {
    }
    
    // 从该部件中拖拽出去的操作.
    void MyListWidget::mousePressEvent(QMouseEvent *event) {
        //确保左键拖拽.
        if (event->button() == Qt::LeftButton)
        {
            //先保存拖拽的起点.
            m_dragPoint = event->pos();
            //保留被拖拽的项.
            m_dragItem = this->itemAt(event->pos());
        }
        //保留原QListWidget部件的鼠标点击操作.
        QListWidget::mousePressEvent(event);
    }
    void MyListWidget::mouseMoveEvent(QMouseEvent *event) {
        //确保按住左键移动.
        if (event->buttons() & Qt::LeftButton)
        {
            QPoint temp = event->pos() - m_dragPoint;
            //只有这个长度大于默认的距离,才会被系统认为是形成拖拽的操作.
            if (temp.manhattanLength() > QApplication::startDragDistance())
            {
                QDrag *drag = new QDrag(this);
                QMimeData *mimeData = new QMimeData;
                mimeData->setText(m_dragItem->text());
                drag->setMimeData(mimeData);
                auto action = drag->exec(Qt::CopyAction | Qt::MoveAction);
    
                if (action == (Qt::CopyAction) || (action == Qt::MoveAction))
                {
                    //当成功拖拽后,删除拖拽项.
                    auto i = this->takeItem(this->row(m_dragItem));
                    delete i;
                }
            }
        }
        QListWidget::mouseMoveEvent(event);
    }
    // 接受外来拖拽的操作.
    void MyListWidget::dragEnterEvent(QDragEnterEvent *event) {
        //设置动作为移动动作.
        event->setDropAction(Qt::MoveAction);
        //然后接受事件.这个一定要写.
        event->accept();
    }
    void MyListWidget::dragMoveEvent(QDragMoveEvent *event) {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
    void MyListWidget::dropEvent(QDropEvent *event) {
        QString str = event->mimeData()->text();
        if (!str.isEmpty())
        {
            //找到当前鼠标位置在部件中的项.
            auto item = this->itemAt(event->pos());
            //
            if (!item)
                this->addItem(str);
            else
                this->insertItem(this->row(item), str);
    
            event->setDropAction(Qt::MoveAction);
            event->accept();
        }
    }
    

    右键提升可视化控件与自定义类关联起来。


    提升控件

    简单方法。。。额。。。


    image.png

    参考:https://blog.csdn.net/qq_37233607/article/details/78630924
    未完待续...

    相关文章

      网友评论

          本文标题:Qt学习笔记(十三)列表(QListWidget)

          本文链接:https://www.haomeiwen.com/subject/tfjrlftx.html