- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Menus & toolbars in Qyoto
In this part of the Visual Basic Qyoto programming tutorial, we will work with menus and toolbars.
A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application. Menus group commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.
Simple menu
The first example will show a simple menu.
' ZetCode Mono Visual Basic Qt tutorial ' ' This program shows a simple ' menu. It has one action, which ' will terminate the program, when ' selected. ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Qyoto Public Class VBQApp Inherits QMainWindow Public Sub New() Me.SetWindowTitle("Simple menu") Me.InitUI() Me.Resize(250, 200) Me.Move(300, 300) Me.Show() End Sub Private Sub InitUI() Dim quit As New QAction("&Quit", Me) Dim file As QMenu = Me.MenuBar().AddMenu("&File") file.AddAction(quit) Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")) End Sub Public Shared Sub Main(ByVal args() As String) Dim qapp As New QApplication(args) Dim app as New VBQApp QApplication.Exec() End Sub End Class
We have a menubar, a menu and an action. In order to work with menus, we must inherit from QMainWindow
widget.
Dim quit As New QAction("&Quit", Me)
This code line creates a QAction
. Each QMenu
has one or more action objects. Note the ampersand (&) character. It creates a shortcut for the item: Alt+Q. It also underlines the Q character. The shortcut is active, when the file menu is dropped down.
Dim file As QMenu = Me.MenuBar().AddMenu("&File") file.AddAction(quit)
We create a QMenu
object. The ampersand character creates a shortcut: Alt+F. The consecutive shortcuts Alt+F, Alt+Q quit the application.
Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()"))
When we select this option from the menu, the application quits.

Creating a submenu
A submenu is a menu plugged into another menu object. The next example demonstrates this.
' ZetCode Mono Visual Basic Qt tutorial ' ' This program creates ' a submenu ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Qyoto Public Class VBQApp Inherits QMainWindow Public Sub New() Me.SetWindowTitle("Submenu") Me.InitUI() Me.Resize(280, 200) Me.Move(300, 300) Me.Show() End Sub Private Sub InitUI() Dim quit As New QAction("&Quit", Me) Dim file As QMenu = MenuBar().AddMenu("&File") Dim impm As New QMenu("Import") Dim seeds As New QAction("Import news feed...", Me) Dim marks As New QAction("Import bookmarks...", Me) Dim mail As New QAction("Import mail...", Me) impm.AddAction(seeds) impm.AddAction(marks) impm.AddAction(mail) file.AddMenu(impm) file.AddAction(quit) Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")) End Sub Public Shared Sub Main(ByVal args() As String) Dim qapp As New QApplication(args) Dim app As New VBQApp QApplication.Exec() End Sub End Class
In the example, we have three options in a submenu of a file menu.
Dim file As QMenu = MenuBar().AddMenu("&File") Dim impm As New QMenu("Import")
We have two QMenu
objects. The file menu and the import menu.
Dim seeds As New QAction("Import news feed...", Me) Dim marks As New QAction("Import bookmarks...", Me) Dim mail As New QAction("Import mail...", Me)
We create three action objects.
impm.AddAction(seeds) impm.AddAction(marks) impm.AddAction(mail)
We add the action objects into the import menu.
file.AddMenu(impm)
Finally, we add the import menu into the file menu.

Images, menus, separators
In the following example, we will further enhance our previous application. We will add icons to the menus, use shortcuts and a separator.
' ZetCode Mono Visual Basic Qt tutorial ' ' This program shows image ' menu items, a shorcut and a separator ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Qyoto Public Class VBQApp Inherits QMainWindow Public Sub New() Me.SetWindowTitle("Image menu") Me.InitUI() Me.Resize(280, 200) Me.Move(300, 300) Me.Show() End Sub Private Sub InitUI() Dim newpix As New QIcon("new.png") Dim openpix As New QIcon("open.png") Dim quitpix As New QIcon("quit.png") Dim newa As New QAction(newpix, "&New", Me) Dim open As New QAction(openpix, "&Open", Me) Dim quit As New QAction(quitpix, "&Quit", Me) quit.Shortcut = New QKeySequence("Ctrl+Q") Dim file As QMenu = MenuBar().AddMenu("&File") file.AddAction(newa) file.AddAction(open) file.AddSeparator() file.AddAction(quit) Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")) End Sub Public Shared Sub Main(ByVal args() As String) Dim qapp As New QApplication(args) Dim app As New VBQApp QApplication.Exec() End Sub End Class
In our example, we have one menu with three actions. Only the quit action will actually do something if we select it. We also create a separator and a Ctrl+Q shortcut, which will terminate the application.
Dim newpix As New QIcon("new.png") Dim openpix As New QIcon("open.png") Dim quitpix As New QIcon("quit.png")
These are PNG images that we will use in the application.
Dim newa As New QAction(newpix, "&New", Me) Dim open As New QAction(openpix, "&Open", Me) Dim quit As New QAction(quitpix, "&Quit", Me)
Here we create three action objects. The first parameter is the QIcon
.
quit.Shortcut = New QKeySequence("Ctrl+Q")
This line creates a shortcut. By pressing this shortcut, we will run the quit action, which will terminate the application.
file.AddSeparator()
We create a separator. The separator is a horizontal line, which enables us to group menu actions into some logical parts.

A toolbar
The QToolBar
class provides a movable panel that contains a set of controls, which provide a quick access to the application actions.
' ZetCode Mono Visual Basic Qt tutorial ' ' This program creates a ' toolbar ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Qyoto Public Class VBQApp Inherits QMainWindow Public Sub New() Me.SetWindowTitle("Toolbar") Me.InitUI() Me.Resize(280, 200) Me.Move(300, 300) Me.Show() End Sub Private Sub InitUI() Dim newpi As New QIcon("new.png") Dim openpi As New QIcon("open.png") Dim quitpi As New QIcon("quit.png") Dim toolbar As QToolBar = AddToolBar("main toolbar") toolbar.AddAction(newpi, "New File") toolbar.AddAction(openpi, "Open File") toolbar.AddSeparator() Dim quit As QAction = toolbar.AddAction(quitpi, _ "Quit Application") Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")) End Sub Public Shared Sub Main(ByVal args() As String) Dim qapp As New QApplication(args) Dim app as New VBQApp QApplication.Exec() End Sub End Class
We create a toolbar with three action objects and one separator.
Dim newpi As New QIcon("new.png") Dim openpi As New QIcon("open.png") Dim quitpi As New QIcon("quit.png")
Toolbar action objects will display these icons.
Dim toolbar As QToolBar = AddToolBar("main toolbar")
The AddToolBar()
method of the QMainWindow
class creates a toolbar for the application. The text string gives a toolbar a name. This name is used to reference this toolbar, because there can be multiple toolbars in one application. If we right click on the window area, we can see a checkable option, which shows/hides the toolbar.
toolbar.AddSeparator()
We create a vertical separator.
Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()"))
When we click on the quit action object, the application terminates.

Undo redo
The following example demonstrates, how we can deactivate toolbar buttons on the toolbar. It is a common practice in GUI programming. For example the save button. If we save all changes of our document to the disk, the save button is deactivated in most text editors. This way the application indicates to the user that all changes are already saved.
' ZetCode Mono Visual Basic Qt tutorial ' ' This program disables/enables ' toolbuttons on a toolbar ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Qyoto Public Class VBQApp Inherits QMainWindow Dim clicks As Integer = 0 Dim undoButton As QToolButton Dim redoButton As QToolButton Public Sub New() Me.SetWindowTitle("Undo redo") Me.InitUI() Me.Resize(280, 200) Me.Move(300, 300) Me.Show() End Sub Private Sub InitUI() Dim undoi As New QIcon("undo.png") Dim redoi As New QIcon("redo.png") Dim quitpi As New QIcon("quit.png") Dim toolbar As New QToolBar undoButton = New QToolButton redoButton = New QToolButton Dim undoAction As New QAction(undoi, "Undo", undoButton) Dim redoAction As New QAction(redoi, "Redo", redoButton) undoButton.SetDefaultAction(undoAction) redoButton.SetDefaultAction(redoAction) toolbar.AddSeparator() toolbar.AddWidget(undoButton) toolbar.AddWidget(redoButton) Dim quit As QAction = toolbar.AddAction(quitpi, "Quit Application") Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")) Connect(undoButton, SIGNAL("triggered(QAction*)"), _ Me, SLOT("Count(QAction*)")) Connect(redoButton, SIGNAL("triggered(QAction*)"), _ Me, SLOT("Count(QAction*)")) AddToolBar(toolbar) End Sub <Q_SLOT()> _ Private Sub Count(ByVal action As QAction) If "Undo".Equals(action.Text) clicks -= 1 Else clicks += 1 End If If clicks <= 0 undoButton.SetDisabled(True) redoButton.SetDisabled(False) End If If clicks >= 5 undoButton.SetDisabled(False) redoButton.SetDisabled(True) End If End Sub Public Shared Sub Main(ByVal args() As String) Dim qapp As New QApplication(args) Dim app As New VBQApp QApplication.Exec() End Sub End Class
In our example, we have three QAction
objects and a separator. After several clicks on the undo or redo buttons, they become deactivated. Visually, the buttons are grayed out.
Dim clicks As Integer = 0
The clicks variable determines, which button is activated or deactivated.
Connect(undoButton, SIGNAL("triggered(QAction*)"), _ Me, SLOT("Count(QAction*)")) Connect(redoButton, SIGNAL("triggered(QAction*)"), _ Me, SLOT("Count(QAction*)"))
Clicking on the toolbar button, the triggered()
signal is emitted. We connect this signal to the Count()
method. It receives the QAction
object, which triggered it.
If "Undo".Equals(action.Text) clicks -= 1 Else clicks += 1 End If
The undo toolbar button subtracts 1 from the clicks variable. The redo adds 1. Depending on the value of the clicks variable, we enable/disable the toolbar buttons.
If clicks <= 0 undoButton.SetDisabled(True) redoButton.SetDisabled(False) End If
The SetDisabled()
method activates or deactivates the toolbar buttons.

In this part of the Visual Basic Qyoto tutorial, we mentioned menus and toolbars.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论