How to make a window that occupies the full screen without maximising? - python

I'm writing in python using Qt
I want to create the application window (with decorations) to occupy the full screen size. Currently this is the code I have:
avGeom = QtGui.QDesktopWidget().availableGeometry()
self.setGeometry(avGeom)
the problem is that it ignores window decorations so the frame is larger... I googled and what not, found this:
http://harmattan-dev.nokia.com/docs/library/html/qt4/application-windows.html#window-geometry
which seems to indicate I need to set the frameGeometry to the avGeom however I haven't found a way to do that. Also, in the comments in the above link it says what I'm after may not be even possible as the programme can't set the frameGeometry before running... If that is the case I just want confirmation that my problem is not solvable.
EDIT:
So I played around with the code a bit and this gives what I want... however the number 24 is basically through trial and error until the window title is visible.... I want some better way to do this... which is window manager independent..
avGeom = QtGui.QDesktopWidget().availableGeometry()
avGeom.setTop(24)
self.setGeometry(avGeom)
Now I can do what I want but purely out of trial and error
Running Ubuntu, using Spyder as an IDE
thanks

Use QtGui.QApplication().desktop().availableGeometry() for the size of the window:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.pushButtonClose = QtGui.QPushButton(self)
self.pushButtonClose.setText("Close")
self.pushButtonClose.clicked.connect(self.on_pushButtonClose_clicked)
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.pushButtonClose)
titleBarHeight = self.style().pixelMetric(
QtGui.QStyle.PM_TitleBarHeight,
QtGui.QStyleOptionTitleBar(),
self
)
geometry = app.desktop().availableGeometry()
geometry.setHeight(geometry.height() - (titleBarHeight*2))
self.setGeometry(geometry)
#QtCore.pyqtSlot()
def on_pushButtonClose_clicked(self):
QtGui.QApplication.instance().quit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

I've always found inheritting from the QMainWindow class to be particularly useful. Like this:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class Some_APP(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
### this line here is what you'd be looking for
self.setWindowState(Qt.WindowMaximized)
###
self.show()
def main():
app = QApplication(sys.argv)
some_app = Some_APP()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Related

PyQt4 Label not showing

I am very new to PyQt4 and this question is probably very simple but I have tried many different things and nothing works. I am trying to make a label in PyQt4.
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class Display(QtGui.QWidget):
def __init__(self):
super(Display, self).__init__()
self.time = Time() #Another class in the program
self.ShowFullScreen()
self.setStyleSheet("background-color: black;")
self.show()
self.MainDisplay()
def MainDisplay(self):
self.timedisplay = QtGui.QLabel(self)
self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
self.timedisplay.setText(time.GetTime())
self.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Display()
sys.exit(app.exec())
The label doesn't show up, and there is no error message. What am I doing wrong?
I use PySide and not Qt, but they are supposed to be 99.99% compatible. The main problem is your call to the show() function, which makes the window visible. You have two calls to show. The first time it is called, you have not yet called MainDisplay so the QLabel hasn't been created yet. The second time you call show the window is already visible, so nothing changes.
If you create the widgets first and call show once, at the end, it will work the way you want. With this code the label shows up.
There are other issues:
You will have to change the import statements back the way you had them.
I did not have your Time class, so I just wrote a simple piece of text into the label.
The function ShowFullScreen should be showFullScreen.
The function that runs the event loop in QtApp is named exec_ not exec.
import sys
from PySide import QtCore
from PySide import QtGui
class Display(QtGui.QWidget):
def __init__(self):
super(Display, self).__init__()
self.setStyleSheet("background-color: black;")
self.MainDisplay()
self.showFullScreen()
def MainDisplay(self):
self.timedisplay = QtGui.QLabel(self)
self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
self.timedisplay.setText("What time is it now?")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Display()
sys.exit(app.exec_())

QToolbar behaving weird with QAction and &(ampersand)

According to the docs the QAction uses a single & to mark a shortcut mnemonic but when I used it on the QToolbar it does not work. Then I tried && which worked and the mnemonic appeared with the shortcut working fine and underline appearing properly.
But according to the docs && is used to show single & in the label.
Failing code
from PySide.QtGui import *
from PySide.QtCore import *
import sys
#####Custom edited example
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.actionNotification = QAction(self)
self.actionNotification.setCheckable(True)
self.actionNotification.setChecked(False)
self.actionNotification.setEnabled(True)
self.actionNotification.setAutoRepeat(True)
self.actionNotification.setVisible(True)
self.actionNotification.setIconVisibleInMenu(False)
self.actionNotification.setObjectName("actionNotification")
self.toolBar = QToolBar(self)
self.toolBar.setLayoutDirection(Qt.RightToLeft)
self.toolBar.setStyleSheet("")
self.toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.toolBar.setObjectName("toolBar")
self.toolBar.addAction(self.actionNotification)
self.actionNotification.setText("&Notification") #the problem lies here
self.actionNotification.setToolTip(
QApplication.translate("MainWindow", "Click to see new notifications", None,
QApplication.UnicodeUTF8))
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Main()
form.show()
app.exec_()
Working code
from PySide.QtGui import *
from PySide.QtCore import *
import sys
#####Custom edited example
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.actionNotification = QAction(self)
self.actionNotification.setCheckable(True)
self.actionNotification.setChecked(False)
self.actionNotification.setEnabled(True)
self.actionNotification.setAutoRepeat(True)
self.actionNotification.setVisible(True)
self.actionNotification.setIconVisibleInMenu(False)
self.actionNotification.setObjectName("actionNotification")
self.toolBar = QToolBar(self)
self.toolBar.setLayoutDirection(Qt.RightToLeft)
self.toolBar.setStyleSheet("")
self.toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.toolBar.setObjectName("toolBar")
self.toolBar.addAction(self.actionNotification)
self.actionNotification.setText("&&Notification") #this works
self.actionNotification.setToolTip(
QApplication.translate("MainWindow", "Click to see new notifications", None,
QApplication.UnicodeUTF8))
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Main()
form.show()
app.exec_()
A quick question on IRC(people there were really helpful) confirmed me that it was a qt issue as this was the same issue in pyqt4 and that QAction works fine with QMenu and the problem exists only for QToolBar
I thought about asking this question here to have an extended discussion and if possible to learn why it behaves so.
tl;dr:what should be done about this weird behaviour of QToolBar? I would like to know why it behaves so.
Any help or suggestion would be really great
system: Debian,python2.7, PySide-1.1
To work around this bug, you can call QAction.setIconText("&Foo") and the mnemonic will be respected.

How to show the correct frame with python and pyQt

I'm a beginner in python , and I try to generate the frame with pyQt.
Here is my code , and I have some trouble that can not show a correct frame .
At first I wrote , and it can showed the result that I want.
#!/usr/bin/python
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('simple2')
widget.show()
sys.exit(app.exec_())
I changed the wording of object-oriented later , and it can't show a frame which I except.
#!/usr/bin/python
import sys
from PyQt4 import QtGui
class Apple(QtGui.QWidget):
def _int_(self,parent=None):
super().__init__()
self.widget = QtGui.QWidget()
self.resize(250, 150)
self.setWindowTitle('simple2')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.show()
sys.exit(app.exec_())
Someone knows how do I fix my error ?
You have made a typo in your code
def _int_(self,parent=None):
should be
def __init__(self, parent=None):
Edit your code this way:
#!/usr/bin/python
# -* coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Apple(QtGui.QMainWindow):
def _init_(self): # if you have no parent you don't need to write parent = None. And yes, there was a typo
super(Apple, self).__init__() # it's a little bit better than super().__init__()
#self.widget = QtGui.QWidget() # you've already init QWidget. This code do nothing
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.setWindowTitle('simple2') # here must be window title
mywidget.resize(250, 150) # and resizing
mywidget.show()
sys.exit(app.exec_())
Now you get what you want

Qt QGraphicsDropShadowEffect is not showing

I am creating a custom widget my_widget inheriting from QWidget.
Here, I have a label to which I would like to apply QGraphicsDropShadowEffect however it does not seem to be working since I don't see any shadows.
My code is in Python and it's:
eff = QGraphicsDropShadowEffect()
self.my_widget_label.setGraphicsEffect(eff)
I tried various alterations to this code to no avail.
After doing a through search on Google, I came across many similar questions without answers.
What might be the cause? How can I get the shadow?
Works for me in C++. I did the following in a QDialog containing a QLabel object named titleLabel. I'm using Qt 4.8.4 on a Windows XP computer.
QGraphicsDropShadowEffect* eff = new QGraphicsDropShadowEffect(this);
eff->setBlurRadius(5);
titleLabel->setGraphicsEffect(eff);
See if this works for you:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class testShadow(QWidget):
def __init__(self, parent=None):
super(testShadow, self).__init__(parent)
self.resize(94, 35)
self.verticalLayout = QVBoxLayout(self)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QLabel(self)
self.label.setText("Text Label")
self.shadow = QGraphicsDropShadowEffect(self)
self.shadow.setBlurRadius(5)
self.label.setGraphicsEffect(self.shadow)
self.verticalLayout.addWidget(self.label)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main = testShadow()
main.show()
sys.exit(app.exec_())
I have only every tried to use this (and used it successfully) in QGraphicsScene situations. This works for me, while trying to set it on a normal QWidget actually crashes the entire application:
from PyQt4 import QtGui
class Graphics(QtGui.QWidget):
def __init__(self):
super(Graphics, self).__init__()
layout = QtGui.QVBoxLayout(self)
layout.setMargin(0)
shad = QtGui.QGraphicsDropShadowEffect(self)
shad.setBlurRadius(5)
self.scene = QtGui.QGraphicsScene(self)
self.view = QtGui.QGraphicsView(self)
self.view.setScene(self.scene)
text = self.scene.addText("Drop Shadow!")
text.setGraphicsEffect(shad)
layout.addWidget(self.view)
if __name__ == "__main__":
app = QtGui.QApplication([])
main = Graphics()
main.show()
main.raise_()
app.exec_()

PyQt4: how to make undercorated window with reserved space

I'd like to make a panel-like application using PyQt4 for Linux. for this i need the window i created:
to be undecorated
to have reserved space
to appear on all workspaces
From reading the documentation i've got the idea that i should use QtWindowFlags. But i have no clue as to how to do that. Also i believe there should be a Qt.WindowType hint somewhere telling the WM the window's a "dock" application. I have made this with pygtk following this thread, but here with Qt i don't really know how to handle this. (I need Qt for its ability to theme/skin application more easily.)
Below is the current code i made (nothing extraordinary).
import sys
from PyQt4 import QtGui
class Panel(QtGui.QWidget):
def __init__(self, parent=None): ## should the QtWindowFlag be here?
QtGui.QWidget.__init__(self, parent) ## should the QtWindowFlag be there as well?
self.setWindowTitle('QtPanel')
self.resize(QtGui.QDesktopWidget().screenGeometry().width(), 25)
self.move(0,0)
def main():
app = QtGui.QApplication(sys.argv)
panel = Panel()
panel.show()
sys.exit(app.exec_())
return 0
if __name__ == '__main__':
main()
Can anyone help me with this? Thanks :)
Read about the QWidget.windowFlags property: http://doc.qt.nokia.com/4.7/qwidget.html#windowFlags-prop
Example:
>>> from PyQt4 import QtGui, QtCore
>>> app = QtGui.QApplication([])
>>> win = QtGui.QMainWindow()
>>> win.setWindowFlags(win.windowFlags() | QtCore.Qt.FramelessWindowHint)
>>> win.show()
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
qbtn = QtGui.QPushButton('Quit', self)
#qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.clicked.connect(self.test)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show()
def test(self):
print "test"
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The solution is to use Python-Xlib, and it has been described in an answer on a universal way to reserve screen space on X.

Categories