FramelessWindowHint fails at runtime on MainWindow



  • When I execute setWindowFlags(Qt::FramelessWindowHint) before the main window is shown this works, but if I try to execute after the main window is shown the window disappears from the screen and the task manager but is still running. I have to kill it in Qt Creator using the stop button.

    Here is some test code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        // ui is vanilla window with one pushbutton
        ui->setupUi(this);
    
        // works here before window shows
        // setWindowFlags(Qt::FramelessWindowHint);
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        // causes window to disappear and not visible in task
        // manager but it is still running
        setWindowFlags(Qt::FramelessWindowHint);
    }
    

    Qt 5.5 on Windows 10

    Any help is much appreciated!


  • Qt Champions 2016

    @Rory_1 said:

    Qt::FramelessWindowHint

    Might be bug in newer version
    https://forum.qt.io/topic/53502/toggle-window-frames-qt-framelesswindowhint-in-qt-5-4-1

    why cant you just set in constructor ?
    need to toggle it ?



  • @mrjj

    Yes, I want to toggle.


  • Qt Champions 2016

    @Rory_1
    ok. i get same result on win 7, qt 5.5 so its a bug it seems.

    there is showFullScreen(); if you need for something like that.

    can I ask what case you need borderless window for?



  • @mrjj

    Mainly for ascetics and screen real estate. I am building a raw image viewer and want to be able to toggle to a slideshow view to maximize the image size. When viewing images vertical space is always at a premium for portrait oriented images, so anything I can eliminate is helpful.


  • Qt Champions 2016

    @mrjj said:

    well I assume fullscreen is too drastic ?

    One option you have is to use a dialog for viewing the image. When change to borderless mode, you can simply
    create a new instance where you set the flag in the constructor. (and delete the old one)

    It would also be possible to do this with mainwindow from main, but would be slightly more haxish.


  • Moderators

    That's not a bug. That's just how window managers work. You shouldn't change the frame on a visible window.
    And btw. you should just add the hint, not replace all of them i.e. to make it frameless:

    hide();
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    show();
    

    and to go back:

    hide();
    setWindowFlags(windowFlags() & ~Qt::FramelessWindowHint);
    show();
    

  • Qt Champions 2016

    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

    So clear when you see it :)

    Can you tell why it does seem work in ctor after ui setup
    setWindowFlags(Qt::FramelessWindowHint);

    Even if not OR ing.


  • Moderators

    Can you tell why it does seem work in ctor after ui setup

    It's an (un)fortunate coincidence. The default flags are something like:
    Window|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint|WindowFullscreenButtonHint
    If you call setWindowFlags(Qt::FramelessWindowHint) it becomes Window|FramelessWindowHint (you can't really loose the Window flag), so you loose all the hint flags. It seems to work, since frameless window doesn't use them anyway, but if you tried to go back or if somebody set some other flags (either with setWindowFlags or via the second param of QWidget's constructor) you'd loose them and had no way to know what they were.

    As to why did the setWindowFlags call seem to work in the constructor and didn't later, it's because when a constructor is run the widget is not yet shown, so changing flags is ok there.


  • Qt Champions 2016

    Ah so cannot really clear the Window flag and hence we see no ill effect when only using the default values.
    So always OR. Even in ctor.



  • @Chris-Kawa

    Thanks so much Chris for the solution and the clear explanation. Sorry about my inexperience, but how do I show the issue has been solved?

    Best regards
    Rory


  • Qt Champions 2016

    @Rory_1
    hi
    Use the Topic Tools button to mark as Solved.
    its sort below topic/post to the right.



  • @mrjj Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.