简体   繁体   中英

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?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM