Using SARibbon in Non-MainWindow Scenarios¶
This guide explains how to integrate SARibbon in QDialog, QWidget sub-windows, QMdiSubWindow, and other non-QMainWindow scenarios.
SARibbonWidget Overview¶
SARibbonWidget is the QWidget-based container class provided by SARibbon, designed specifically for scenarios that do not require a full QMainWindow. It unifies management of SARibbonBar and the content area, using an internal layout to keep the Ribbon fixed at the top.
Core Features¶
| Feature | Description |
|---|---|
| No QMainWindow dependency | Can be embedded in QDialog, QWidget, QMdiSubWindow, or any other container |
| Automatic layout management | Uses an internal layout to automatically place SARibbonBar at the top, with the content area filling the remaining space |
| Q_PROPERTY support | Exposes the ribbonTheme property, supporting the Qt property system and quick theme switching |
| Full Ribbon functionality | Same complete Ribbon capabilities as SARibbonMainWindow (category pages, panels, quick access bar, etc.) |
SARibbonWidget vs SARibbonMainWindow Comparison¶
| Comparison Aspect | SARibbonMainWindow | SARibbonWidget |
|---|---|---|
| Base class | QMainWindow (comes with menu bar, toolbar, status bar, etc.) |
QWidget (lightweight, no predefined areas) |
| ribbonBar() access | SARibbonMainWindow::ribbonBar() auto-created |
SARibbonWidget::ribbonBar() auto-created |
| System button bar | Provides windowButtonBar() — minimize, maximize, close and other system buttons |
No system button bar, requires manual management of window frame and buttons |
| Window frame | Custom frameless (FramelessHelper) or native frame options available | Uses the host window's frame/attributes, no frameless handling of its own |
| Content area setting | setCentralWidget() |
setWidget() |
| Applicable scenarios | Application main window, scenarios requiring full window management | Dialogs, sub-windows, tab pages, MDI sub-windows, plugin containers, etc. |
| Layout considerations | No need to manually layout the Ribbon | Cannot add a layout to SARibbonWidget itself; instead, set content via setWidget() |
SARibbonWidget Class API Summary¶
Q_PROPERTY¶
1 | |
| Property Name | Type | Accessors | Description |
|---|---|---|---|
ribbonTheme |
SARibbonTheme |
ribbonTheme() / setRibbonTheme() |
Ribbon theme (supports 6 built-in themes) |
Key Methods¶
| Method Signature | Description |
|---|---|
SARibbonBar* ribbonBar() const |
Get the internal SARibbonBar pointer |
void setRibbonBar(SARibbonBar* bar) |
Set a custom SARibbonBar instance |
void setRibbonTheme(SARibbonTheme theme) |
Set the Ribbon theme |
SARibbonTheme ribbonTheme() const |
Get the current theme |
bool isUseRibbon() const |
Check whether currently in Ribbon mode |
void setWidget(QWidget* w) |
Set the content area widget |
QWidget* widget() const |
Get the current content area widget |
QWidget* takeWidget() |
Remove and return the content area widget (ownership transferred) |
Usage Scenarios and Code Examples¶
Scenario 1: Using Ribbon Menu in QDialog¶
Suitable for complex dialogs that need a Ribbon-style interface (such as settings dialogs, data editing dialogs).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
Scenario 2: Using Ribbon in QWidget Sub-windows¶
Suitable for sub-panels that are part of a main window (such as tab pages in a QTabWidget), where each tab page has its own independent Ribbon menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
Scenario 3: Using Ribbon in MDI Sub-windows¶
Embed an independent Ribbon interface in each child window of a QMdiArea.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
Layout Considerations¶
Layout Optimization in Widget Mode
Core rule: Do not call
setLayout()onSARibbonWidgetitself.
SARibbonWidgetalready uses an internal layout to keepSARibbonBarfixed at the top. If you add an additional layout to it, it will cause layout conflicts and abnormal Ribbon display.Correct approach: Set the content widget into
SARibbonWidgetviasetWidget(), then placeSARibbonWidgetinto the host window's layout:
1 2 3 4 5 6 7 8 9 10// ✅ Correct SARibbonWidget* rw = new SARibbonWidget(); rw->setWidget(myContentWidget); // Content managed by SARibbonWidget parentLayout->addWidget(rw); // Add SARibbonWidget to parent layout // ❌ Wrong SARibbonWidget* rw = new SARibbonWidget(); QVBoxLayout* layout = new QVBoxLayout(rw); // This will break the internal layout! layout->addWidget(rw->ribbonBar()); layout->addWidget(myContentWidget);Recommended compact settings: In Widget mode, the Ribbon should occupy as little space as possible. It is recommended to uniformly set:
1 2 3ribbon->setTitleVisible(false); ribbon->setApplicationButton(nullptr); ribbon->setRibbonStyle(SARibbonBar::RibbonStyleCompactThreeRow);For space-constrained scenarios (e.g., embedded devices or small windows), single-row mode is recommended:
1 2 3 4ribbon->setTitleVisible(false); ribbon->setApplicationButton(nullptr); ribbon->setRibbonStyle(SARibbonBar::RibbonStyleCompactSingleRow); ribbon->setEnableIconRightText(true);
Theme Switching Considerations¶
Setting the theme directly in the SARibbonWidget constructor may not take full effect. It is recommended to use QTimer to defer execution until the end of the event queue:
1 2 3 4 5 6 7 8 9 10 | |
Using Together with SARibbonMainWindow¶
In a SARibbonMainWindow main window, dialogs or sub-windows can use SARibbonWidget to maintain a consistent interface style:
1 2 3 4 5 6 7 8 9 10 11 12 | |
This combination approach allows the main window to retain full window management capabilities, while sub-components also get a consistent Ribbon experience.