Drag tab between two QtabWidget
-
@Narutoblaze said in Drag tab between two QtabWidget:
if (e->source()->parent() == this)
return;It shouldn't make any difference since you are comparing pointer addresses.
-
@Narutoblaze
source()
returns a pointer to QObject. QObject has aparent()
method and QWidget also has aparentWidget()
method. You can cast that pointer to QWidget pointer if you need to, but here you just want to compare addresses, so usingparent()
is enough. In case of widgets parent and parent widget are (usually) the same thing. -
@Chris-Kawa so what function do i use, how can i make c++ equivalent of this python code when smiller function does not exist.
inside *dropevent (QDropEvent e) function i need e->source()->parentWidget() e->source()->parentWidget().widget() e->source()->TABINDEX etc but in c++ there is no function like this so i can i transform those python code to c++ ?? is there alternative function that i can use ??
-
@Narutoblaze
WhereQObject
will do just usee->source()->parent()
.If you do need a
QWidget
viaparentWidget()
:QWidget *parentWidget = qobject_cast<QWidget *>(parent()); Q_ASSERT(parentWidget); // Now `parentWidget` is indeed a `QWidget*` pointing to a widget, use as per Python code
-
@JonB this only solves for one function
what about others why c++ does not have similar function ? self.addTab(e.source().parentWidget().widget(self.parent.TABINDEX),e.source().tabText(self.parent.TABINDEX))e.source().tabText() not found
parent.TABINDEX not foundand few more.
-
@Narutoblaze said in Drag tab between two QtabWidget:
e.source().tabText() not found
parent.TABINDEX not foundtabText() is a method of QTabWidget
TABINDEX is a variable defined in the custom Window python class -
@Narutoblaze said in Drag tab between two QtabWidget:
what about others why c++ does not have similar function ?
Because Python allows you to write code to call any function you like on any object, and if the object is of the right type it works and if not it generates a runtime error. C++ requires you have the right class at compile-time before you can call a method, e.g.
qobject_cast
,dynamic_cast
,static_cast
etc. -
AS @JonB said,
Python is a dynamic language, C++ is not.
So, you need to make an explicit cast to retreive the type of object you want.
If you expect e->source() to return a QTabWidget, you need to do:QTabWidget* tabWidget=qobject_cast<QTabWidget*>(e->source()); if(tabWidget) // is a tab widget ? ( // indeed it is ) else { // is not }
-