Qt::WA_DeleteOnClose

13,916

Qt takes care of the deletion by itself if you set all the parenting right (if you create a new QObject/QWidget set the parent in the constructor). If the parent will be destructed, then the children will be too. In your main file, you can create the mainwindow on the stack, such that it will be destructed at the end of scope.

To call addToolbar you don't need this->, since it is a method of the class anyway.

The toolbar ptr should be a member to access it easily later on. But initialize it with nullptr (or NULL if you don't have c++11) in the initialization list of the constructor to know whether it is initialized or not.

The addToolBar call should work. A workaround would be to create a QToolBar on your own and add the pointer to the MainWindow using another addToolBar overload.

Share:
13,916
tom
Author by

tom

Updated on June 05, 2022

Comments

  • tom
    tom almost 2 years

    I am learning Qt and trying some examples in the book "Foundations of Qt Development". In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setAttribute(Qt::WA_DeleteOnClose);
        setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
    
        connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
    
        createActions();
        createMenu();
        createToolbars();
    
        statusBar()->showMessage("Done");    
    }
    

    The book said that "Setting the windows attribute to Qt::WA_DeleteOnClose so that Qt takes care of deleting the window from memory as soon as it is closed.

    How it works?

    Because if i use setAttribute(Qt::WA_DeleteOnClose);, when I end the program, there is a Debug Assertion Failed warning:_BLOCK_TYPEIS_VALID(pHead->nBlockUse). There is no problem if the setAttribute is removed.

    • Gombat
      Gombat over 8 years
      Did you try it without tr()?
    • tom
      tom over 8 years
      I tried it without tr(). It creates the toolbar but not returning a pointer to the toolbar. So, I cannot use FileBar->addAction(anyaction);
    • Gombat
      Gombat over 8 years
      tr() does nothing but translation stuff. It cannot be the reason it does not work
    • drescherjm
      drescherjm over 8 years
      You are setting setAttribute(Qt::WA_DeleteOnClose); on MainWindow. Can you show your main()? I bet you have it on the stack. So the problem is you are deleting a stack variable with setAttribute(Qt::WA_DeleteOnClose);
    • drescherjm
      drescherjm over 8 years
    • tom
      tom over 8 years
      @drescherjm Yes, the MainWindow is created on the stack. Thanks. Does it mean if I use new to create the MainWindow, I don't need to delete it if I set the Qt::WA_DeleteOnClose attribute?
    • drescherjm
      drescherjm over 8 years
      @tom It means to not use Qt::WA_DeleteOnClose for this.
    • drescherjm
      drescherjm over 8 years
      The part below is solved. You probably should have started 2 separate questions. I recommend you still do this. Since @Gombat answered the second part edit this question removing the first question and start a new first question with just the contents of first question.
    • tom
      tom over 8 years
      @drescherjm OK Question moved to here. link
  • tom
    tom over 8 years
    I think QToolBar * FileBar = this->addToolBar(tr("File")) is exactly the same as QToolBar * FileBar = addToolBar(tr("File")) which is not the reason of the error