I’m trying to display a QLabel (without parent) at a fixed position using PyQt. The QLabel functions as a graphic that appears only for a moment, it’s appears in the middle of the screen by default but I can’t find how to move its position.
Any suggestion??
updated:
This is my code, maybe is a problem in the way I’m doing this:
from PyQt4.QtCore import Qt,QTimer
from PyQt4.QtGui import QLabel,QPixmap,QApplication
import os
import subprocess
class BrightnessControl(QLabel):
def __init__(self,parent=None):
self.__command__ = "nvclock -S%s"
self.__imgPath__ = "../../../../ui/alfredozn_vaio/brightness/img/"
self.__image__ = QPixmap()
super(BrightnessControl,self).__init__(parent,Qt.SplashScreen)
#self.loadImg(100)
def comman(self):
return self.__command__
def image(self):
return self.__image__
def imgPath(self):
return self.__imgPath__
def currentValue(self):
'''
Obtenemos el valor actual del smartdimmer
'''
res=subprocess.Popen(["smartdimmer","-g"],stdout=subprocess.PIPE)
return int(res.stdout.readline().split()[-1])
def loadImg(self,value):
'''
Aquí asumimos que el único medio de control será la aplicación, por lo que los valores se moverán en múltiplos de 10
'''
os.system(self.comman() % str(value))
self.image().load("%s%i.png" % (self.imgPath(),value))
self.setPixmap(self.image())
self.setMask(self.image().mask())
self.update()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
try:
if len(sys.argv) < 2 :
raise ValueError
else:
val=10
if sys.argv[1] == "down":
val*=-1
elif sys.argv[1] != "up":
raise ValueError
form = BrightnessControl()
val += form.currentValue()
val = 20 if val < 20 else 100 if val > 100 else val
form.loadImg(val)
#This move function is not working
form.move(100,100)
form.show()
QTimer.singleShot(3000,app.quit)
app.exec_()
except ValueError:
print "Modo de uso: SonyBrightnessControl [up|down]"
You can use pos property or you can move it calling move() into the label. But it doesn’t always work well. The best method from my experience is to use label.setGeometry() function.
If you want it in the middle of the screen the code should be like this (Is c++ code, I dont know python sorry):
If you do this into the resizeEvent of the parent you will have it always in the center of it.
You can also optimize the code saving the sizeHint into a variable to not call it several times.