QAbstractItemDelegate 拖动时绘画问题

发布于 2024-10-27 08:07:19 字数 575 浏览 1 评论 0原文

我正在重载 QAbstractItemDelegate (我自己的 Item 委托类)中的 Paint() 函数。

拖动时,它会绘制整个单元格的内容,这是我不想要的。我假设在拖动时调用了 Paint() 函数,但我似乎没有找到它。

我能找到的最接近的是拥有视图类中的 QState 变量(访问函数 QTableView::state() 受到保护。)通过在我的 QTableView 派生类上创建一个名为“isDragging()”的函数,该函数调用该函数函数并返回是否拖动,我可以在我的委托类中确定我是否正在拖动,并且可以修改 Paint() 函数。

这几乎有效。

问题是它在原始单元格中显示修改后的绘画图像,这是我不想要的 - 我想使原始单元格中的图像保持不变。

我想,必须搜索示例,看看是否有东西可以做到这一点...

我已经爬遍了 Qt 源代码,我可以通过调用 QItemDelegate::paint() 函数来查看它在哪里设置拖动像素图,但是它唯一改变的是它在项目选项样式中强制 QStyle::State_Selected 。这还不够,因为该项目已经被选中。

有什么方法可以知道如何在拖动时显式绘制单元格的内容?

I'm overloading the paint() function in QAbstractItemDelegate (my own Item delegate class).

When dragging, it paints the contents of the entire cell, which I don't want. I'm assuming that the paint() function is called with something specific while dragging, but I don't seem to find it.

The closest I've been able to find is a QState variable in the owning view class (access function QTableView::state() is protected.) By creating a function on my QTableView-derived class called 'isDragging()' which calls that function and returns whether dragging or no, I can determine within my delegate class whether I'm dragging or not, and can modify the paint() function.

This almost works.

The problem is that it shows the modified paint image in the original cell, which I don't want - I want to leave the image in the original cell untouched.

Have to scour the examples, I guess, and see if there's something that does this...

I have crawled through the Qt source and I can see where it sets the drag pixmap by calling the QItemDelegate::paint() function, but the only thing it changes is it forces QStyle::State_Selected in the item option style. That's not enough, since the item is selected, already.

Any way to know how to draw a cell's contents explicitly when dragging?

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

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

发布评论

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

评论(2

明天过后 2024-11-03 08:07:19

好吧,最终的答案是,是的,在“startDrag”上设置标志,但不要将其保留并在鼠标释放按钮事件上取消设置,只需调用基本方法然后取消设置即可。

原因是光标的图像仅被请求(和绘制)一次——而不是像我最初想到的那样在拖动期间连续地请求(和绘制)。保留标志设置意味着光标图像将在不适当的时间绘制。

所以,实现看起来像:

MyClass::dragStart(Qt::DropActions supportedActions)
{
  __dragStart = true;
  TableView::dragStart(supportedActions);
                   // request for drag cursor image happens here
  __dragStart = false;
}

Ok, the ultimate answer on this was to, yes, set the flag on 'startDrag', but rather than leaving it around and unsetting it on mouse release button event, simply call the base method and then unset.

The reason is that the image for the cursor is only requested (and painted) once - not continuously during the drag, as I had first thought. Leaving the flag set meant the cursor image would get drawn at inappropriate times.

So, the implementation looks like:

MyClass::dragStart(Qt::DropActions supportedActions)
{
  __dragStart = true;
  TableView::dragStart(supportedActions);
                   // request for drag cursor image happens here
  __dragStart = false;
}
花心好男孩 2024-11-03 08:07:19

你为什么不自己做呢?拖动开始时设置一个标志并记住活动的 ModelIndex,设置标志时进行一些特殊的绘制,并在拖动完成时清除标志。您可以通过重写 QAbstractItemView::startDrag 来做到这一点。

Why don't you do that yourself? Set a flag when dragging starts and remember the active ModelIndex, do some special painting when the flag is set, and clear the flag when dragging is finished. You can do this by overriding QAbstractItemView::startDrag.

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