qtwidgets.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // Copyright (C) 2018 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. /*********************************************************************
  4. * INJECT CODE
  5. ********************************************************************/
  6. // @snippet qtreewidgetitemiterator-next
  7. if (**%CPPSELF) {
  8. QTreeWidgetItemIterator *%0 = new QTreeWidgetItemIterator((*%CPPSELF)++);
  9. %PYARG_0 = %CONVERTTOPYTHON[QTreeWidgetItemIterator *](%0);
  10. }
  11. // @snippet qtreewidgetitemiterator-next
  12. // @snippet qtreewidgetitemiterator-value
  13. QTreeWidgetItem *%0 = %CPPSELF.operator *();
  14. %PYARG_0 = %CONVERTTOPYTHON[QTreeWidgetItem *](%0);
  15. Shiboken::Object::releaseOwnership(%PYARG_0);
  16. // @snippet qtreewidgetitemiterator-value
  17. // @snippet qgraphicsitem
  18. PyObject *userTypeConstant = PyLong_FromLong(QGraphicsItem::UserType);
  19. tpDict.reset(PepType_GetDict(Sbk_QGraphicsItem_TypeF()));
  20. PyDict_SetItemString(tpDict.object(), "UserType", userTypeConstant);
  21. // @snippet qgraphicsitem
  22. // @snippet qgraphicsitem-scene-return-parenting
  23. if (%0) {
  24. QObject *parent = %0->parent();
  25. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QObject *](parent));
  26. Shiboken::Object::setParent(pyParent, %PYARG_0);
  27. }
  28. // @snippet qgraphicsitem-scene-return-parenting
  29. // @snippet qgraphicsitem-isblockedbymodalpanel
  30. QGraphicsItem *item_ = nullptr;
  31. %RETURN_TYPE retval_ = %CPPSELF.%FUNCTION_NAME(&item_);
  32. %PYARG_0 = PyTuple_New(2);
  33. PyTuple_SetItem(%PYARG_0, 0, %CONVERTTOPYTHON[%RETURN_TYPE](retval_));
  34. PyTuple_SetItem(%PYARG_0, 1, %CONVERTTOPYTHON[QGraphicsItem *](item_));
  35. // @snippet qgraphicsitem-isblockedbymodalpanel
  36. // @snippet qitemeditorfactory-registereditor
  37. Shiboken::Object::releaseOwnership(%PYARG_2);
  38. // @snippet qitemeditorfactory-registereditor
  39. // @snippet qitemeditorfactory-setdefaultfactory
  40. //this function is static we need keep ref to default value, to be able to call python virtual functions
  41. static PyObject *_defaultValue = nullptr;
  42. %CPPSELF.%FUNCTION_NAME(%1);
  43. Py_INCREF(%PYARG_1);
  44. if (_defaultValue)
  45. Py_DECREF(_defaultValue);
  46. _defaultValue = %PYARG_1;
  47. // @snippet qitemeditorfactory-setdefaultfactory
  48. // @snippet qformlayout-fix-args
  49. int _row;
  50. QFormLayout::ItemRole _role;
  51. %CPPSELF->%FUNCTION_NAME(%ARGUMENT_NAMES, &_row, &_role);
  52. %PYARG_0 = PyTuple_New(2);
  53. PyTuple_SetItem(%PYARG_0, 0, %CONVERTTOPYTHON[int](_row));
  54. // On the C++ side, *rolePtr is not set if row == -1, in which case on
  55. // the Python side this gets converted to a random value outside the
  56. // enum range. Fix this by setting _role to a default value here.
  57. if (_row == -1)
  58. _role = QFormLayout::LabelRole;
  59. PyTuple_SetItem(%PYARG_0, 1, %CONVERTTOPYTHON[QFormLayout::ItemRole](_role));
  60. // @snippet qformlayout-fix-args
  61. // @snippet qfiledialog-return
  62. %BEGIN_ALLOW_THREADS
  63. %RETURN_TYPE retval_ = %CPPSELF.%FUNCTION_NAME(%1, %2, %3, %4, &%5, %6);
  64. %END_ALLOW_THREADS
  65. %PYARG_0 = PyTuple_New(2);
  66. PyTuple_SetItem(%PYARG_0, 0, %CONVERTTOPYTHON[%RETURN_TYPE](retval_));
  67. PyTuple_SetItem(%PYARG_0, 1, %CONVERTTOPYTHON[QString](%5));
  68. // @snippet qfiledialog-return
  69. // @snippet qwidget-addaction-glue
  70. static PyObject *connectAction(QAction *action, PyObject *callback)
  71. {
  72. PyObject *pyAct = %CONVERTTOPYTHON[QAction *](action);
  73. Shiboken::AutoDecRef result(PyObject_CallMethod(pyAct, "connect", "OsO",
  74. pyAct,
  75. SIGNAL(triggered()), callback));
  76. if (result.isNull()) {
  77. Py_DECREF(pyAct);
  78. return nullptr;
  79. }
  80. return pyAct;
  81. }
  82. static inline PyObject *addActionWithPyObject(QWidget *self, const QString &text,
  83. PyObject *callback)
  84. {
  85. QAction *act = self->addAction(text);
  86. return connectAction(act, callback);
  87. }
  88. static inline PyObject *addActionWithPyObject(QWidget *self, const QIcon &icon, const QString &text,
  89. PyObject *callback)
  90. {
  91. auto *act = self->addAction(icon, text);
  92. return connectAction(act, callback);
  93. }
  94. static inline PyObject *addActionWithPyObject(QWidget *self, const QString &text,
  95. const QKeySequence &shortcut,
  96. PyObject *callback)
  97. {
  98. QAction *act = self->addAction(text, shortcut);
  99. return connectAction(act, callback);
  100. }
  101. static inline PyObject *addActionWithPyObject(QWidget *self, const QIcon &icon,
  102. const QString &text,
  103. const QKeySequence &shortcut,
  104. PyObject *callback)
  105. {
  106. QAction *act = self->addAction(icon, text, shortcut);
  107. return connectAction(act, callback);
  108. }
  109. // @snippet qwidget-addaction-glue
  110. // FIXME PYSIDE7: Remove in favor of widgets methods
  111. // @snippet qmenu-glue
  112. inline PyObject *addMenuActionWithPyObject(QMenu *self, const QIcon &icon,
  113. const QString &text, PyObject *callback,
  114. const QKeySequence &shortcut)
  115. {
  116. QAction *act = self->addAction(text);
  117. if (!icon.isNull())
  118. act->setIcon(icon);
  119. if (!shortcut.isEmpty())
  120. act->setShortcut(shortcut);
  121. self->addAction(act);
  122. PyObject *pyAct = %CONVERTTOPYTHON[QAction *](act);
  123. Shiboken::AutoDecRef result(PyObject_CallMethod(pyAct, "connect", "OsO",
  124. pyAct,
  125. SIGNAL(triggered()), callback));
  126. if (result.isNull()) {
  127. Py_DECREF(pyAct);
  128. return nullptr;
  129. }
  130. return pyAct;
  131. }
  132. // @snippet qmenu-glue
  133. // addAction(QString,PyObject*,QKeySequence) FIXME PYSIDE7 deprecated
  134. // @snippet qmenu-addaction-1
  135. %PYARG_0 = addMenuActionWithPyObject(%CPPSELF, QIcon(), %1, %2, %3);
  136. // @snippet qmenu-addaction-1
  137. // addAction(QIcon,QString,PyObject*,QKeySequence) FIXME PYSIDE7 deprecated
  138. // @snippet qmenu-addaction-2
  139. %PYARG_0 = addMenuActionWithPyObject(%CPPSELF, %1, %2, %3, %4);
  140. // @snippet qmenu-addaction-2
  141. // @snippet qmenu-addaction-3
  142. %CPPSELF.addAction(%1);
  143. // @snippet qmenu-addaction-3
  144. // addAction(QString,PyObject*)
  145. // @snippet qwidget-addaction-2
  146. %PYARG_0 = addActionWithPyObject(%CPPSELF, %1, %2);
  147. // @snippet qwidget-addaction-2
  148. // addAction(QString,QKeySequence,PyObject*) or addAction(QIcon,QString,PyObject*)
  149. // @snippet qwidget-addaction-3
  150. %PYARG_0 = addActionWithPyObject(%CPPSELF, %1, %2, %3);
  151. // @snippet qwidget-addaction-3
  152. // addAction(QIcon,QString,QKeySequence,PyObject*)
  153. // @snippet qwidget-addaction-4
  154. %PYARG_0 = addActionWithPyObject(%CPPSELF, %1, %2, %3, %4);
  155. // @snippet qwidget-addaction-4
  156. // @snippet qmenu-clear
  157. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  158. const auto &actions = %CPPSELF.actions();
  159. for (auto *act : actions) {
  160. if (auto wrapper = bm.retrieveWrapper(act)) {
  161. auto pyObj = reinterpret_cast<PyObject *>(wrapper);
  162. Py_INCREF(pyObj);
  163. Shiboken::Object::setParent(nullptr, pyObj);
  164. Shiboken::Object::invalidate(pyObj);
  165. Py_DECREF(pyObj);
  166. }
  167. }
  168. // @snippet qmenu-clear
  169. // @snippet qmenubar-clear
  170. const auto &actions = %CPPSELF.actions();
  171. for (auto *act : actions) {
  172. Shiboken::AutoDecRef pyAct(%CONVERTTOPYTHON[QAction *](act));
  173. Shiboken::Object::setParent(nullptr, pyAct);
  174. Shiboken::Object::invalidate(pyAct);
  175. }
  176. // @snippet qmenubar-clear
  177. // @snippet qtoolbox-removeitem
  178. QWidget *_widget = %CPPSELF.widget(%1);
  179. if (_widget) {
  180. Shiboken::AutoDecRef pyWidget(%CONVERTTOPYTHON[QWidget *](_widget));
  181. Shiboken::Object::setParent(0, pyWidget);
  182. }
  183. // @snippet qtoolbox-removeitem
  184. // @snippet qlayout-help-functions
  185. #ifndef _QLAYOUT_HELP_FUNCTIONS_
  186. #define _QLAYOUT_HELP_FUNCTIONS_ // Guard for jumbo builds
  187. static const char msgInvalidParameterAdd[] =
  188. "Invalid parameter None passed to addLayoutOwnership().";
  189. static const char msgInvalidParameterRemoval[] =
  190. "Invalid parameter None passed to removeLayoutOwnership().";
  191. void addLayoutOwnership(QLayout *layout, QLayoutItem *item);
  192. void removeLayoutOwnership(QLayout *layout, QWidget *widget);
  193. inline void addLayoutOwnership(QLayout *layout, QWidget *widget)
  194. {
  195. if (layout == nullptr || widget == nullptr) {
  196. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterAdd);
  197. return;
  198. }
  199. //transfer ownership to parent widget
  200. QWidget *lw = layout->parentWidget();
  201. QWidget *pw = widget->parentWidget();
  202. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QWidget *](widget));
  203. //Transfer parent to layout widget
  204. if (pw && lw && pw != lw)
  205. Shiboken::Object::setParent(nullptr, pyChild);
  206. if (!lw && !pw) {
  207. //keep the reference while the layout is orphan
  208. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget *](layout));
  209. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(pyParent.object()),
  210. retrieveObjectName(pyParent).constData(),
  211. pyChild, true);
  212. } else {
  213. if (!lw)
  214. lw = pw;
  215. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget *](lw));
  216. Shiboken::Object::setParent(pyParent, pyChild);
  217. }
  218. }
  219. inline void addLayoutOwnership(QLayout *layout, QLayout *other)
  220. {
  221. if (layout == nullptr || other == nullptr) {
  222. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterAdd);
  223. return;
  224. }
  225. //transfer all children widgets from other to layout parent widget
  226. QWidget *parent = layout->parentWidget();
  227. if (!parent) {
  228. //keep the reference while the layout is orphan
  229. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QLayout *](layout));
  230. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QLayout *](other));
  231. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(pyParent.object()),
  232. retrieveObjectName(pyParent).constData(),
  233. pyChild, true);
  234. return;
  235. }
  236. for (int i = 0, i_max = other->count(); i < i_max; ++i) {
  237. QLayoutItem *item = other->itemAt(i);
  238. if (PyErr_Occurred() || !item)
  239. return;
  240. addLayoutOwnership(layout, item);
  241. }
  242. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QLayout *](layout));
  243. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QLayout *](other));
  244. Shiboken::Object::setParent(pyParent, pyChild);
  245. }
  246. inline void addLayoutOwnership(QLayout *layout, QLayoutItem *item)
  247. {
  248. if (layout == nullptr || item == nullptr) {
  249. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterAdd);
  250. return;
  251. }
  252. if (QWidget *w = item->widget()) {
  253. addLayoutOwnership(layout, w);
  254. } else {
  255. if (QLayout *l = item->layout())
  256. addLayoutOwnership(layout, l);
  257. }
  258. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QLayout *](layout));
  259. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QLayoutItem *](item));
  260. Shiboken::Object::setParent(pyParent, pyChild);
  261. }
  262. static void removeWidgetFromLayout(QLayout *layout, QWidget *widget)
  263. {
  264. if (layout == nullptr || widget == nullptr) {
  265. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterRemoval);
  266. return;
  267. }
  268. if (QWidget *parent = widget->parentWidget()) {
  269. //give the ownership to parent
  270. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget *](parent));
  271. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QWidget *](widget));
  272. Shiboken::Object::setParent(pyParent, pyChild);
  273. } else {
  274. //remove reference on layout
  275. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget *](layout));
  276. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QWidget *](widget));
  277. Shiboken::Object::removeReference(reinterpret_cast<SbkObject *>(pyParent.object()),
  278. retrieveObjectName(pyParent).constData(),
  279. pyChild);
  280. }
  281. }
  282. inline void removeLayoutOwnership(QLayout *layout, QLayoutItem *item)
  283. {
  284. if (layout == nullptr || item == nullptr) {
  285. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterRemoval);
  286. return;
  287. }
  288. if (QWidget *w = item->widget()) {
  289. removeWidgetFromLayout(layout, w);
  290. } else {
  291. QLayout *l = item->layout();
  292. if (l && item != l)
  293. removeLayoutOwnership(layout, l);
  294. }
  295. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QLayoutItem *](item));
  296. Shiboken::Object::invalidate(pyChild);
  297. Shiboken::Object::setParent(0, pyChild);
  298. }
  299. inline void removeLayoutOwnership(QLayout *layout, QWidget *widget)
  300. {
  301. if (layout == nullptr || widget == nullptr) {
  302. PyErr_SetString(PyExc_RuntimeError, msgInvalidParameterRemoval);
  303. return;
  304. }
  305. for (int i = 0, i_max = layout->count(); i < i_max; ++i) {
  306. QLayoutItem *item = layout->itemAt(i);
  307. if (PyErr_Occurred() || !item)
  308. return;
  309. if (item->widget() == widget)
  310. removeLayoutOwnership(layout, item);
  311. }
  312. }
  313. #endif // _QLAYOUT_HELP_FUNCTIONS_
  314. // @snippet qlayout-help-functions
  315. // @snippet qlayout-setalignment
  316. %CPPSELF.setAlignment(%1);
  317. // @snippet qlayout-setalignment
  318. // @snippet addownership-item-at
  319. if (%0 != nullptr)
  320. addLayoutOwnership(%CPPSELF, %0);
  321. // @snippet addownership-item-at
  322. // @snippet addownership-1
  323. addLayoutOwnership(%CPPSELF, %1);
  324. // @snippet addownership-1
  325. // @snippet addownership-2
  326. addLayoutOwnership(%CPPSELF, %2);
  327. // @snippet addownership-2
  328. // @snippet removeownership-1
  329. removeLayoutOwnership(%CPPSELF, %1);
  330. // @snippet removeownership-1
  331. // @snippet qgridlayout-getitemposition
  332. int a, b, c, d;
  333. %CPPSELF.%FUNCTION_NAME(%1, &a, &b, &c, &d);
  334. %PYARG_0 = PyTuple_New(4);
  335. PyTuple_SetItem(%PYARG_0, 0, %CONVERTTOPYTHON[int](a));
  336. PyTuple_SetItem(%PYARG_0, 1, %CONVERTTOPYTHON[int](b));
  337. PyTuple_SetItem(%PYARG_0, 2, %CONVERTTOPYTHON[int](c));
  338. PyTuple_SetItem(%PYARG_0, 3, %CONVERTTOPYTHON[int](d));
  339. // @snippet qgridlayout-getitemposition
  340. // @snippet qgraphicsscene-destroyitemgroup
  341. QGraphicsItem *parentItem = %1->parentItem();
  342. Shiboken::AutoDecRef parent(%CONVERTTOPYTHON[QGraphicsItem *](parentItem));
  343. const auto &childItems = %1->childItems();
  344. for (auto *item : childItems)
  345. Shiboken::Object::setParent(parent, %CONVERTTOPYTHON[QGraphicsItem *](item));
  346. %CPPSELF.%FUNCTION_NAME(%1);
  347. // the arg was destroyed by Qt.
  348. Shiboken::Object::invalidate(%PYARG_1);
  349. // @snippet qgraphicsscene-destroyitemgroup
  350. // @snippet qgraphicsscene-addwidget
  351. %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(%1, %2);
  352. %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0);
  353. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(%PYARG_0), "setWidget(QWidget*)1", %PYARG_1);
  354. // @snippet qgraphicsscene-addwidget
  355. // @snippet qgraphicsscene-clear
  356. const QList<QGraphicsItem *> items = %CPPSELF.items();
  357. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  358. for (auto *item : items) {
  359. SbkObject *obj = bm.retrieveWrapper(item);
  360. if (obj) {
  361. if (Py_REFCNT(reinterpret_cast<PyObject *>(obj)) > 1) // If the refcnt is 1 the object will vannish anyway.
  362. Shiboken::Object::invalidate(obj);
  363. Shiboken::Object::removeParent(obj);
  364. }
  365. }
  366. %CPPSELF.%FUNCTION_NAME();
  367. // @snippet qgraphicsscene-clear
  368. // @snippet qtreewidget-clear
  369. QTreeWidgetItem *rootItem = %CPPSELF.invisibleRootItem();
  370. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  371. // PYSIDE-1251:
  372. // Since some objects can be created with a parent and without
  373. // being saved on a local variable (refcount = 1), they will be
  374. // deleted when setting the parent to nullptr, so we change the loop
  375. // to do this from the last child to the first, to avoid the case
  376. // when the child(1) points to the original child(2) in case the
  377. // first one was removed.
  378. for (int i = rootItem->childCount() - 1; i >= 0; --i) {
  379. QTreeWidgetItem *item = rootItem->child(i);
  380. if (SbkObject *wrapper = bm.retrieveWrapper(item))
  381. Shiboken::Object::setParent(nullptr, reinterpret_cast<PyObject *>(wrapper));
  382. }
  383. // @snippet qtreewidget-clear
  384. // @snippet qtreewidgetitem
  385. // Only call the parent function if this return some value
  386. // the parent can be the TreeWidget
  387. if (%0)
  388. Shiboken::Object::setParent(%PYARG_0, %PYSELF);
  389. // @snippet qtreewidgetitem
  390. // @snippet qlistwidget-clear
  391. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  392. for (int i = 0, count = %CPPSELF.count(); i < count; ++i) {
  393. QListWidgetItem *item = %CPPSELF.item(i);
  394. if (auto wrapper = bm.retrieveWrapper(item)) {
  395. auto pyObj = reinterpret_cast<PyObject *>(wrapper);
  396. Py_INCREF(pyObj);
  397. Shiboken::Object::setParent(nullptr, pyObj);
  398. Shiboken::Object::invalidate(pyObj);
  399. Py_DECREF(pyObj);
  400. }
  401. }
  402. %CPPSELF.%FUNCTION_NAME();
  403. // @snippet qlistwidget-clear
  404. // @snippet qwidget-retrieveobjectname
  405. #ifndef _RETRIEVEOBJECTNAME_
  406. #define _RETRIEVEOBJECTNAME_ // Guard for jumbo builds
  407. static QByteArray retrieveObjectName(PyObject *obj)
  408. {
  409. Shiboken::AutoDecRef objName(PyObject_Str(obj));
  410. return Shiboken::String::toCString(objName);
  411. }
  412. #endif
  413. // @snippet qwidget-retrieveobjectname
  414. // @snippet qwidget-glue
  415. // Transfer objects ownership from layout to widget
  416. static inline void qwidgetReparentLayout(QWidget *parent, QLayout *layout)
  417. {
  418. Shiboken::AutoDecRef pyParent(%CONVERTTOPYTHON[QWidget *](parent));
  419. for (int i=0, i_count = layout->count(); i < i_count; i++) {
  420. QLayoutItem *item = layout->itemAt(i);
  421. if (PyErr_Occurred() || !item)
  422. return;
  423. if (QWidget *w = item->widget()) {
  424. QWidget *pw = w->parentWidget();
  425. if (pw != parent) {
  426. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QWidget *](w));
  427. Shiboken::Object::setParent(pyParent, pyChild);
  428. }
  429. } else {
  430. if (QLayout *l = item->layout())
  431. qwidgetReparentLayout(parent, l);
  432. }
  433. }
  434. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QLayout *](layout));
  435. Shiboken::Object::setParent(pyParent, pyChild);
  436. //remove previous references
  437. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(pyChild.object()),
  438. retrieveObjectName(pyChild).constData(),
  439. Py_None);
  440. }
  441. static inline void qwidgetSetLayout(QWidget *self, QLayout *layout)
  442. {
  443. if (!layout || self->layout())
  444. return;
  445. QObject *oldParent = layout->parent();
  446. if (oldParent && oldParent != self) {
  447. if (oldParent->isWidgetType()) {
  448. // remove old parent policy
  449. Shiboken::AutoDecRef pyLayout(%CONVERTTOPYTHON[QLayout *](layout));
  450. Shiboken::Object::setParent(Py_None, pyLayout);
  451. } else {
  452. PyErr_Format(PyExc_RuntimeError, "QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
  453. qPrintable(layout->objectName()), self->metaObject()->className(), qPrintable(self->objectName()));
  454. return;
  455. }
  456. }
  457. if (oldParent != self) {
  458. qwidgetReparentLayout(self, layout);
  459. if (PyErr_Occurred())
  460. return;
  461. self->setLayout(layout);
  462. }
  463. }
  464. // @snippet qwidget-glue
  465. // @snippet qwidget-setstyle
  466. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(%PYSELF), "__style__", %PYARG_1);
  467. // @snippet qwidget-setstyle
  468. // @snippet qwidget-style
  469. QStyle *myStyle = %CPPSELF->style();
  470. if (myStyle && qApp) {
  471. bool keepReference = true;
  472. %PYARG_0 = %CONVERTTOPYTHON[QStyle *](myStyle);
  473. QStyle *appStyle = qApp->style();
  474. if (appStyle == myStyle) {
  475. Shiboken::AutoDecRef pyApp(%CONVERTTOPYTHON[QApplication *](qApp));
  476. // Do not set parentship when qApp is embedded
  477. if (Shiboken::Object::wasCreatedByPython(reinterpret_cast<SbkObject *>(pyApp.object()))) {
  478. Shiboken::Object::setParent(pyApp, %PYARG_0);
  479. Shiboken::Object::releaseOwnership(%PYARG_0);
  480. keepReference = false;
  481. }
  482. }
  483. if (keepReference)
  484. Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(%PYSELF), "__style__", %PYARG_0);
  485. }
  486. // @snippet qwidget-style
  487. // @snippet qapplication-init
  488. static void QApplicationConstructor(PyObject *self, PyObject *pyargv, QApplicationWrapper **cptr)
  489. {
  490. static int argc;
  491. static char **argv;
  492. PyObject *stringlist = PyTuple_GetItem(pyargv, 0);
  493. if (Shiboken::listToArgcArgv(stringlist, &argc, &argv, "PySideApp")) {
  494. *cptr = new QApplicationWrapper(argc, argv, 0);
  495. Shiboken::Object::releaseOwnership(reinterpret_cast<SbkObject *>(self));
  496. PySide::registerCleanupFunction(&PySide::destroyQCoreApplication);
  497. }
  498. }
  499. // @snippet qapplication-init
  500. // @snippet qapplication-setStyle
  501. if (qApp) {
  502. Shiboken::AutoDecRef pyApp(%CONVERTTOPYTHON[QApplication *](qApp));
  503. Shiboken::Object::setParent(pyApp, %PYARG_1);
  504. Shiboken::Object::releaseOwnership(%PYARG_1);
  505. }
  506. // @snippet qapplication-setStyle
  507. // @snippet qwidget-setlayout
  508. qwidgetSetLayout(%CPPSELF, %1);
  509. // %FUNCTION_NAME() - disable generation of function call.
  510. // @snippet qwidget-setlayout
  511. // @snippet qtabwidget-removetab
  512. QWidget *tab = %CPPSELF.widget(%1);
  513. if (tab) {
  514. Shiboken::AutoDecRef pyWidget(%CONVERTTOPYTHON[QWidget *](tab));
  515. %CPPSELF.%FUNCTION_NAME(%1);
  516. }
  517. // @snippet qtabwidget-removetab
  518. // @snippet qtabwidget-clear
  519. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  520. for (int i = 0, count = %CPPSELF.count(); i < count; ++i) {
  521. QWidget *widget = %CPPSELF.widget(i);
  522. if (bm.hasWrapper(widget)) {
  523. Shiboken::AutoDecRef pyWidget(%CONVERTTOPYTHON[QWidget *](widget));
  524. Shiboken::Object::releaseOwnership(pyWidget);
  525. }
  526. }
  527. %CPPSELF.%FUNCTION_NAME();
  528. // @snippet qtabwidget-clear
  529. // @snippet qlineedit-addaction
  530. %CPPSELF.addAction(%1);
  531. // @snippet qlineedit-addaction
  532. // addAction(QIcon,QString,const QObject*,const char*,Qt::ConnectionType)
  533. // @snippet qwidget-addaction-1
  534. QAction *action = %CPPSELF.addAction(%1, %2);
  535. %PYARG_0 = %CONVERTTOPYTHON[QAction *](action);
  536. Shiboken::AutoDecRef result(PyObject_CallMethod(%PYARG_0,
  537. "connect", "OsO",
  538. %PYARG_0, SIGNAL(triggered()), %PYARG_3)
  539. );
  540. // @snippet qwidget-addaction-1
  541. // addAction(QString,const QObject*,const char*,Qt::ConnectionType)
  542. // @snippet qwidget-addaction-2
  543. QAction *action = %CPPSELF.addAction(%1);
  544. %PYARG_0 = %CONVERTTOPYTHON[QAction *](action);
  545. Shiboken::AutoDecRef result(PyObject_CallMethod(%PYARG_0,
  546. "connect", "OsO",
  547. %PYARG_0, SIGNAL(triggered()), %PYARG_2)
  548. );
  549. // @snippet qwidget-addaction-2
  550. // @snippet qtoolbar-clear
  551. QList<PyObject *> lst;
  552. Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
  553. const auto &toolButtonChildren = %CPPSELF.findChildren<QToolButton *>();
  554. for (auto *child : toolButtonChildren) {
  555. if (bm.hasWrapper(child)) {
  556. PyObject *pyChild = %CONVERTTOPYTHON[QToolButton *](child);
  557. Shiboken::Object::setParent(nullptr, pyChild);
  558. lst << pyChild;
  559. }
  560. }
  561. //Remove actions
  562. const auto &actions = %CPPSELF.actions();
  563. for (auto *act : actions) {
  564. Shiboken::AutoDecRef pyAct(%CONVERTTOPYTHON[QAction *](act));
  565. Shiboken::Object::setParent(nullptr, pyAct);
  566. Shiboken::Object::invalidate(pyAct);
  567. }
  568. %CPPSELF.clear();
  569. for (auto *obj : std::as_const(lst)) {
  570. Shiboken::Object::invalidate(reinterpret_cast<SbkObject *>(obj));
  571. Py_XDECREF(obj);
  572. }
  573. // @snippet qtoolbar-clear
  574. // @snippet qapplication-1
  575. QApplicationConstructor(%PYSELF, args, &%0);
  576. // @snippet qapplication-1
  577. // @snippet qapplication-2
  578. PyObject *empty = PyTuple_New(2);
  579. if (!PyTuple_SetItem(empty, 0, PyList_New(0)))
  580. QApplicationConstructor(%PYSELF, empty, &%0);
  581. // @snippet qapplication-2
  582. // @snippet qgraphicsproxywidget-setwidget
  583. QWidget *_old = %CPPSELF.widget();
  584. if (_old)
  585. Shiboken::Object::setParent(nullptr, %CONVERTTOPYTHON[QWidget *](_old));
  586. %CPPSELF.%FUNCTION_NAME(%1);
  587. Shiboken::Object::setParent(%PYSELF, %PYARG_1);
  588. // @snippet qgraphicsproxywidget-setwidget
  589. // @snippet qapplication-exec
  590. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  591. "'exec_' will be removed in the future. "
  592. "Use 'exec' instead.",
  593. 1)) {
  594. return nullptr;
  595. }
  596. %BEGIN_ALLOW_THREADS
  597. int cppResult = %CPPSELF.exec();
  598. %END_ALLOW_THREADS
  599. %PYARG_0 = %CONVERTTOPYTHON[int](cppResult);
  600. // @snippet qapplication-exec
  601. // @snippet qmenu-exec-1
  602. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  603. "'exec_' will be removed in the future. "
  604. "Use 'exec' instead.",
  605. 1)) {
  606. return nullptr;
  607. }
  608. %BEGIN_ALLOW_THREADS
  609. QAction *cppResult = %CPPSELF.exec();
  610. %END_ALLOW_THREADS
  611. %PYARG_0 = %CONVERTTOPYTHON[QAction*](cppResult);
  612. // @snippet qmenu-exec-1
  613. // @snippet qmenu-exec-2
  614. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  615. "'exec_' will be removed in the future. "
  616. "Use 'exec' instead.",
  617. 1)) {
  618. return nullptr;
  619. }
  620. %BEGIN_ALLOW_THREADS
  621. QAction *cppResult = %CPPSELF.exec(%1, %2);
  622. %END_ALLOW_THREADS
  623. %PYARG_0 = %CONVERTTOPYTHON[QAction*](cppResult);
  624. // @snippet qmenu-exec-2
  625. // @snippet qmenu-exec-3
  626. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  627. "'exec_' will be removed in the future. "
  628. "Use 'exec' instead.",
  629. 1)) {
  630. return nullptr;
  631. }
  632. %BEGIN_ALLOW_THREADS
  633. QAction *cppResult = %CPPSELF.exec(%1, %2, %3, %4);
  634. %END_ALLOW_THREADS
  635. %PYARG_0 = %CONVERTTOPYTHON[QAction*](cppResult);
  636. // @snippet qmenu-exec-3
  637. // @snippet qstyleoption-typename
  638. const char *styleOptionType(const QStyleOption *o)
  639. {
  640. switch (o->type) {
  641. case QStyleOption::SO_Default:
  642. break;
  643. case QStyleOption::SO_FocusRect:
  644. return "QStyleOptionFocusRect";
  645. case QStyleOption::SO_Button:
  646. return "QStyleOptionButton";
  647. case QStyleOption::SO_Tab:
  648. return "QStyleOptionTab";
  649. case QStyleOption::SO_MenuItem:
  650. return "QStyleOptionMenuItem";
  651. case QStyleOption::SO_Frame:
  652. return "QStyleOptionFrame";
  653. case QStyleOption::SO_ProgressBar:
  654. return "QStyleOptionProgressBar";
  655. case QStyleOption::SO_ToolBox:
  656. return "QStyleOptionToolBox";
  657. case QStyleOption::SO_Header:
  658. return "QStyleOptionHeader";
  659. case QStyleOption::SO_DockWidget:
  660. return "QStyleOptionDockWidget";
  661. case QStyleOption::SO_ViewItem:
  662. return "QStyleOptionViewItem";
  663. case QStyleOption::SO_TabWidgetFrame:
  664. return "QStyleOptionTabWidgetFrame";
  665. case QStyleOption::SO_TabBarBase:
  666. return "QStyleOptionTabBarBase";
  667. case QStyleOption::SO_RubberBand:
  668. return "QStyleOptionRubberBand";
  669. case QStyleOption::SO_ToolBar:
  670. return "QStyleOptionToolBar";
  671. case QStyleOption::SO_GraphicsItem:
  672. return "QStyleOptionGraphicsItem";
  673. case QStyleOption::SO_Slider:
  674. return "QStyleOptionSlider";
  675. case QStyleOption::SO_SpinBox:
  676. return "QStyleOptionSpinBox";
  677. case QStyleOption::SO_ToolButton:
  678. return "QStyleOptionToolButton";
  679. case QStyleOption::SO_ComboBox:
  680. return "QStyleOptionComboBox";
  681. case QStyleOption::SO_TitleBar:
  682. return "QStyleOptionTitleBar";
  683. case QStyleOption::SO_GroupBox:
  684. return "QStyleOptionGroupBox";
  685. case QStyleOption::SO_SizeGrip:
  686. return "QStyleOptionSizeGrip";
  687. default:
  688. break;
  689. }
  690. return "QStyleOption";
  691. }
  692. // @snippet qstyleoption-typename
  693. // @snippet qwizardpage-registerfield
  694. auto *signalInst = reinterpret_cast<PySideSignalInstance *>(%PYARG_4);
  695. const auto data = PySide::Signal::getEmitterData(signalInst);
  696. if (data.methodIndex == -1)
  697. return PyErr_Format(PyExc_RuntimeError, "QWizardPage::registerField(): Unable to retrieve signal emitter.");
  698. const auto method = data.emitter->metaObject()->method(data.methodIndex);
  699. const QByteArray signature = QByteArrayLiteral("2") + method.methodSignature();
  700. %BEGIN_ALLOW_THREADS
  701. %CPPSELF.%FUNCTION_NAME(%1, %2, %3, signature.constData());
  702. %END_ALLOW_THREADS
  703. // @snippet qwizardpage-registerfield
  704. // The constructor heuristics generate setting a parent-child relationship
  705. // when creating a QDialog with parent. This causes the dialog to leak
  706. // when it synchronous exec() is used instead of asynchronous show().
  707. // In that case, remove the parent-child relationship.
  708. // @snippet qdialog-exec-remove-parent-relation
  709. Shiboken::Object::removeParent(reinterpret_cast<SbkObject *>(%PYSELF));
  710. // @snippet qdialog-exec-remove-parent-relation
  711. // @snippet qmessagebox-open-connect-accept
  712. if (!PySide::callConnect(%PYSELF, SIGNAL(accepted()), %PYARG_1))
  713. return nullptr;
  714. %CPPSELF.%FUNCTION_NAME();
  715. // @snippet qmessagebox-open-connect-accept
  716. // @snippet replace-widget-child
  717. $CHILD_TYPE* oldChild = %CPPSELF.$FUNCTION_GET_OLD();
  718. if (oldChild != nullptr && oldChild != $CPPARG) {
  719. Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[$CHILD_TYPE*](oldChild));
  720. Shiboken::Object::setParent(nullptr, pyChild);
  721. Shiboken::Object::releaseOwnership(pyChild);
  722. }
  723. Shiboken::Object::setParent(%PYSELF, $PYARG);
  724. // @snippet replace-widget-child
  725. /*********************************************************************
  726. * CONVERSIONS
  727. ********************************************************************/
  728. /*********************************************************************
  729. * NATIVE TO TARGET CONVERSIONS
  730. ********************************************************************/