Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Crash while attempting to add QWidget to QDialog via QGridLayout
Forum Updated to NodeBB v4.3 + New Features

[solved] Crash while attempting to add QWidget to QDialog via QGridLayout

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    minbraz
    wrote on last edited by
    #1

    Hi,

    I have started learning Qt, recently. So, I could have overlooked something here. The crash occurs when I try to repeatedly add QWidget object to subclassed QDialog. There is the code that reproduces the crash:

    @
    #include <QApplication>
    #include <QDialog>
    #include <QGridLayout>
    #include <QLabel>

    class Gui {

    private:
    QWidget mWidget;
    public:
    Gui(){ mWidget=nullptr; }
    QWidget
    getWidget(){

    if(mWidget == nullptr){

    // adding layout
    mWidget = new QWidget();
    QGridLayout *gridLayout = new QGridLayout();
    mWidget->setLayout(gridLayout);
    
    // adding label
    QLabel *label = new QLabel("Label");
    gridLayout->addWidget(label, 0,0);
    

    }

    return mWidget;
    }

    };

    class GuiContainer : public QDialog{

    private:
    Gui *cGui;
    QGridLayout *mGridLayout;

    public:
    GuiContainer(Gui *gui){
    cGui = gui;
    mGridLayout = new QGridLayout();
    /A/setLayout(mGridLayout);
    /A/mGridLayout->addWidget(cGui->getWidget()); // crash here

    // another crash scenario
    ///B/mGridLayout->addWidget(cGui->getWidget());
    ///B/setLayout(mGridLayout); // blocks here

    resize(100, 100);
    }
    ~GuiContainer(){
    if(layout()){
    mGridLayout->removeWidget(cGui->getWidget());
    delete mGridLayout;
    }
    }

    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Gui *gui = new Gui();

    GuiContainer *guiContainerA = new GuiContainer(gui);
    delete guiContainerA;

    GuiContainer *guiContainerB = new GuiContainer(gui); // crash here
    delete guiContainerB;

    return a.exec();

    }
    @

    I would appreciate your thoughts on this.

    Additional information:
    OS: Windows 7; Qt: 5.2.1; compiler: msvc2012, 64bits.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      When deleting guiContainerA, you delete also all children thus mWidget from Gui, so with guiContainerB you try to access an invalid pointer.

      It's a pretty strange design, what are you trying to achieve ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        minbraz
        wrote on last edited by
        #3

        Thank you for your replay. This solved the issue:
        @
        if(layout()){
        mGridLayout->removeWidget(cGui->getWidget());
        cGui->getWidget()->setParent(nullptr); // parent removal
        delete mGridLayout;
        }
        @

        Design... well, let me show you a bigger picture. There is what I am trying to achieve "summary":https://www.dropbox.com/s/ath03c34zyz4mza/2014-08-04_summary.docx . Currently, I have decided to use QDockWidget instead of QDialog. p.s. sorry for my English.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved