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_()
Related
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()
I have tried
self.mdiArea.activeSubWindow().objectName
self.mdiArea.activeSubWindow().objectName()
But neither of them gave me the object name I set:
self.subwindow.setObjectName("subwindow_1")
Any advice? Thanks a lot. Full testing code is attached:
# Created by: PyQt5 UI code generator 5.6
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(687, 438)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setGeometry(QtCore.QRect(140, 10, 531, 381))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow_test_the_object_name")
self.Add = QtWidgets.QPushButton(self.centralwidget)
self.Add.setGeometry(QtCore.QRect(10, 30, 121, 23))
self.Add.setObjectName("Add")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 687, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Add.clicked.connect(self.Add_window) # connect the button to Add_window function
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def Add_window(self):
self.mdiArea.addSubWindow(self.subwindow)
print("there is a subwindow activated")
self.mdiArea.activeSubWindow()
self.subwindow.show()
Act_window=self.mdiArea.activeSubWindow()
print("this is subwindow 1 "+str(Act_window.objectName()))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.subwindow.setWindowTitle(_translate("MainWindow", "Subwindow"))
self.Add.setText(_translate("MainWindow", "Add Window"))
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 a very simple sample with objectName() does not work with subwindow but it works with button and the parent MDIarea, my goal is to get the active subwindow object name such as subwindow_test_the_object_name in this case, if I have multiple subwindows with different object name, I can grab this object name as a signal. Thanks.
With the following instruction:
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow_test_the_object_name")
you are giving a name to the widget that is going to be inside the QMdiSubWindow, not the QMdiSubWindow, so if you want to retrieve the name of that element you must use the widget() method of QMdiSubWindow:
def Add_window(self):
self.mdiArea.addSubWindow(self.subwindow)
print("there is a subwindow activated")
self.mdiArea.activeSubWindow()
self.subwindow.show()
Act_window =self.mdiArea.activeSubWindow()
print("this is subwindow 1 "+str(Act_window.widget().objectName()))
Output:
there is a subwindow activated
this is subwindow 1 subwindow_test_the_object_name
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.setText(_translate("MainWindow", "확인"))
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_())
when button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I'm trying to make a lineEdit widget have place holder text (look like 'MM/DD/YYYY') when not in focus and have input masking (looks like ' / / ', or you can put in slashes through other means) when in focus.
Currently the input mask setInputMask('99/99/9999') takes precedence and I can't figure out a way to put a hasFocus (or similiar) signal on a LineEdit widget.
How would you implement both of these functionalities in one widget?
Would it be easier to leave the place holder text as is and create a method that adds in a slash after entering a certain number of characters?
Also, the flicking cursor is really thick when you set a mask. What's up with that and how do you revert it?
Full code:
from PySide import QtCore, QtGui
class Ui_MainWindow(object):
def myChanges(self):
self.lineEdit.installEventFilter(self.lineEdit.setInputMask('99/99/9999'))
self.lineEdit.setPlaceholderText('MM/DD/YYYY')
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(557, 351)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(320, 130, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 557, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.myChanges()
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
If you want a custom behavior of a widget you must create a class that inherits from this, in your case of QLineEdit. To get your specific objective you must use the focusInEvent and focusOutEvent methods as described in the following section:
class LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
QtGui.QLineEdit.__init__(self, parent=parent)
self.setPlaceholderText('MM/DD/YYYY')
def focusInEvent(self, event):
self.setInputMask('99/99/9999')
def focusOutEvent(self, event):
self.setInputMask('')
And then you should change to:
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
to:
self.lineEdit = LineEdit(self.centralwidget)
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_())