How can an image to be displayed on a qdialog?

5.7k views Asked by At

Pls help on this code, cuz i don't really get the concept. I start a dialog by clicking a button on a qwidget. I also would like to display an image on the aforementiond qdialog by clicking on a different button (img_btn) on the dialog . I have added some code below:

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

class BasicWidget(QWidget):
    def __init__(self, parent=None):
        super(BasicWidget, self).__init__()

        layout = QVBoxLayout()
        self.btn = QPushButton('Show Dialog')
        layout.addWidget(self.btn)
        self.setLayout(layout)
        self.show()

        self.btn.clicked.connect(self.showpic)

    def showpic(self):
        imgshow = PaintPicture()

class PaintPicture(QDialog):
    def __init__(self, parent=None):
        super(PaintPicture, self).__init__()

        layout = QVBoxLayout()
        self.img_btn = QPushButton('Enter')
        layout.addWidget(self.img_btn)

        filename = r'\\some\basic\picture.jpg'
        image = QImage(filename )

        self.imageLabel = QLabel()
        self.imageLabel.setPixmap(QPixmap.fromImage(image))

        layout.addWidget(self.imageLabel)

        self.setLayout(layout)
        self.show()

if __name__ =="__main__":
    app = QApplication(sys.argv)
    widget = BasicWidget()
    sys.exit(app.exec_())
1

There are 1 answers

0
Aleksandar On

first of all there are two mistakes in your code:

  1. imgshow = PaintPicture() should be self.imgshow = PaintPicture()

  2. path doesn't work for me like this filename = r'\\some\basic\picture.jpg' , but it works this way:filename = r'./some/basic/picture.jpg'

Now, if I understand, you want to open dialog with only "enter" button on it. When you press that button you want image to show on the same dialog, under the button? If so, here's the code:

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

class BasicWidget(QWidget):
    def __init__(self, parent=None):
        super(BasicWidget, self).__init__()

        layout = QVBoxLayout()
        self.btn = QPushButton('Show Dialog')
        layout.addWidget(self.btn)
        self.setLayout(layout)
        self.show()

        self.btn.clicked.connect(self.showpic)

    def showpic(self):
        self.imgshow = PaintPicture()

class PaintPicture(QDialog):
    def __init__(self, parent=None):
        super(PaintPicture, self).__init__()

        layout = QVBoxLayout()
        self.img_btn = QPushButton('Enter')
        self.img_btn.clicked.connect(self.showImage)
        layout.addWidget(self.img_btn)

        self.setLayout(layout)
        self.show()

    def showImage(self):
        filename = r'./some/basic/picture.jpg'
        image = QImage(filename )

        self.imageLabel = QLabel()
        self.imageLabel.setPixmap(QPixmap.fromImage(image))

        layout = self.layout()
        layout.addWidget(self.imageLabel)

if __name__ =="__main__":
    app = QApplication(sys.argv)
    widget = BasicWidget()
    sys.exit(app.exec_())