简体   繁体   English

QT拖放-使用qobject_cast创建临时QLabel

[英]QT drag and drop - Creating a temporary QLabel using qobject_cast

I'm trying to give a custom class that inherits from QLabel to be draggable. 我试图给一个继承自QLabel的自定义类是可拖动的。 Towards that end, I'm trying to create a temporary copy of the class at the current mouse position using the following code inside of the class' mousePressEvent: 为此,我正在尝试使用类'mousePressEvent中的以下代码在当前鼠标位置创建类的临时副本:

QLabel *child = qobject_cast{QLabel*}(this->childAt(event->pos()));

NOTE: this line has carrots instead of brackets, but stack overflow interprets it and takes it out if (!child) return;

 NOTE: this line has carrots instead of brackets, but stack overflow interprets it and takes it out if (!child) return; 

The child is never created, and I can't figure out why. 这个孩子永远不会被创造出来,我也不知道为什么。 Any ideas? 有任何想法吗?

If your code is within your QLabel -derived class, childAt() is not the right function. 如果您的代码 QLabel派生的类中,则childAt()不是正确的函数。 That would return a child widget contained within your label. 这将返回标签中包含的子窗口小部件。 It doesn't sound like that is what you are trying to do, but correct me if I am misunderstanding. 这听起来不像你正在尝试做的,但如果我误解,请纠正我。

The object you want to copy is this , but "copy" can have many meanings in c++, and I am not sure exactly you are trying to do. 您要复制的对象是this ,但是“复制”在c ++中可能有很多含义,而且我不确定您到底打算做什么。 You will probably have to implement it yourself, perhaps with a method called clone() that creates a new instance of your class and populates the values you need to reproduce. 您可能必须自己实现它,也许使用称为clone()的方法来创建您的类的新实例并填充您需要重现的值。

I suspect, though, that there is a better way to implement the drag and drop functionality you are looking for without a copy. 但我怀疑,有一种更好的方法可以实现您正在寻找的拖放功能,而无需复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM