PyQt frameless window at specified coordinates

為{幸葍}努か 提交于 2021-02-04 20:05:07

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/28960298/pyqt-frameless-window-at-specified-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!