简体   繁体   中英

How to get complete fullscreen with PyQt4

I am having trouble getting a QWidget to real fullscreen in PyQt 4.8 . I took two approaches:

  1. Code directly (this works)

     import sys from PyQt4 import QtGui, QtCore import qimage2ndarray as q2n import numpy as np from scipy.misc.common import lena class FullscreenWindow(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.qg = QtGui.QGraphicsView(self) self.scene = QtGui.QGraphicsScene(self) self.qg.setScene(self.scene) # Make window fullscreen and always on top self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) self.setWindowFlags(QtCore.Qt.FramelessWindowHint) # set the image (lena) qimg = q2n.array2qimage(np.pad(lena(), ((0,0),(0,0)), mode='constant')) pix = QtGui.QPixmap(qimg) self.scene.clear() self.scene.addPixmap(pix) self.show() class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.fullscreen = FullscreenWindow() qdw = QtGui.QDesktopWidget() screen = qdw.screenGeometry(screen=1) self.fullscreen.setGeometry(screen) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) winMain = MainWindow(None) winMain.show() app.exec_()
  2. Designed the window(s) with QtDesigner, having a QWidget with a QGraphicsView, set a QGraphicsScene with my Image. Basically the same code as above but importing the ui-file with the uic module. An image of the result is appended. I am always getting a gray border that appears to be part of the QWidget.

Why is that? How to get rid of it?

带边框的全屏图像

To my knowledge, its the default style sheet of QWidgets adding that slight border to themselves; in this case QGraphicsView .

So, if set the style sheet on your main window through setStyleSheet , you can override the css to remove the border--

    styleSheetCss="QGraphicsView {border-width: 0px;}"
    self.setStyleSheet(styleSheetCss)

In your example-

class FullscreenWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent) 
        self.qg = QtGui.QGraphicsView(self)
        self.scene = QtGui.QGraphicsScene(self)
        self.qg.setScene(self.scene)

        # Make window fullscreen and always on top
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        # set the image (lena)
        qimg = q2n.array2qimage(np.pad(lena(), ((0,0),(0,0)), mode='constant'))        
        pix = QtGui.QPixmap(qimg)        
        self.scene.clear()
        self.scene.addPixmap(pix)  

        styleSheetCss="QGraphicsView {border-width: 0px;}"
        self.setStyleSheet(styleSheetCss)

        self.show()   

If there is another way to address this problem, I dunno, but this has worked for me.

Side note, you can also string css changes together-

styleSheetCss="""
QPushButton {color:#ffffff;background-color:#202020;padding:4px;border:1px solid #303030;}
QMenu {color:#ffffff;background-color:#505050;border:1px solid #282828;}
QMenu::item {color:#ffffff;background-color:#505050;padding:2px;}
QMenu::item:selected {color:#ffffff;background-color:#6c6c6c;padding:2px;}
QSlider {background-color:#323232;}
QScrollBar:vertical {width:10px;color:#ffffff;background-color:#808080;border:1px solid #202020;}"""
self.setStyleSheet(styleSheetCss)

Problem is a bug with QGraphicsView::fitInView() they don't care about: https://bugreports.qt.io/browse/QTBUG-11945

Solution:

self.setViewportMargins(-2, -2, -2, -2)
self.setFrameStyle(QFrame.NoFrame)

self is QGraphicsView in this case, so you might want to change this.

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