Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt frameless window at specified coordinates

After much googling I'm aware that I need to use self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

to generate a frameless window, and some variation of the co-ordinates (0,0) to place it in the upper left corner of the screen.

The problem I'm having is that everywhere that I've tried using this code, it generates errors, even in my sparse existing code:

from PyQt4 import QtCore, QtGui
from ui.mainwindow import MainWindow

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

After much googling I'm unable to make solutions that work for others work for me. What is the magic syntax to get my window displayed without borders in the upper left corner of the screen?

like image 272
Adam_ABC Avatar asked Jan 09 '23 18:01

Adam_ABC


2 Answers

It turns out I have a lot to learn about Python and hierarchical inheritance/ what belongs to what-ness, the code I was looking for is:

from PyQt4 import QtCore, QtGui
from ui.mainwindow import MainWindow

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = MainWindow()
    ui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    ui.move(0, 0)
    ui.show()
    sys.exit(app.exec_())

The eureka moment was realising that MainWindow is masquerading in shorthand under the variable ui. For the benefit of others as nooby as me.

like image 174
Adam_ABC Avatar answered Jan 17 '23 17:01

Adam_ABC


Basically if you just set the Frameless flag, you are still holding on to a bunch of default flags that get applied to QWidgets or QMainWindows.

If you also include the CustomizeWindowHint which will clear all the previous flags, and then you bitwise or in the additional flags you want to use: see the | operator.

http://doc.qt.io/qt-5/qt.html#WindowType-enum

http://doc.qt.io/qt-5/qwidget.html#setAttribute

http://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum

http://doc.qt.io/qt-5/qwidget.html#pos-prop

http://doc.qt.io/qt-5/application-windows.html#window-geometry

I'll post a code snippet in a few minutes.

UPDATE:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::Tool);
//    w.setAttribute(Qt::WA_TranslucentBackground);
    w.move(0,0);
    w.show();

    return a.exec();
}

Hope that helps.

like image 21
phyatt Avatar answered Jan 17 '23 18:01

phyatt