To remove the OS window frame from a QWidget, use the FramelessWidgetsHelper class.
1. Initialization
You MUST call FramelessHelper::Widgets::initialize() in your main function before any QApplication or QGuiApplication objects are constructed.
2. Applying Frameless Mode
Use the static method FramelessWidgetsHelper::get(QObject *) to obtain a helper instance for your widget. This method is safe to call multiple times; it returns the existing instance if one is already attached to that widget's top-level window.
To hide the default OS title bar, call extendsContentIntoTitleBar() on the helper instance. It is recommended to do this early in the widget's lifecycle.
3. Custom Title Bar and Hit Testing
- Draggability: To make the window draggable, provide a custom widget and register it using
setTitleBarWidget(QWidget *). - Interactivity: By default,
FramelessHelper intercepts mouse and keyboard events in the title bar area. To allow specific widgets (like buttons or search boxes) inside your custom title bar to receive events, you must call setHitTestVisible(QWidget *) for each of those widgets.
4. Lifecycle and Safety
- Parenting: The helper objects are automatically parented to the top-level widget.
- Memory Management: Do not manually delete the
FramelessWidgetsHelper object. Qt will handle its deletion automatically. - Initialization Timing: If you need to perform operations that depend on the window being fully customized (like changing geometry or flags), connect to the
ready() signal of the FramelessWidgetsHelper instance.
// 1. Initialize in main
int main(int, char **)
{
FramelessHelper::Widgets::initialize();
// ...
}
// 2. Apply to widget
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
FramelessWidgetsHelper::get(this)->extendsContentIntoTitleBar();
}
// 3. Set custom title bar
void MyWidget::myFunction()
{
FramelessWidgetsHelper::get(this)->setTitleBarWidget(m_myTitleBarWidget);
}
// 4. Enable interaction for specific widgets
void MyWidget::myFunction2()
{
FramelessWidgetsHelper::get(this)->setHitTestVisible(m_someSearchBox);
FramelessWidgetsHelper::get(this)->setHitTestVisible(m_someButton);
}