0

I understand (more or less) content of the page of official documentation except the the final example, which is essencially as follows (I do not care the button):

from ui_imagedialog import ImageDialog

class MyImageDialog(ImageDialog):
    def __init__(self):
      super(MyImageDialog, self).__init__()

    # Connect up the buttons.
      self.okButton.clicked.connect(self.accept)

Problem: I'm stuck a bit trying to understand how to make this snippet work. It rises an error: 'cannon import name ImageDialog'.

What should I add from the first example of the aforementioned documentation page to make this code to show up a Dialog window?

What I've tried: I've made the file with generated Python code with the name ui_imagedialog.py as requiered. It has the the following content, which obviously works on its own:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_ImageDialog(object):
    def setupUi(self, ImageDialog):
        ImageDialog.setObjectName("ImageDialog")
        ImageDialog.resize(303, 204)
        self.pushButton = QtWidgets.QPushButton(ImageDialog)
        self.pushButton.setGeometry(QtCore.QRect(200, 160, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(ImageDialog)
        QtCore.QMetaObject.connectSlotsByName(ImageDialog)

    def retranslateUi(self, ImageDialog):
        _translate = QtCore.QCoreApplication.translate
        ImageDialog.setWindowTitle(_translate("ImageDialog", "Dialog"))
        self.pushButton.setText(_translate("ImageDialog", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ImageDialog = QtWidgets.QDialog()
    ui = Ui_ImageDialog()
    ui.setupUi(ImageDialog)
    ImageDialog.show()
    sys.exit(app.exec_())

Any constructive help is appreciated.

garej
  • 508
  • 1
  • 8
  • 24

1 Answers1

1

Qt Designer is used to create the graphic part, but not for the logic, you must create the logical part depending on the widget you used in the design. In your case by the name I think it is QDialog.

from ui_imagedialog import Ui_ImageDialog

from PyQt5 import QtCore, QtGui, QtWidgets

class ImageDialog(QtWidgets.QDialog, Ui_ImageDialog):
    def __init__(self, parent=None):
      super(ImageDialog, self).__init__(parent=parent)
      self.setupUi(self)

      self.pushButton.clicked.connect(self.accept)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = ImageDialog()
    w.show()
    sys.exit(app.exec_())

Observations: In the ui_imagedialog.py file there is no ImageDialog class, only the Ui_ImageDialog class, so I generated the error. Also in the design the button is called self.pushButton, so you can not call it self.okButton.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • PyQt3 is an old version of PyQt, I have not had the opportunity to work with that. But the example is simple to reproduce. If my answer helps you mark it as correct please. – eyllanesc Apr 07 '17 at 14:13
  • It is not part of pyqt, that is generated by Qt Designer, and analyzing we can realize that design the graphic part. – eyllanesc Apr 20 '17 at 16:54