QTreeview mousePressEvent 实现阻止选择项目

发布于 2024-09-29 13:06:46 字数 1295 浏览 11 评论 0原文

大家好 我有从 Qtreeview 继承的类,并且实现了简单(空)mousePressEvent 函数
但每当我尝试执行此操作时,Qtreeview 中的项目选择都会被禁用,当我删除此功能时,一切正常
我在这里缺少什么?
这是代码:

void MyTreeWidget::mousePressEvent(QMouseEvent *event)
    {   
        QModelIndex index =  this->indexAt(event->pos());
        QAbstractItemModel *model = this->model();
        QMap<int, QVariant> ItemData = model->itemData(index);
        QMap<int, QVariant>::const_iterator i = ItemData.constBegin();
        while (i != ItemData.constEnd()) {
            QString k = QString::number(i.key());
            QString v = i.value().toString();

         ++i;
        }
        if (event->button() == Qt::LeftButton) {
             QByteArray itemData ;
             QString urlTo;
             itemData.append(urlTo);
             QDrag *drag = new QDrag(this);
             QMimeData *mimeData = new QMimeData;
             mimeData->setData("application/x-dnditemdata", itemData);
             drag->setMimeData(mimeData);

             Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
             if (dropAction == Qt::MoveAction)
             {
                UT::getInstance()->LogToFile("dropAction");
             }


        }
        QTreeView::mousePressEvent(event);
    }

Hello all
I have class that inherited from Qtreeview and I implement simple ( empty ) mousePressEvent function
But whenever I try to do this , the selection of the items in the Qtreeview are disabled , when I remove this function everything is working fine
What im missing here ?
Here Is the code:

void MyTreeWidget::mousePressEvent(QMouseEvent *event)
    {   
        QModelIndex index =  this->indexAt(event->pos());
        QAbstractItemModel *model = this->model();
        QMap<int, QVariant> ItemData = model->itemData(index);
        QMap<int, QVariant>::const_iterator i = ItemData.constBegin();
        while (i != ItemData.constEnd()) {
            QString k = QString::number(i.key());
            QString v = i.value().toString();

         ++i;
        }
        if (event->button() == Qt::LeftButton) {
             QByteArray itemData ;
             QString urlTo;
             itemData.append(urlTo);
             QDrag *drag = new QDrag(this);
             QMimeData *mimeData = new QMimeData;
             mimeData->setData("application/x-dnditemdata", itemData);
             drag->setMimeData(mimeData);

             Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
             if (dropAction == Qt::MoveAction)
             {
                UT::getInstance()->LogToFile("dropAction");
             }


        }
        QTreeView::mousePressEvent(event);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-10-06 13:06:46

这是因为当你重写一个方法时,原来的 on 就不再被调用了。您必须在您创建的方法中手动调用 QTreeView 的 mousePressEvent 方法。

以下是如何做到这一点:

void YourClass::mousePressEvent ( QMouseEvent * event )
{
    QTreeView::mousePressEvent(event);
}

希望这会有所帮助。

It's because that when you override a method, the original on is not called anymore. You would have to manually call the mousePressEvent method of QTreeView in the method you created.

Here is how to do it:

void YourClass::mousePressEvent ( QMouseEvent * event )
{
    QTreeView::mousePressEvent(event);
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文