Qt World Summit 2023: Berlin awaits! Join us now
最新版Qt 6.3已正式发布。 了解更多。
最新バージョンQt6.5がご利用いただけます。 詳細はこちら

Mac Toolbars for Qt Quick

As Jens wrote a while ago, we are looking into customizing Qt Quick for use on the desktop. In one of the comments he also revealed that I'm working a Mac-specific project. Time to blog!

Toolbar in action : Nothing we haven't seen on Mac before, although the customize menu is new to Qt applications.

What is different from the current Qt solution? First of all, we're using NSToolBar instead of QToolBar, which means we get the correct native look and feel (including the desired customization menu) for free. Second, the toolbar can be created just like any other QML item:

MacToolBar {
    MacToolButton {
        text : "Button 1"
        iconSource : "images/qtlogo.png"
        toolTip : "This is button number 1"
        onActivated : status.text = "Button1 Clicked"

MacToolButton { standardItem : MacToolButton.ShowColors } MacToolButton { standardItem : MacToolButton.Space } }

MacToolBars are not drawn on the QML scene, but instead attach a NSToolBar to the window.

The color panel can be accessed in a similar fashon:

MacColorPanel {
    onColorChanged : { status.color = color; }

Finally, if you are using Qt from C++ there is also a QAction-based interface:

QWidget *window = ...;
QtMacToolBar * macToolBar = new QtMacToolBar(window);
QAction * fooAction = macToolBar->addAction("FooButton");
QAction * barAction = macToolBar->addAction("BarButton");

Code is at examples/MacToolbar.qml from qt-components/desktop, branch "mac".

Removed ampersand from C++ example.

Blog Topics: