Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any way to use Qt without using dynamic memory for everything?
    primarykey
    data
    text
    <p>So, I think I've searched the web quite thoroughly about this and found nothing really useful (just confusing at most...).</p> <p>I'd like to know how I can (if possible) use Qt with non-dynamic memory. The problem I face is that for many widgets, I know exactly what I want to use (these sub-widgets, these layouts, in fixed numbers, etc.). Yet, everything in Qt seems to get in the way when you don't use dynamic memory. A simple example is <code>QLayout</code>, which <a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qlayout.html#addItem" rel="nofollow">from the Qt documentation</a> is designed to take ownership of anything it is added. So basically, the following code:</p> <pre><code>//In header class ThumbnailDialog : public QDialog { Q_OBJECT public: ThumbnailDialog(QWidget* parent = 0); ~ThumbnailDialog(void); private: QPushButton m_confirm; QPushButton m_cancel; QHBoxLayout m_buttonsLayout; }; //Implementation of ctor ThumbnailDialog::ThumbnailDialog(QWidget* parent): QDialog(parent) { //... m_buttonsLayout.addWidget(&amp;m_confirm); m_buttonsLayout.addWidget(&amp;m_cancel); //... setLayout(&amp;m_dialogLayout); } </code></pre> <p>...will end up (on MSVC) in a debug assertion fail for <code>_BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse)</code> because, in the <code>ThumbnailDialog</code>'s dtor, the layout tries to delete the buttons... which it obviously shouldn't.</p> <p>So, am I forced to use dynamic memories everywhere, <a href="http://qt-project.org/forums/viewthread/10160/#57700" rel="nofollow">as this "Qt Expert"</a> advocates (while mentioning "heap", though...) ? This seems wrong as this prevents taking advantage of RAII (if parent-child relation means there'll be a delete, one can't use smart pointers to do that I suppose, then). It also feels terribly wrong to resort to dynamic memory for things known at compile-time... (but I could be wrong, that's just my feeling).</p> <p>So: is there any way of using Qt without resorting to dynamic memory and <code>new</code>s for every single widget/layout ? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload