title: Qt C++ Creating toolbar
body: <p>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.
  However I am having problem with toolbar creating.</p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      setAttribute(Qt::WA_DeleteOnClose);
      setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
  
      connect(ui-&gt;docWidget-&gt;document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
  
      createActions();
      createMenu();
      createToolbars();
  
      statusBar()-&gt;showMessage("Done");    
  }
  </code></pre>
  
  <p>It is the constructor of the main window.</p>
  
  <pre><code>void MainWindow::createToolbars()
  {
      QToolBar* toolbar;
      toolbar = addToolBar(tr("File"));
      toolbar-&gt;addAction(anyaction);
  }
  </code></pre>
  
  <p>This is how the book create the toolbar.
  However, when I try to run the program, there are two toolbars created.
  One is the toolbar created by the code and called "File"
  Another is a blank toolbar created by the ui designer ie. <code>*ui.toolbar</code>.</p>
  
  <p>In order to get rid of two toolbars, I tried using only the <code>*ui.toolbar</code>.
  It's working. The code is shown below.</p>
  
  <pre><code>void MainWindow::createToolbars()
  {
      ui-&gt;toolBar-&gt;addAction(anyaction);
  }
  </code></pre>
  
  <p>But I tried to create the toolbar by code only, ie. not adding a toolbar in the ui designer.
  So I write this:</p>
  
  <pre><code>void MainWindow::createToolbars()
  {
      QToolBar* FileBar = this-&gt;addToolBar(tr("File"));
      FileBar-&gt;addAction(anyaction);
  }
  </code></pre>
  
  <p>However, there is a compile error.
  The compiler use this function:</p>
  
  <pre><code>void QMainWindow::addToolBar(QT::ToolBarArea area, QToolBar * toolbar)
  </code></pre>
  
  <p>instead of what I want:</p>
  
  <pre><code>QToolBar * QMainWindow::addToolBar(const QString &amp; title)
  </code></pre>
  
  <p><a href="http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3" rel="nofollow">http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3</a></p>
  
  <p>What is my mistake here?</p>