Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT designer pushbutton



  • Hi everyone,
    I am creating a desktop application using qt designer and python. I have created a UI file and converted it to a py file. After that, I have created another logic python file. What it has to do is, I have to browse a pst file and I have to give some filters like from, to. The browse button is working. Whenever I type something in from TextEdit and press load, I have to fetch that data and store it in a variable. I have attached my designer file. I am quite confused about the pushbutton to connect with TextEdit.

        self.retranslateUi(MainWindow)
        self.browse.clicked.connect(self.browseSlot)
        self.pstfile.returnPressed.connect(self.returnPressedSlot)
        self.run.clicked.connect(self.runSlot)
        self.frombutton.clicked.connect(self.fromkeyword.copy)
        self.tobutton.clicked.connect(self.tokeyword.copy)
        self.subjectbutton.clicked.connect(self.subjectkeyword.copy)
        self.domainbutton.clicked.connect(self.domainkeyword.copy)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    

    11b6dbe5-8210-4f90-8ce6-193db448915f-image.png

    09b1bf5d-6c35-4843-9128-9139cf5e6910-image.png



  • @praneshpk
    I don't know whether this will help/suit you, but....

    A lot of us who use Designer do not do the signals/slots in it. I believe what the Designer allows you to do with them is quite limited.

    If we were writing this in code (maybe it can be adapted if you still want to do it it Designer), all we would need is something like:

    class MainWindow(QMainWindow):
        def __init__():
            super().__init__()
            self.fromLoadButton.clicked(self.onFromLoadClicked)
    
        # this is a slot, might want a Python `@decorator` for PyQt or PySide?
        def onFromLoadClicked():
            from = self.fromTextEdit.text()
            print(from)
            logicPythonModule.load(from)
    

    You would have to adapt, but is that all you are looking to understand from your question?



  • Hi,
    Thank you for your reply. Whether I can directly add this in the code or I have to change the slot and signal in a designer. I'm very new to this. For your reference, Ill attach the codes.

    All I need is whenever something is typed in the text edit box and the load button is pressed I need to get that text in a variable. I need to use that value. For example, I can get the filename using browse files as fileName. Similarly, I need values for other text edit boxes.

    1.) Whether I need to give slot and signal to that button and TextEdit, if yes how can I configure. you can see this In my screenshot.

    Here I have three python files.

    Myapp.py

        from PyQt5 import QtCore, QtGui, QtWidgets
    
        from PyQt5.QtCore import QObject, pyqtSlot
    
        from mainwindow import Ui_MainWindow
    
        import sys
    
        from model import Model   
    
        class MainWindowUIClass( Ui_MainWindow ):
    
            def __init__( self ):
    
                super().__init__()
    
                self.model = Model()
    
            def refreshAll( self ):
    
                self.pstfile.setText( self.model.getFileName() )
    
            def returnPressedSlot( self ):
    
                fileName =  self.lineEdit.text()
    
                if self.model.isValid( fileName ):
    
                    self.model.setFileName( self.lineEdit.text() )
    
                    self.refreshAll()
    
                else:
    
                    m = QtWidgets.QMessageBox()
    
                    m.setText("Invalid file name!\n" + fileName )
    
                    m.setIcon(QtWidgets.QMessageBox.Warning)
    
                    m.setStandardButtons(QtWidgets.QMessageBox.Ok
    
                                        | QtWidgets.QMessageBox.Cancel)
    
                    m.setDefaultButton(QtWidgets.QMessageBox.Cancel)
    
                    ret = m.exec_()
    
                    self.lineEdit.setText( "" )
    
                    self.refreshAll()
    
                    self.debugPrint( "Invalid file specified: " + fileName  )
    
    
            def browseSlot( self ):
    
                options = QtWidgets.QFileDialog.Options()
    
                options |= QtWidgets.QFileDialog.DontUseNativeDialog
    
                fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
    
                                None,
    
                                "QFileDialog.getOpenFileName()",
    
                                "",
    
                                "All Files (*);;Python Files (*.py)",
    
                                options=options)
    
                if fileName:
    
                    self.model.setFileName( fileName )
    
                    self.refreshAll()
    
                print(fileName)
    
    
        def main():
    
            app = QtWidgets.QApplication(sys.argv)
    
            MainWindow = QtWidgets.QMainWindow()
    
            ui = MainWindowUIClass()
    
            ui.setupUi(MainWindow)
    
            MainWindow.show()
    
            sys.exit(app.exec_())
    
        main()
    

    mainwindow.py

    # -*- coding: utf-8 -*-
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QObject, pyqtSlot
    
    class Ui_MainWindow(QObject):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(805, 723)
            MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.Title = QtWidgets.QLabel(self.centralwidget)
            self.Title.setGeometry(QtCore.QRect(20, 10, 381, 71))
            font = QtGui.QFont()
            font.setFamily("Arial Black")
            font.setPointSize(20)
            font.setBold(True)
            font.setWeight(75)
            self.Title.setFont(font)
            self.Title.setObjectName("Title")
            self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget.setGeometry(QtCore.QRect(200, 120, 431, 71))
            self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
            self.pstfile = QtWidgets.QLineEdit(self.verticalLayoutWidget)
            self.pstfile.setObjectName("pstfile")
            self.verticalLayout.addWidget(self.pstfile)
            self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(640, 120, 141, 71))
            self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
            self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.browse = QtWidgets.QPushButton(self.verticalLayoutWidget_2)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.browse.setFont(font)
            self.browse.setObjectName("browse")
            self.verticalLayout_2.addWidget(self.browse)
            self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(20, 120, 171, 71))
            self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
            self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
            self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_3.setObjectName("verticalLayout_3")
            self.label = QtWidgets.QLabel(self.verticalLayoutWidget_3)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(9)
            font.setBold(True)
            font.setWeight(75)
            self.label.setFont(font)
            self.label.setAlignment(QtCore.Qt.AlignCenter)
            self.label.setObjectName("label")
            self.verticalLayout_3.addWidget(self.label)
            self.verticalLayoutWidget_4 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_4.setGeometry(QtCore.QRect(200, 240, 431, 171))
            self.verticalLayoutWidget_4.setObjectName("verticalLayoutWidget_4")
            self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_4)
            self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_4.setObjectName("verticalLayout_4")
            self.fromkeyword = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.fromkeyword.setObjectName("fromkeyword")
            self.verticalLayout_4.addWidget(self.fromkeyword)
            self.tokeyword = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.tokeyword.setObjectName("tokeyword")
            self.verticalLayout_4.addWidget(self.tokeyword)
            self.subjectkeyword = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.subjectkeyword.setObjectName("subjectkeyword")
            self.verticalLayout_4.addWidget(self.subjectkeyword)
            self.domainkeyword = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.domainkeyword.setObjectName("domainkeyword")
            self.verticalLayout_4.addWidget(self.domainkeyword)
            self.run = QtWidgets.QPushButton(self.centralwidget)
            self.run.setGeometry(QtCore.QRect(340, 500, 131, 51))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(14)
            font.setBold(True)
            font.setWeight(75)
            self.run.setFont(font)
            self.run.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.run.setObjectName("run")
            self.verticalLayoutWidget_5 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_5.setGeometry(QtCore.QRect(20, 240, 171, 171))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.verticalLayoutWidget_5.setFont(font)
            self.verticalLayoutWidget_5.setObjectName("verticalLayoutWidget_5")
            self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_5)
            self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_5.setObjectName("verticalLayout_5")
            self.fromm = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.fromm.setFont(font)
            self.fromm.setAlignment(QtCore.Qt.AlignCenter)
            self.fromm.setObjectName("from")
            self.verticalLayout_5.addWidget(self.fromm)
            self.to = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.to.setFont(font)
            self.to.setAlignment(QtCore.Qt.AlignCenter)
            self.to.setObjectName("to")
            self.verticalLayout_5.addWidget(self.to)
            self.subject = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.subject.setFont(font)
            self.subject.setAlignment(QtCore.Qt.AlignCenter)
            self.subject.setObjectName("subject")
            self.verticalLayout_5.addWidget(self.subject)
            self.domain = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.domain.setFont(font)
            self.domain.setAlignment(QtCore.Qt.AlignCenter)
            self.domain.setObjectName("domain")
            self.verticalLayout_5.addWidget(self.domain)
            self.verticalLayoutWidget_6 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_6.setGeometry(QtCore.QRect(640, 230, 141, 191))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.verticalLayoutWidget_6.setFont(font)
            self.verticalLayoutWidget_6.setObjectName("verticalLayoutWidget_6")
            self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_6)
            self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_6.setObjectName("verticalLayout_6")
            self.frombutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.frombutton.setFont(font)
            self.frombutton.setObjectName("frombutton")
            self.verticalLayout_6.addWidget(self.frombutton)
            self.tobutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.tobutton.setFont(font)
            self.tobutton.setObjectName("tobutton")
            self.verticalLayout_6.addWidget(self.tobutton)
            self.subjectbutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.subjectbutton.setFont(font)
            self.subjectbutton.setObjectName("subjectbutton")
            self.verticalLayout_6.addWidget(self.subjectbutton)
            self.domainbutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.domainbutton.setFont(font)
            self.domainbutton.setObjectName("domainbutton")
            self.verticalLayout_6.addWidget(self.domainbutton)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 805, 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)
            self.browse.clicked.connect(self.browseSlot)
            self.pstfile.returnPressed.connect(self.returnPressedSlot)
            self.run.clicked.connect(self.runSlot)
            self.frombutton.clicked.connect(self.fromkeyword.copy)
            self.tobutton.clicked.connect(self.tokeyword.copy)
            self.subjectbutton.clicked.connect(self.subjectkeyword.copy)
            self.domainbutton.clicked.connect(self.domainkeyword.copy)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.Title.setText(_translate("MainWindow", "PST Mail Analyzer"))
            self.browse.setText(_translate("MainWindow", "Browse PST"))
            self.label.setText(_translate("MainWindow", "Select the PST file/folder"))
            self.run.setText(_translate("MainWindow", "RUN"))
            self.fromm.setText(_translate("MainWindow", "From"))
            self.to.setText(_translate("MainWindow", "To"))
            self.subject.setText(_translate("MainWindow", "Subject / Body"))
            self.domain.setText(_translate("MainWindow", "Domain"))
            self.frombutton.setText(_translate("MainWindow", "Load"))
            self.tobutton.setText(_translate("MainWindow", "Load"))
            self.subjectbutton.setText(_translate("MainWindow", "Load"))
            self.domainbutton.setText(_translate("MainWindow", "Load"))
    
    
        @pyqtSlot( )
        def browseSlot( self ):
            pass
    
        @pyqtSlot( )
        def returnPressedSlot( self ):
            pass
    
        @pyqtSlot( )
        def runSlot( self ):
            pass
    
        @pyqtSlot( )
        def fromkeyword( self ):
            pass
    
        @pyqtSlot( )
        def tokeyword( self ):
            pass
    
        @pyqtSlot( )
        def subjectkeyword( self ):
            pass
    
        @pyqtSlot( )
        def domainkeyword( self ):
            pass
    
    
    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_())


  • @praneshpk

    self.frombutton.clicked.connect(self.fromkeyword.copy)
    

    I don't know what self.fromkeyword.copy does/is supposed to do.

    You seem to have a widget named fromkeyword and a slot named fromkeyword? In the following I assume self.fromkeyword is the QTextEdit against the From label.

    self.frombutton.clicked.connect(self.fromkeywordClicked)
    
        @pyqtSlot( )
        def fromkeywordClicked( self ):
            print(self.fromkeyword.text())
    


  • As per your code, I have changed self.frombutton.clicked.connect(self.fromkeywordClicked) in code

    d0dcf270-323b-4bca-8003-0f6bd1b0b512-image.png

    And in Myapp file, I have added this

    b48a7b9f-5e89-47c1-afc8-c933b2361709-image.png

    When I type something in the textedit and press the button, app closes.



  • @praneshpk
    For all I know you need to mark that method with an @pyqtSlot( ). Or not.

    Use a debugger. Place a breakpoint on the first statement in fromkeywordClicked(). Or a print() as the first statement. Is it hit?

    Do the same against other of your slots. Do they work?

    Make sure you don''t have some "artefact" still lying around perhaps pointing to a non-existent slot or something, however that gets connected up from the Designer.

    Run your code in whatever Python debugger you use. Do you get trace back information when it "crashes"?



  • Thankyou I have changed that from UI and it is working.


Log in to reply