[SOLVED] Removing titlebar from Qt window while keeping the default window borders

Issue

I’m trying to remove the default Windows titlebar from my Qt window (QT version 5.12.2 and using C++) while still keeping the window borders. I’ve more or less achieved this using the flag Qt::CustomizeWindowHint. However, this changes the window borders to white lines instead of the default borders.

Example of how the borders look AFTER I’ve applied the Qt::CustomizeWindowHint flag:
enter image description here

As you can see, these are not the normal Windows window borders.

How can I change/edit these boders (i.e. change their color) or how am I able to keep the default Windows window borders while removing the titlebar?

Here’s a minimal reproducible example:

main.cpp:

int main(int argc, char* argv[]) {

    QApplication application(argc, argv);
    launcher mainWindow;

    //debugChecks();
    mainWindow.setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::MSWindowsFixedSizeDialogHint);
    mainWindow.setWindowTitle("Test");
    mainWindow.show();

    return application.exec();
}

launcher.h

#pragma once

#include <QtWidgets/QMainWindow>
#include <QMouseEvent>
#include <QPoint>
#include "ui_launcher.h"

class launcher : public QMainWindow {
    Q_OBJECT

public:
    launcher(QWidget* parent = Q_NULLPTR);

private:
    Ui::launcherClass ui;
    void mousePressEvent(QMouseEvent* eventVar);
    void mouseMoveEvent(QMouseEvent* eventVar);
    int mouseClickX = 0;
    int mouseClickY = 0;

};

launcher.cpp

#include "launcher.h"

launcher::launcher(QWidget* parent) : QMainWindow(parent) {
    ui.setupUi(this);
}


void launcher::mousePressEvent(QMouseEvent* eventVar) {

    mouseClickX = eventVar->x();
    mouseClickY = eventVar->y();
}

void launcher::mouseMoveEvent(QMouseEvent* eventVar) {
    move(eventVar->globalX() - mouseClickX, eventVar->globalY() - mouseClickY);
}

Solution

Your description isn’t really clear. One should not be using a QMainWindow for a logon/in dialog. Having said that I created an application on Ubuntu 20.04 (should build fine for you as I used qmake). You can download the project zip here. When the application starts it looks like this:

enter image description here

After clicking on "golden" it looks like this:

enter image description here

After Green it looks like this:

enter image description here

After clicking on Freaky it looks like this:

enter image description here

Please note that FramelessWindowHint not only removes the title bar and system menu along with window frame, it also removes your ability to resize/drag/move the window.

My apologies for not taking out the StackOver1.pro.user file. Didn’t think about it until just now. You will need to delete that or it could hose your build.

Answered By – user3450148

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *