Drag tab between two QtabWidget
-
@Narutoblaze said in Drag tab between two QtabWidget:
e.source().parentWidget()
I see 4 occurrences of this in the Python code. Which are you talking about? Where in your C++ code is
parentWidget()
?wrote on 28 Jun 2023, 19:06 last edited by@JonB this function does not exist in c++ i have also looked in doc this e->source()->parent() exist but e->source()->parentWidget() does not.
-
@JonB this function does not exist in c++ i have also looked in doc this e->source()->parent() exist but e->source()->parentWidget() does not.
wrote on 28 Jun 2023, 19:23 last edited by mpergand@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.
-
@JonB this function does not exist in c++ i have also looked in doc this e->source()->parent() exist but e->source()->parentWidget() does not.
@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. -
@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.wrote on 30 Jun 2023, 13:57 last edited by@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 ??
-
@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 ??
wrote on 30 Jun 2023, 14:06 last edited by JonB@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
-
@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.
wrote on 30 Jun 2023, 15:45 last edited by@mpergand what about other function that does not exist function used on the python code does not exist in c++ problem is not about some pointer !
-
@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
wrote on 30 Jun 2023, 16:31 last edited by@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.
-
@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.
wrote on 30 Jun 2023, 16:43 last edited by mpergand@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 -
@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.
wrote on 30 Jun 2023, 17:20 last edited by@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. -
@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.
wrote on 30 Jun 2023, 17:32 last edited by mpergandAS @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 }
-
12/12