【发布时间】:2022-08-18 21:06:09
【问题描述】:
我想没有人知道这件事。我问同样的问题两天了,没有人回答。
我找到了一个关于拖放的 toDoList 项目。我想知道我能得到拖放的项目吗?我正在阅读文档 2 天。我实施这些方法。
protected:
void dragEnterEvent( QDragEnterEvent *anEvent ) override;
void dragMoveEvent( QDragMoveEvent *anEvent ) override;
void dragLeaveEvent( QDragLeaveEvent *anEvent ) override;
void dropEvent( QDropEvent *anEvent ) override;
有 2 个列表视图和工具栏。我将添加和删除添加到工具栏。
我可以拖放,但我无法获取拖动项目的文本。这是主要代码。
我真的很想知道,我们是否正确地覆盖了方法。但是我们没有将方法与某事联系起来。该方法如何工作?
todolist::todolist(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::todolist)
{
QWidget* pWidget = new QWidget(this);
pWidget->setStyleSheet(\"background-color: #ECF0F1\");
setCentralWidget(pWidget);
QVBoxLayout* pMainLayout = new QVBoxLayout();
pWidget->setLayout(pMainLayout);
QLabel* pwTitle = new QLabel(\"To Do List\", this);
pMainLayout->addWidget(pwTitle);
pwTitle->setAlignment(Qt::AlignCenter);
pwTitle->setStyleSheet(\"font-size: 30pt; margin: 10%;\");
QHBoxLayout* pHLayoutLabels = new QHBoxLayout();
pMainLayout->addLayout(pHLayoutLabels);
QLabel* plblPending = new QLabel(\"Pending\", this);
plblPending->setStyleSheet(\"font-size: 15pt;\");
pHLayoutLabels->addWidget(plblPending);
QLabel* plblCompleted = new QLabel(\"Completed\", this);
plblCompleted->setStyleSheet(\"font-size: 15pt;\");
pHLayoutLabels->addWidget(plblCompleted);
QHBoxLayout* pHLayout = new QHBoxLayout();
pMainLayout->addLayout(pHLayout);
m_pwPending = new QListView(this);
m_pwPending->setDragEnabled(true);
m_pwPending->setAcceptDrops(true);
m_pwPending->setDropIndicatorShown(true);
m_pwPending->setDefaultDropAction(Qt::MoveAction);
pHLayout->addWidget(m_pwPending);
m_pwCompleted = new QListView(this);
m_pwCompleted->setDragEnabled(true);
m_pwCompleted->setAcceptDrops(true);
m_pwCompleted->setDropIndicatorShown(true);
m_pwCompleted->setDefaultDropAction(Qt::MoveAction);
pHLayout->addWidget(m_pwCompleted);
m_pwPending->setModel(new QStringListModel());
m_pwCompleted->setModel(new QStringListModel());
m_pwPending->setStyleSheet
(\"QListView { font-size: 20pt; font-weight: bold; }\"
\"QListView::item { background-color: #E74C3C; padding: 10%;\"
\"border: 1px solid #C0392B; }\"
\"QListView::item::hover { background-color: #C0392B }\");
m_pwCompleted->setStyleSheet
(\"QListView { font-size: 20pt; font-weight: bold; }\"
\"QListView::item { background-color: #2ECC71; padding: 10%;\"
\"border: 1px solid #27AE60; }\"
\"QListView::item::hover { background-color: #27AE60 }\");
QToolBar* pToolBar = new QToolBar(this);
addToolBar(pToolBar);
m_pActAdd = new QAction(this);
m_pActAdd->setIcon(QIcon(\":/resources/add.png\"));
connect(m_pActAdd, &QAction::triggered, this, &todolist::onAdd);
m_pActRemove = new QAction(this);
m_pActRemove->setIcon(QIcon(\":/resources/remove.png\"));
connect(m_pActRemove, &QAction::triggered, this, &todolist::onRemove);
pToolBar->addAction(m_pActAdd);
pToolBar->addAction(m_pActRemove);
setAcceptDrops(true);
}
void todolist::onAdd()
{
m_pwPending->model()->insertRow(m_pwPending->model()->rowCount());
QModelIndex oIndex = m_pwPending->model()->index(
m_pwPending->model()->rowCount() - 1, 0);
m_pwPending->edit(oIndex);
}
void todolist::onRemove()
{
QModelIndex oIndex = m_pwPending->currentIndex();
m_pwPending->model()->removeRow(oIndex.row());
}
void todolist::dropEvent(QDropEvent* event) {
const QMimeData* mimeData = event->mimeData();
QString temp;
if(mimeData->hasText()) {
temp = mimeData->text();
}
QMessageBox::information(this,\"x\",temp);
}
void todolist::dragEnterEvent(QDragEnterEvent *anEvent)
{
anEvent->setAccepted(true);
}
void todolist::dragMoveEvent(QDragMoveEvent *anEvent)
{
}
void todolist::dragLeaveEvent(QDragLeaveEvent *anEvent)
{
}
todolist::~todolist()
{
delete ui;
}
-
你描述了你是如何准备水槽的,即东西掉在哪里。您还必须准备源,即您打算从哪里拖动东西(无论源和接收器是相同类还是不同类的实例)。你知道吗?拖放不是很容易的东西,但在 Qt 文档中有一些有价值的教程。关于这个。因此,MVC 小部件(
QTreeView、QTableView等)提供了额外的 DnD 功能,但双方仍需要或多或少的自定义代码来启用拖放。 -
@Scheff\'sCat 我知道这并不容易。但我正在搜索它 3 天。你的意思是我必须覆盖dragEnterEvent?给我一些起点,我可以继续。
-
您必须肯定覆盖dragEnterEvent():如果该事件被忽略,小部件将不会收到任何拖动移动事件。然而,这是在放置站点上调用的东西。在拖动站点上,您必须准备拖动的数据,例如通过提供覆盖mimeData() 的自定义模型。 (我不知道默认的 impl. 做了什么。它对我的意图从来没有用处。)
-
我必须承认,关于
mimeTypes(),我也有一些困惑。 AFAIR,mimeTypes()并不重要。即使返回一个空字符串列表也没有任何负面影响。 (我刚刚重新审视了我的工作实施,以确保不会说错话。)
标签: c++ qt drag-and-drop draggable qlistview