I'm trying to setup a very basic GUI using PyQt5 and QtDesigner. I found a very good example with included pan and zoom function. However, I do not grasp how to get the QGraphicsView and QGraphicScene classes into the QGraphicView widget created by QtDesigner. Right now it is a separate window, not associated to any tab etc. I would much appreciate
if someone could give me some input on how to put the QGraphicsView class from the example in the existing QGraphicsView widget from QtDesigner. Thanks in advance!
From QtDesigner:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 621, 491))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.graphicsView = QtWidgets.QGraphicsView(self.tab)
self.graphicsView.setGeometry(QtCore.QRect(60, 30, 361, 371))
self.graphicsView.setObjectName("graphicsView")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
From Logic:
class Logic(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Logic, self).__init__(parent)
self.setupUi(self)
From Pan/Zoom example:
class ZoomAndPan(QW.QGraphicsView):
def __init__(self,parent=None):
super(ZoomAndPan,self).__init__(parent)
#QW.QGraphicsView.__init__(self, parent=parent)
self.setWindowTitle('ZoomAndPan')
self.setGeometry(600,300,600,400)
# Create scene
self.sc=scene(self,self)
self.setScene(self.sc)
def mouseMoveEvent(self,event):
'Pan by manipulting sceneRect'
pos=event.pos()
pos=self.mapToScene(pos)
dx=pos.x()-self.sc.startPos.x()
dy=pos.y()-self.sc.startPos.y()
rect=self.sceneRect().getRect()
self.setSceneRect(rect[0]-dx,rect[1]-dy,rect[2],rect[3])
# Increas counter to show that the loop works
self.count+=1
self.showMatrix()
class scene(QW.QGraphicsScene):
def __init__(self,parent,myView=[]):
QW.QGraphicsScene.__init__(self,parent)
self.myView=myView
# Some items in scene
self.txt=self.addSimpleText("///////")
self.txt.setPos(2,-20)
self.txt.setScale(2)
self.txt.setBrush(QtGui.QBrush(QtCore.Qt.green))
self.addRect(0,16,20,20, pen=QtGui.QPen(QtCore.Qt.blue))
self.addRect(10,60,32,8, pen=QtGui.QPen(QtCore.Qt.red))
self.addRect(30,16,20,20, pen=QtGui.QPen(QtCore.Qt.blue))
self.N=0
def mousePressEvent(self, event):
self.myView.setDragMode(1) # Works fine without this
self.startPos=event.scenePos()
def mouseReleaseEvent(self, event):
self.myView.setDragMode(0)
def wheelEvent(self, event):
'zoom'
sc=event.delta()/100
if sc<0: sc=-1/sc
self.myView.scale(sc,sc)
self.myView.setDragMode(0)
self.myView.showMatrix()
def main():
app = QtWidgets.QApplication(sys.argv)
ui = Logic(None)
view = ZoomAndPan(ui)
ui.showMaximized()
sys.exit(app.exec_())
main()
Related
I want to display an image using QGraphicsView in a window. I have a basic QGraphicsView and a button based test.ui file generated from QtDesigner tool. Corresponding test.py file:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I made use of QGraphicsScene to display an image inside this QGraphicsView. My current working solution is to mess up the above .py file as follows:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.scene = QtWidgets.QGraphicsScene(self.centralwidget) #added
self.graphicsView = QtWidgets.QGraphicsView(self.scene) #edited
self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.display)
def display(self):
import imageio, numpy
self.scene.clear()
input_image = imageio.imread('image.jpg').copy()
height, width, channels = input_image.shape
bytesPerLine = channels * width
qimg = QtGui.QImage(input_image, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
self.pixmap = QtGui.QPixmap.fromImage(qimg)
self.scene.addPixmap(self.pixmap) #add pixmap to scene
self.graphicsView.scale(0.5,0.5)
self.scene.update()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
However, this solution is not systematic. I want to do the same without editing the original test.ui generated test.py file.
So, I tried following code which to me appears like a systematic translation of the previous working code. However, it fails to produce any display on button click:
from PyQt5 import QtCore, QtGui, QtWidgets
from test import Ui_MainWindow
class myWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
self.scene = QtWidgets.QGraphicsScene(self.centralwidget) #append scene method
self.graphicsView = QtWidgets.QGraphicsView(self.scene) #graphicsView re-assigned with scene
self.pushButton.clicked.connect(self.display)
def display(self):
import imageio, numpy
self.scene.clear()
input_image = imageio.imread('image.jpg').copy()
height, width, channels = input_image.shape
bytesPerLine = channels * width
qimg = QtGui.QImage(input_image, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
self.pixmap = QtGui.QPixmap.fromImage(qimg)
self.scene.addPixmap(self.pixmap) #add pixmap to scene
self.graphicsView.scale(0.5,0.5)
self.scene.update()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = myWindow()
w.show()
sys.exit(app.exec_())
I am likely messing up something due to my limited understanding of class inheritance, QGraphicsView, and QGraphicsScene. Any pointers to my mistakes would be very helpful.
Note: I am using a myWindow class to integrate some key press functionality in future. I think the issue is more to do with how to selectively append/redefine few lines inside a parent class function. Is this even possible without re-writing the whole setupUi method? Or, is there another smarter way to perform the same task?
You have to set the scene to the existing QGraphicsView instead of creating a new QGraphicsView:
class myWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.scene = QtWidgets.QGraphicsScene(self.centralwidget)
self.graphicsView.setScene(self.scene)
self.pushButton.clicked.connect(self.display)
def display(self):
# ...
I'm newbie for using Pyqt5.
I use qtdesigner for building GUI.
I have MainWindow for passing value to dialogwindow
I want to LineEdit( in dialogwindow) show value after user input and click button (in MainWindow)
I try
self.ui = Ui_Dialog(self,data)
but it doesn't work
My code mainpage
MainWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
class Ui_MainWindow(object):
def openDialog(self):
data = self.lineEdit.text()
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog(self,data)
self.ui.setupUi(self.window)
# MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 236)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 40, 91, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 40, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 110, 93, 28))
self.pushButton.clicked.connect(self.openDialog)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 505, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Passing Value"))
self.pushButton.setText(_translate("MainWindow", "Send"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Dialog code.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(577, 253)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(260, 100, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(180, 100, 55, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Value"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
advice me plz
Thank you,
Mint
PyQt recommends not modifying the .py generated by pyuic and Qt Designer but creating another file that uses that class to fill a widget so I recommend regenerating the MainWindow.py and dialog.py files.
Now create a main.py where we inherit the appropriate class by setting a constructor with the requirement:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
from MainWindow import Ui_MainWindow
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, text, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText(text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
def openDialog(self):
data = self.lineEdit.text()
w = Dialog(data)
w.exec_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I'm pretty new to PYQT5 and I wanted to draw a circle on a Pixmap that is contained in a QLabel in the MainWindow UI on PYQT5 with this code:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = '001_01_01_041_05.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 60, 331, 401))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setPixmap(self._image)
def paintEvent(self, event):
painter = QtGui.QPainter(self._image)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
However, I cannot get the paintEvent(self, event) function to get called. When I did get to call paintEvent(self, event), I could not see the circle being drawn onto the pixmap. I tried setting the QPainter to the Pixmap itself (in painter = QtGui.QPainter(self._image)), but that did not work either.
What did I do incorrectly?
The code below works, which is what my code is based on:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Any thoughts or advice would be greatly appreciated!
You missed a couple of lines.
Try it:
from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = 'E:/_Qt/img/heart.png' # '001_01_01_041_05.png'
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
super().__init__() # <-------
self._image = QtGui.QPixmap(background_image_path) # <-------
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawEllipse(300, 300, 70, 70)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
The real problem here is that you are editing the module generated by pyuic,
which is always a very bad idea. Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
background_image_path = 'E:/_Qt/img/heart.png'
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 0, 760, 560))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self._image = QtGui.QPixmap(background_image_path)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
# self.label.setPixmap(self._image)
self.label.setText(_translate("MainWindow",
"""
It is not recommended to modify the design file,
it is appropriate to create another file
to join the logic with the design.
"""))
# def paintEvent(self, event):
# painter = QtGui.QPainter(self._image)
# painter.drawPixmap(self.rect(), self._image)
# pen = QtGui.QPen()
# pen.setWidth(5)
# painter.setPen(pen)
# painter.drawEllipse(300, 300, 70, 70)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.setupUi(self)
self._image = QtGui.QPixmap(background_image_path)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
#pen = QtGui.QPen()
#pen.setWidth(5)
#painter.setPen(pen)
painter.setPen(QtGui.QPen(QtCore.Qt.blue, 5))
painter.drawEllipse(350, 350, 70, 70)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
window = MyApp()
window.show()
sys.exit(app.exec_())
Got stuck with accessing to outer class functions, it'll be great to receive some advice or solution how can i get access to another class method in Python.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(480, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 520, 114, 32))
self.pushButton.setObjectName("pushButton")
#self.pushButton.clicked.connect(self.btnCheck)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(190, 520, 114, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 150, 411, 311))
font = QtGui.QFont()
font.setPointSize(66)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton1"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton2"))
self.label.setText(_translate("MainWindow", "TextLabel"))
class Ui_Support(object):
def __init__(self):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def btnCheck(self):
self.ui.label.setText(a)
def closeBtn(self):
QtCore.QCoreApplication.instance().quit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is my test form, i'd like to call button event from another class, it's easy to call it from inner class function, but it's better to contain multiple functions in another class, thank you for the reply!
The setupUi() function requires an object of type QMainWindow, in your case the optimum would be that Ui_Support should inherit from QMainWindow.
class Ui_Support(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self, parent=None)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.btnCheck)
def btnCheck(self):
self.ui.label.setText("a")
def closeBtn(self):
QtCore.QCoreApplication.instance().quit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_Support()
MainWindow.show()
sys.exit(app.exec_())
Well, this is a problem that has been bugging me for few days. I'm trying to open a Ui_form from a MainWindow. For some reason the child window opens and closes instantly.
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self,parent)
def setupUi(self, MainWindow):
# Some Gui Code
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(406, 234)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.listView = QtGui.QListView(self.centralwidget)
self.listView.setObjectName(_fromUtf8("listView"))
self.horizontalLayout.addWidget(self.listView)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.NewLog)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "New Log", None, QtGui.QApplication.UnicodeUTF8))
def NewLog(self):
app = QtGui.QWidget()
myapp = new_application_log.Ui_Form()
myapp.setupUi(app)
myapp.show()
#app.exec_()
class Ui_Form(QtGui.QWidget):
def __init__(self,parent = None):
QtGui.QWidget.__init__(self,parent)
def setupUi(self, Form):
# Some Gui Code.
Form.setObjectName(_fromUtf8("Form"))
Form.resize(312, 269)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.listView = QtGui.QListView(Form)
self.listView.setObjectName(_fromUtf8("listView"))
self.verticalLayout.addWidget(self.listView)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
The simplest solution is to change your newLog to this
def NewLog(self):
app = QtGui.QWidget()
self.form = Ui_Form()
self.form.setupUi(app)
self.form.show()
with the self.form defined in the Ui_mainwindow class
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.form = None
In your code, you are defining the myapp = Ui_form() variable inside of the newLog function and then it is going out of scope and being deleted right when the function exits. If you were to somehow include some blocking of the function ending you wouldn't technically need to put self.form in the __init__() of Ui_mainWindow. Say, for instance, you had a form that would return require a button hit to close, then you could just do it as you had it, but since you're just show()ing a widget, you need to have it survive outside of the function.
Edit: For instance, this code would run just fine for the above stated reason--you have to click the button before the function exits
def NewLog(self, text='I am open!', buttons=QtGui.QMessageBox.Ok):
errorBox = QtGui.QMessageBox()
errorBox.setText(text)
errorBox.setStandardButtons(buttons)
return errorBox.exec_()