The following code I think approaches what is requested:
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class ExtendedQLabel(QLabel):
def __init(self, parent):
QLabel.__init__(self, parent)
def mouseReleaseEvent(self, ev):
self.emit(SIGNAL('clicked()'))
class Window(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.label1 = ExtendedQLabel(self.tr(u"Clique aqui"))
self.label2 = ExtendedQLabel(self.tr(u"Olá!"))
self.label2.setVisible(False)
self.connect(self.label1, SIGNAL('clicked()'), self.toggleLabel2)
layout = QHBoxLayout(self)
layout.addWidget(self.label1)
layout.addWidget(self.label2)
def toggleLabel2(self):
self.label2.setVisible(not self.label2.isVisible())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
The class ExtendedQLabel
inherits the class QLabel
, including the event clicked()
, that actually sends out a signal when the event mouseReleaseEvent()
is called. The event clicked()
of label1
is then connected to a function that switches the visibility of the label2
.
I saw that approach here: An example on how to make Qlabel clickable. It seems that there is a new and more appropriate way to use signs and slots, I didn’t use it because it’s new to me.
I won’t answer because I don’t use Qt in Python. But I do. If you choose to use Qlabel yourself, you can create a class of your own by inheriting Qlabel and reimplementing the method
mousePressEvent
to do something by pressing the mouse button. Or, if you prefer to use Qpushbutton to display image (because it also works), just add the image as an icon (usingsetIcon
) or useimage
in style (with something likesetStyleSheet("image: url(imagem.jpg)")
, for example).– Luiz Vieira
Other alternatives (although the codes are in C++, should serve as a basis for vc) here: http://stackoverflow.com/questions/2671842/setting-background-image-for-qpushbutton
– Luiz Vieira
using this stylesheet does nothing, gives no error, or loads anything.. Searching the internet I found a method through the icon, but the image is too small, I want the image to occupy the entire space of the button, which has the size of 100x100
– Mega Anim
icon_data = urllib.urlopen(url). read()
icon_pixmap = QPixmap()
icon_pixmap.loadFromData(icon_data)
icon = QIcon(icon_pixmap)
self.btnBotao.setIcon(icon)
– Mega Anim
I managed to solve, it was only to increase the size of the icon in the properties of the xD button
– Mega Anim
I’m glad you could handle it. In this case, how about you offer an answer explaining in detail (add the relevant code snippets, for example) how you solved it and mark it as accepted? So you potentially help other people in the future. If you do, you get my +1.
– Luiz Vieira
The code I posted in the previous message. The size of the icon I changed in the same qtdesign, just went in icon and set the width and height sizes is basic thing
– Mega Anim
Previous message? There is no previous "message". And I understood what you did. I’m just suggesting that you create an answer yourself to demonstrate how you did it. Remember, this site is not a forum. If you haven’t done it yet, please do the [tour].
– Luiz Vieira
icon_data = urllib.urlopen(url). read() icon_pixmap = QPixmap() icon_pixmap.loadFromData(icon_data) icon = QIcon(icon_pixmap) self.btnBotao.setIcon(icon) – Mega Anim 5/06 às 6:45 << essa foi a mensagem com o codigo que resolveu
– Mega Anim
Yes, I understand. But this is not exactly a "message", but a comment. It gets lost easily amid so many other comments, and makes it difficult for someone who might need it in the future (because they have the same question/problem as you). Therefore, the ideal is yourself answer back your question, putting this code well-formatted in the body of the answer.
– Luiz Vieira
There is a "Your answer" field below. Put your code there, and I’ll help you format it. I could answer in your place, but then you lose the opportunity to earn a reputation for it. : ) Please read this text: http://answall.com/help/self-answer
– Luiz Vieira
So, but I can’t answer my own question, the site won’t let me, once I answered my question, and they warned me not to do it, and they said I should only include things in the original question.
– Mega Anim
Look, as far as I’m concerned, there’s no limit to reputation to answering your own question. Are you sure you’re trying to do this in the right place (there’s a field for it down on this page, as I’ve said before)? Also, either you got confused with what you were told (it wouldn’t be to not answer on the body of the question? ) or who told you this did not know what he was talking about.
– Luiz Vieira
The only field you have here is the blue button: "Answer your question" the time I tried came the message that could not answer
– Mega Anim