Qt toolbar toogle view action

Hello now I want share how to create toolbar view action, that mean is to show and hide the toolbar from the menubar. You can look the apps like this:

Qt toolbar apps
Figure 1. Qt toolbar apps

Ok now create the source code, first you must create a new project (example name project is test):

Project structure
Figure 2. Project structure

Now open your main_window.h and put this source:

#ifndef MAIN_WINDOW_H_
#define MAIN_WINDOW_H_

#include <QMainWindow>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>

class MainWindow : public QMainWindow {
    Q_OBJECT

 public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

 private:
  void CreateAct();
  void CreateMenus();
  void createToolBars();

  QMenu* window_menu_;
  QAction* undo_act_;
  QAction* redo_act_;
  QAction* cut_act_;
  QAction* copy_act_;
  QAction* paste_act_;
  QAction* edit_toolbar_act_;
  QAction* file_toolbar_act_;
  QToolBar* file_toolbar_;
  QToolBar* edit_toolbar_;
};

#endif  // MAIN_WINDOW_H_

And for the main_window.cc, put this source:

#include "main_window.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent) {
  setMinimumSize(800, 600);

  CreateAct();
  createToolBars();
  CreateMenus();
}

void MainWindow::CreateAct() {
  undo_act_ = new QAction(QIcon::fromTheme("edit-undo",
                         QIcon(":/images/undo.png")), tr("&Undo"), this);
  redo_act_= new QAction(QIcon::fromTheme("edit-redo",
                         QIcon(":/images/redo.png")), tr("&Redo"), this);
  cut_act_ = new QAction(QIcon::fromTheme("edit-cut",
                         QIcon(":/images/cut.png")), tr("Cu&t"), this);
  copy_act_ = new QAction(QIcon::fromTheme("edit-copy",
                          QIcon(":/images/copy.png")), tr("&Copy"), this);
  paste_act_ = new QAction(QIcon::fromTheme("edit-paste",
                           QIcon(":/images/paste.png")), tr("&Paste"), this);

  edit_toolbar_act_ = new QAction(tr("Show edit toolbar"), this);
  edit_toolbar_act_->setCheckable(true);
  edit_toolbar_act_->setChecked(true);

  file_toolbar_act_ = new QAction(tr("Show file toolbar"), this);
  file_toolbar_act_->setCheckable(true);
  file_toolbar_act_->setChecked(true);
}

void MainWindow::CreateMenus() {
  window_menu_ = menuBar()->addMenu(tr("&Window"));
  window_menu_->addAction(file_toolbar_act_);
  window_menu_->addAction(edit_toolbar_act_);
}

void MainWindow::createToolBars() {
  file_toolbar_ = addToolBar("File toolbar");
  file_toolbar_act_ = file_toolbar_->toggleViewAction();
  file_toolbar_->addAction(undo_act_);
  file_toolbar_->addAction(redo_act_);
  file_toolbar_->toggleViewAction();
  file_toolbar_->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);

  edit_toolbar_ = addToolBar(tr("Edit toolbar"));
  edit_toolbar_act_ = edit_toolbar_->toggleViewAction();
  edit_toolbar_->addAction(cut_act_);
  edit_toolbar_->addAction(copy_act_);
  edit_toolbar_->addAction(paste_act_);
  edit_toolbar_->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
}

MainWindow::~MainWindow() {}

Explanations: I assume you know to create simple apps using Qt. Like add label, lineedit, button because I was only explain how to create toggle view action. From the main_window.cc source, look line number 25,26,29,30. That mean is I create the menu item with checkable.

file_toolbar_act_-&gt;setCheckable(true);

And the default menu item is selected enable.

file_toolbar_act_->setChecked(true);

And for show and hide the toolbar look at the code highlighted line 41 and 48 with this code.

file_toolbar_act_ = file_toolbar_->toggleViewAction();
edit_toolbar_act_ = edit_toolbar_->toggleViewAction();

And for the last open main.cc and put this source:

#include <QApplication>
#include "main_window.h"

int main(int argc, char *argv[]) {
  Q_INIT_RESOURCE(application);

  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  return a.exec();
}

Now build and run the project. If there is no error, then it will look like the image above.
Source: http://stackoverflow.com/a/27296293
Source code: http://downloads.ziddu.com/download/24293324/Toolbar.7z.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s