tags:

views:

179

answers:

2

Hi

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]"
A: 

Use its pos property.

Jurily
I already tried with the move() function but it doesn't work for me. Also I tried to set directly the QPoint for the pos property like pos=QPoint(100,100)It compile and run but I don't know if is correct because pos doesn't have a setter...
alfredozn
A: 

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):

label.setGeometry((this->width() - label.sizeHint().width()) / 2), (this->height() - label.sizeHint().height()) / 2, label.sizeHint().width(), label.sizeHint().height());

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.

cnebrera
It doesn't work either, I set the geometry: form.setGeometry(10,10,200,50) just before the form.show() but it doesn't work, the label appears again in the middle of the screen. Maybe the problem is that the top level widget is the label, it doesn't has a parent. But I don't know how to solve it.
alfredozn
I see. The problem is that. You have no top level widget, that means Qt automatically creates a Window to be the top level one of your form. A solution is to use a transparent QDialog as top level window. Once you show the dialog you can move it to your desired position on the screen. You can even know the screen resulution using QDesktopServices. Hope this helps your problem.
cnebrera
Thank you, I followed your suggestion and it worked when I was creating the Qdialog in the __main__ and adding to it my custom QLabel. Then I tried to replace the type of my custom widget for a Qdialog and the problem occurs again =S.So looking in this behavior I found that the problem was at this line: super(BrightnessControl,self).__init__(parent,Qt.SplashScreen) because of parent was none.... so I replaced and now I can move my widget even if it is only the Qlabel.
alfredozn
I very glad it finally works! :) I have to learn a little bit of python, it is easy to use Qt with it? Do you know any good online manual?
cnebrera
I found something new, the problem wasn't the None parent, the real problem was the window flag Qt.SplashScreen =S, I used Qt.Popup instead.I'm new to Qt and python too, I'm a Java programmer and the python's learning curve has been very easy. I used this book to start learning python and Qt: http://acmsel.safaribooksonline.com/9780132354189 but the python tutorial (http://docs.python.org/tutorial/index.html) is very useful and clear. It could be the best point to start
alfredozn
Thank you very much for the info! So you have it finally working all right then?
cnebrera
Yes, it works now =)
alfredozn