Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create simple messagebox to report error in input value
Forum Updated to NodeBB v4.3 + New Features

Create simple messagebox to report error in input value

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 663 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PyGrrl_FL
    wrote on 4 Jun 2019, 20:48 last edited by
    #1

    Hello,

    I am completely new to GUI development using Qt and have about 1 year of python programming. I am working on a project that involves connecting to Oracle database and creating a file for processing. If the result of the query returns a null dataset, I want to have a messagebox popup to inform the user that the information they entered is incorrect.

    Here is my code so far:

    *import sys
    import os
    import cx_Oracle
    import csv
    from datetime import datetime as dt
    from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton, QFormLayout, QLabel, QMessageBox
    from invalid_popup import Ui_invalidPopup

    class Form(QDialog):
    def init(self, parent=None):
    super(Form,self).init(parent)
    self.setWindowTitle("Window Title")

        # Create widgets
        self.myCourseInput = QLineEdit(self)
        self.QCourseLabel = QLabel("Enter Course ID: ")
        self.submitBtn = QPushButton("SUBMIT")
        self.showdialog = QMessageBox
    
        # Create layout and add widgets
        layout = QFormLayout()
        layout.addRow(self.QCourseLabel,self.myCourseInput)
        layout.addRow(self.submitBtn)
    
        # Set dialog layout
        self.setLayout(layout)
    
        # Connect submit button to action
        self.submitBtn.clicked.connect(self.Submit_btn)
    
    # Run code to generate files for grade dispute processing
    def Submit_btn(self):
        Text = self.myCourseInput.text().upper() # Set any alpha input to uppercase
    
        # Connect to Oracle database
        os.putenv('ORACLE_HOME', <path to oracle home>)
        os.putenv('LD_LIBRARY_PATH',r"C:\oracle\instantclient_18_3")
        cnxn = cx_Oracle.connect(database connection info)
        cursor = cnxn.cursor()
    
        self.SQL1 = "select cm.BATCH_UID as EXTERNAL_COURSE_KEY, cm.COURSE_ID, cm.COURSE_NAME, cm.AVAILABLE_IND, cm.ROW_STATUS, ds.BATCH_UID as DATA_SOURCE_KEY from course_main cm, DATA_SOURCE ds where cm.COURSE_ID = :cid and ds.PK1 = cm.DATA_SRC_PK1"
    
        cursor.execute(self.SQL1, {'cid': (Text)})
        courseResults = cursor.fetchall()
        if courseResults == []:
            self.showdialog.show()
        else:
            dskCourse = str(courseResults[0][5])
    
            filename = (r"C:\Downloads\\" + dskCourse)
            gdCourseFile = open(filename,"w");
            output=csv.writer(gdCourseFile, delimiter = "|", lineterminator = "\n")
            header = ('EXTERNAL_COURSE_KEY', 'COURSE_ID', 'COURSE_NAME', 'AVAILABLE_IND', 'ROW_STATUS', 'DATA_SOURCE_KEY', 'NEW_DATA_SOURCE_KEY')
            output.writerow(header)
            ctr = 0
            for row in courseResults:
                if row[4] == 2:
                    ctr += 1
                    rowStatus = 'ENABLED'
                    newRow = list(row)
                    newRow[3] = 'Y'
                    newRow[4] = (str(rowStatus)) #Update the row_status of the course to enable for the grade dispute
                    newRow.append('SYSTEM')
                    output.writerow(newRow)
    
                    #Create footer for feed file
                    currentDt = dt.now()
                    today = currentDt.strftime('%H:%M:%S %m/%d/%Y')
                    footer = ('***FileFooter', str(ctr), today)
                    output.writerow(footer)
                    return(self.myCourseInput.text())
                else:
                    return(self.myCourseInput.text())
    
            gdCourseFile.close()
    
        cnxn.commit()
        cursor.close()
        cnxn.close()
        print("Connection closed.")
    
    def showdialog(QDialog):
       msg = QMessageBox()
       msg.setIcon(QMessageBox.Information)
       msg.setText("There are no results for the value entered.")
       msg.setWindowTitle("Invalid Entry")
       msg.setStandardButtons(QMessageBox.Ok)
    

    Create the Qt Application

    app = QApplication(sys.argv)

    Create and show the form

    form = Form()
    form.show()

    Run the main Qt loop

    sys.exit(app.exec())*

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Jun 2019, 21:01 last edited by
      #2

      Hi and welcome to devnet,

      You should use the equivalent of the QMessageBox::information static method.

      if not courseResults:
         QMessageBox.information(self, 
                                 self.tr("Invalid Entry")
                                 self.tr("There are no results for the value entered."))
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 4 Jun 2019, 21:11
      3
      • S SGaist
        4 Jun 2019, 21:01

        Hi and welcome to devnet,

        You should use the equivalent of the QMessageBox::information static method.

        if not courseResults:
           QMessageBox.information(self, 
                                   self.tr("Invalid Entry")
                                   self.tr("There are no results for the value entered."))
        
        P Offline
        P Offline
        PyGrrl_FL
        wrote on 4 Jun 2019, 21:11 last edited by
        #3

        @SGaist Thank you! Thank you! It does exactly what I need! Can you explain what the "self.tr" means? (Noob question, I'm sure. :))

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 4 Jun 2019, 21:18 last edited by
          #4

          It's the method used for application internationalisation/translation. It's not mandatory but I have the habit of making all strings shown to the users ready for translation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • P Offline
            P Offline
            PyGrrl_FL
            wrote on 5 Jun 2019, 01:06 last edited by
            #5

            Thanks for the explanation!

            1 Reply Last reply
            0

            1/5

            4 Jun 2019, 20:48

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved