9

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.

tom
  • 1,302
  • 1
  • 16
  • 30
  • Did you try it without tr()? – Gombat Sep 09 '15 at 11:36
  • I tried it without `tr()`. It creates the toolbar but not returning a pointer to the toolbar. So, I cannot use `FileBar->addAction(anyaction);` – tom Sep 09 '15 at 11:40
  • tr() does nothing but translation stuff. It cannot be the reason it does not work – Gombat Sep 09 '15 at 11:43
  • 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 Sep 09 '15 at 11:58
  • 1
    possible duplicate of [When setting the WA\_DeleteOnClose attribute on a Qt MainWindow, the program crashes when deleting the ui pointer](http://stackoverflow.com/questions/3153155/when-setting-the-wa-deleteonclose-attribute-on-a-qt-mainwindow-the-program-cras) – drescherjm Sep 09 '15 at 12:00
  • @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? – tom Sep 09 '15 at 12:11
  • @tom It means to not use Qt::WA_DeleteOnClose for this. – drescherjm Sep 09 '15 at 13:45
  • `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. – drescherjm Sep 09 '15 at 21:17
  • 1
    @drescherjm OK Question moved to here. [link](http://stackoverflow.com/questions/32492897/qt-c-creating-toolbar) – tom Sep 10 '15 at 04:00

1 Answers1

7

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.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
Gombat
  • 1,994
  • 16
  • 21
  • 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 – tom Sep 09 '15 at 11:50