I'm trying to give a custom class that inherits from QLabel to be draggable. Towards that end, I'm t开发者_Go百科rying to create a temporary copy of the class at the current mouse position using the following code inside of the class' 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;
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. 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. 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.
I suspect, though, that there is a better way to implement the drag and drop functionality you are looking for without a copy.
精彩评论