1、簡(jiǎn)單使用 - 增加、刪除裕坊、插入
效果圖
鏈接信號(hào)和槽
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*)));
槽實(shí)現(xiàn)
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
設(shè)置QListWidget策略燕酷,并關(guān)聯(lián)右鍵信號(hào)和槽事件籍凝,customContextMenuRequested
是繼承自QWidget的信號(hào)。
ui.listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui.listWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ItemMenu(const QPoint&)));
在QtCreater中添加action
關(guān)聯(lián)action信號(hào)和槽事件
connect(ui.actionAdd, SIGNAL(triggered()), this, SLOT(OnMenuAdd()));
connect(ui.actionRemove, SIGNAL(triggered()), this, SLOT(OnMenuRemove()));
實(shí)現(xiàn)槽事件
// 右鍵菜單
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 雙擊可編輯
鏈接信號(hào)和槽
connect(ui.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(OnItemDoubleClicked(QListWidgetItem *)));
實(shí)現(xiàn)槽事件
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;// 開(kāi)始拖拽的點(diǎn)
QListWidgetItem *m_dragItem;// 拖拽的項(xiàng)
protected:
//重寫(xiě)下面兩個(gè)函數(shù)來(lái)完成從該部件中拖拽出去的操作.
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
//重寫(xiě)下面三個(gè)函數(shù)來(lái)完成接受外來(lái)拖拽的操作.
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)
{
// 設(shè)置可拖拽
this->setAcceptDrops(true);
}
MyListWidget::~MyListWidget()
{
}
// 從該部件中拖拽出去的操作.
void MyListWidget::mousePressEvent(QMouseEvent *event) {
//確保左鍵拖拽.
if (event->button() == Qt::LeftButton)
{
//先保存拖拽的起點(diǎn).
m_dragPoint = event->pos();
//保留被拖拽的項(xiàng).
m_dragItem = this->itemAt(event->pos());
}
//保留原QListWidget部件的鼠標(biāo)點(diǎn)擊操作.
QListWidget::mousePressEvent(event);
}
void MyListWidget::mouseMoveEvent(QMouseEvent *event) {
//確保按住左鍵移動(dòng).
if (event->buttons() & Qt::LeftButton)
{
QPoint temp = event->pos() - m_dragPoint;
//只有這個(gè)長(zhǎng)度大于默認(rèn)的距離,才會(huì)被系統(tǒng)認(rèn)為是形成拖拽的操作.
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))
{
//當(dāng)成功拖拽后酱讶,刪除拖拽項(xiàng).
auto i = this->takeItem(this->row(m_dragItem));
delete i;
}
}
}
QListWidget::mouseMoveEvent(event);
}
// 接受外來(lái)拖拽的操作.
void MyListWidget::dragEnterEvent(QDragEnterEvent *event) {
//設(shè)置動(dòng)作為移動(dòng)動(dòng)作.
event->setDropAction(Qt::MoveAction);
//然后接受事件.這個(gè)一定要寫(xiě).
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())
{
//找到當(dāng)前鼠標(biāo)位置在部件中的項(xiàng).
auto item = this->itemAt(event->pos());
//
if (!item)
this->addItem(str);
else
this->insertItem(this->row(item), str);
event->setDropAction(Qt::MoveAction);
event->accept();
}
}
右鍵提升可視化控件與自定義類關(guān)聯(lián)起來(lái)退盯。
簡(jiǎn)單方法。。渊迁。額慰照。。琉朽。
參考:https://blog.csdn.net/qq_37233607/article/details/78630924
未完待續(xù)...