tags:

views:

165

answers:

1

I want to make image in my QMainWindow so when you click on it you have a signal translating like qpushbutton I use this:

          self.quit=QtGui.QPushButton(self)
          self.quit.setIcon(QtGui.QIcon('images/9.bmp'))

But the problem is whene I resize the window qpushbutton resized too but not his icon have you solution of my problem?!! thank you

A: 

Qt won't stretch your image for you - and it's best this way. I recommend to keep the pushbutton of a constant size by adding stretchers to the layout holding it. A resizable pushbutton isn't very appealing visually, and is uncommon in GUIs, anyway.

To make a clickable image, here's the simplest code I can think of:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class ImageLabel(QLabel):
    def __init__(self, image, parent=None):
        super(ImageLabel, self).__init__(parent)
        self.setPixmap(image)

    def mousePressEvent(self, event):
        print 'I was pressed'    


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.create_main_frame()       

    def create_main_frame(self):
        name_label = QLabel("Here's a clickable image:")
        img_label = ImageLabel(QPixmap('image.png'))

        vbox = QVBoxLayout()
        vbox.addWidget(name_label)
        vbox.addWidget(img_label)

        main_frame = QWidget()
        main_frame.setLayout(vbox)
        self.setCentralWidget(main_frame)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

Just replace image.png with your image filename (acceptable format by QPixmap) and you're set.

Eli Bendersky
thank Mr eliben ; but it s not what i want to crate a clickable image with fonction connect() that you can send signal to one slot(this slot change the image) thx
brou1986