Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Variable problem in EventFilter

Variable problem in EventFilter

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for pythonpython
4 Posts 3 Posters 761 Views
  • 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.
  • Y Offline
    Y Offline
    YassineKira
    wrote on last edited by YassineKira
    #1

    Hello

    def eventFilter(self, obj, event):
        
    
        if obj == self.ui.tableWidget_2.viewport() and event.type() == QEvent.MouseButtonPress:
    
            if event.button() == Qt.RightButton:
                idx = self.ui.tableWidget_2.indexAt(event.pos())
                
                if idx.isValid():
                    ajouterAction = QAction("Ajouter", self)
                    ajouterAction.setObjectName(str(idx.row()))
                    ajouterAction.triggered.connect(self.ajouter_action_triggered)
    
                    contextMenu = QMenu(self)
                    contextMenu.addAction(ajouterAction)
                    
                    contextMenu.exec(event.globalPos())
        
        return QMainWindow.eventFilter(self, obj, event)
    

    the fonction :

    def ajouter_action_triggered(self):  
        import os.path
    
        BASE_DIR = os.path.dirname(os.path.abspath(__file__))
        db_path = os.path.join(BASE_DIR, "database_prix.db")
        with sqlite3.connect(db_path) as db:
            cursor=db.cursor()
            row = int(QObject.sender(self).objectName())
            id = self.ui.tableWidget_2.item(row, 0).text()
            print(id)
            y=(str(id))
            command = ''' SELECT PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu = ? '''
            result= cursor.execute(command,y)                    
            x = result.fetchone()
            print(x)
            self.ui.tableWidget_3.setItem(0,0,QtWidgets.QTableWidgetItem(str(x[0])))
            self.ui.tableWidget_3.setItem(0,1,QtWidgets.QTableWidgetItem(str(x[1])))
            self.ui.tableWidget_3.setItem(0,2,QtWidgets.QTableWidgetItem(str(x[2])))
            self.ui.tableWidget_3.setItem(0,3,QtWidgets.QTableWidgetItem(str(x[3])))
    

    To explain my problem to you, I want to add a row that belongs to one qtablewidget in another, I added the EventFilter function with event "AjouterAction" but each time I select another row in the first table they add value in the "y" variable and show me the error message:

    sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
    

    the query is good, everything is good except I don't understand why it stores in the variable "y" the old value and he adds to became 3 in this time.

    If you dont understand my problem i can explain with an exemple. Thanks :)

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Y YassineKira

      Hello

      def eventFilter(self, obj, event):
          
      
          if obj == self.ui.tableWidget_2.viewport() and event.type() == QEvent.MouseButtonPress:
      
              if event.button() == Qt.RightButton:
                  idx = self.ui.tableWidget_2.indexAt(event.pos())
                  
                  if idx.isValid():
                      ajouterAction = QAction("Ajouter", self)
                      ajouterAction.setObjectName(str(idx.row()))
                      ajouterAction.triggered.connect(self.ajouter_action_triggered)
      
                      contextMenu = QMenu(self)
                      contextMenu.addAction(ajouterAction)
                      
                      contextMenu.exec(event.globalPos())
          
          return QMainWindow.eventFilter(self, obj, event)
      

      the fonction :

      def ajouter_action_triggered(self):  
          import os.path
      
          BASE_DIR = os.path.dirname(os.path.abspath(__file__))
          db_path = os.path.join(BASE_DIR, "database_prix.db")
          with sqlite3.connect(db_path) as db:
              cursor=db.cursor()
              row = int(QObject.sender(self).objectName())
              id = self.ui.tableWidget_2.item(row, 0).text()
              print(id)
              y=(str(id))
              command = ''' SELECT PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu = ? '''
              result= cursor.execute(command,y)                    
              x = result.fetchone()
              print(x)
              self.ui.tableWidget_3.setItem(0,0,QtWidgets.QTableWidgetItem(str(x[0])))
              self.ui.tableWidget_3.setItem(0,1,QtWidgets.QTableWidgetItem(str(x[1])))
              self.ui.tableWidget_3.setItem(0,2,QtWidgets.QTableWidgetItem(str(x[2])))
              self.ui.tableWidget_3.setItem(0,3,QtWidgets.QTableWidgetItem(str(x[3])))
      

      To explain my problem to you, I want to add a row that belongs to one qtablewidget in another, I added the EventFilter function with event "AjouterAction" but each time I select another row in the first table they add value in the "y" variable and show me the error message:

      sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
      

      the query is good, everything is good except I don't understand why it stores in the variable "y" the old value and he adds to became 3 in this time.

      If you dont understand my problem i can explain with an exemple. Thanks :)

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @YassineKira Why don't you use https://doc.qt.io/qt-5/qsqldatabase.html instead of sqlite3?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 1 Reply Last reply
      1
      • Y YassineKira

        Hello

        def eventFilter(self, obj, event):
            
        
            if obj == self.ui.tableWidget_2.viewport() and event.type() == QEvent.MouseButtonPress:
        
                if event.button() == Qt.RightButton:
                    idx = self.ui.tableWidget_2.indexAt(event.pos())
                    
                    if idx.isValid():
                        ajouterAction = QAction("Ajouter", self)
                        ajouterAction.setObjectName(str(idx.row()))
                        ajouterAction.triggered.connect(self.ajouter_action_triggered)
        
                        contextMenu = QMenu(self)
                        contextMenu.addAction(ajouterAction)
                        
                        contextMenu.exec(event.globalPos())
            
            return QMainWindow.eventFilter(self, obj, event)
        

        the fonction :

        def ajouter_action_triggered(self):  
            import os.path
        
            BASE_DIR = os.path.dirname(os.path.abspath(__file__))
            db_path = os.path.join(BASE_DIR, "database_prix.db")
            with sqlite3.connect(db_path) as db:
                cursor=db.cursor()
                row = int(QObject.sender(self).objectName())
                id = self.ui.tableWidget_2.item(row, 0).text()
                print(id)
                y=(str(id))
                command = ''' SELECT PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu = ? '''
                result= cursor.execute(command,y)                    
                x = result.fetchone()
                print(x)
                self.ui.tableWidget_3.setItem(0,0,QtWidgets.QTableWidgetItem(str(x[0])))
                self.ui.tableWidget_3.setItem(0,1,QtWidgets.QTableWidgetItem(str(x[1])))
                self.ui.tableWidget_3.setItem(0,2,QtWidgets.QTableWidgetItem(str(x[2])))
                self.ui.tableWidget_3.setItem(0,3,QtWidgets.QTableWidgetItem(str(x[3])))
        

        To explain my problem to you, I want to add a row that belongs to one qtablewidget in another, I added the EventFilter function with event "AjouterAction" but each time I select another row in the first table they add value in the "y" variable and show me the error message:

        sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
        

        the query is good, everything is good except I don't understand why it stores in the variable "y" the old value and he adds to became 3 in this time.

        If you dont understand my problem i can explain with an exemple. Thanks :)

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @YassineKira
        As @jsulm says, if you choose to use sqlite3 plus a load of Python code you might be better asking in forums dedicated to those.

        My Python is not perfect, but: y=(str(id)). I think there you are trying to create a list (or whatever it is called) of values here? If you only have a single value to put into that list don't you have to insert a trailing comma: y=(str(id), )?

        You are re-opening and connecting to the database on each action click. Aren't you supposed to open/connect to the database once and re-use the connection as necessary?

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @YassineKira Why don't you use https://doc.qt.io/qt-5/qsqldatabase.html instead of sqlite3?

          Y Offline
          Y Offline
          YassineKira
          wrote on last edited by YassineKira
          #4

          @jsulm Thanks i will try to use tour method @JonB i try this, it works Thanks a lot @JonB Now i will do what @jsulm propose i will work withe the qsql

          1 Reply Last reply
          0

          • Login

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