How to use QToolBar and QToolButton in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Introduction[edit | edit source]

This is a small code snippet which shows the use of the toolbar and tool button in Qt. With use of this code snippet one can easily create a small toolbar and adds buttons to it. Here all three buttons are set for same function that is to quit from application.

Code Snippet[edit | edit source]

  1. include <QApplication>
  2. include <QToolButton>
  3. include <QToolBar>
  4. include <QWidget>
  5. include <QVBoxLayout>
  6. include <QIcon>

int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget *win = new QWidget; QToolBar *tool = new QToolBar; tool->setGeometry(0,0,200,20); QVBoxLayout *layout = new QVBoxLayout; QToolButton *button = new QToolButton; button->setGeometry(0,0,10,20);

button->setIcon(QIcon("c://openbutton.png")); QToolButton *button1 = new QToolButton; button1->setIcon(QIcon("c://savebutton.png")); QToolButton *button2 = new QToolButton; button2->setIcon(QIcon("c://exitbutton.png")); tool->addWidget(button); tool->addSeparator(); tool->addWidget(button1); tool->addSeparator(); tool->addWidget(button2); layout->addWidget(tool); win->setLayout(layout); win->showMaximized(); return app.exec(); }


Screenshot[edit | edit source]

File:Qttoolbar1.jpg

Making the toolbar movable[edit | edit source]

The toolbar can be made movable by using the following functions,

Template:Note

  • This function is used to move toolbar in QMainWindow.
toolbar->setMovable(1); 
  • This is used to enable drag and drop toolbar.
toolbar->setFloatable(1); 
  • This is used to know were toolbar is to be place in QMainWindow.
toolbar->setAllowedAreas(Qt::AllToolBarAreas); 

More information[edit | edit source]

pt:Archived:Como usar QToolBar e QToolButton