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. How to enter Logical expression in QLineEdit with QCompleter?

How to enter Logical expression in QLineEdit with QCompleter?

Scheduled Pinned Locked Moved Solved General and Desktop
qt5
4 Posts 2 Posters 459 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.
  • S Offline
    S Offline
    Selvajothi
    wrote on last edited by Selvajothi
    #1

    I have a lineEdit in which I have to enter Logical expression like(one*two+three). I have loaded QStringList["one", "two", "three"]. Now the issue is Qcompleter clears the lineEdit everytime I highlighted the text using Arrow keys(up & down). In my code i have connect the highlighted signal with a slot which clears the QLineEdit.

    Kindly give me a solution enter logical expression in QLineEdit with QCompleter

    class Completer(QCompleter):
    def __init__(self, *args, **kwargs):
        super(Completer, self).__init__(*args, **kwargs)
    
        self.setCaseSensitivity(Qt.CaseInsensitive)
        self.existing_txt = ""
        self.counter = 0
        self.highlighted.connect(self.onActivated)
    # Add texts instead of replace
    def pathFromIndex(self, index):
        try:
            if self.counter == 0 :
                path = QCompleter.pathFromIndex(self, index)
                self.existing_txt = self.widget().text()
                self.existing_txt=self.existing_txt[0 : self.start_index : ] + self.existing_txt[self.end_index: :]
                self.existing_txt = self.existing_txt[: self.start_index] + path + self.existing_txt[self.start_index:]
                path = self.existing_txt
                self.widget().setText(path)
                self.counter =1
                return path
            else:
                self.counter = 0
                return self.existing_txt
    
        except Exception as e:
            alert = QMessageBox()
            alert.setWindowTitle("Warning!")
            main.error("pathFromIndex" + str(e))
            alert.setIcon(QMessageBox.Warning)
            alert.setText("Error in pathfromIndex!")
            alert.exec_()
    def onActivated(self):
        try:
            # self.existing_txt = self.widget().text()
            QTimer.singleShot(0,self.widget().clear)
            # self.widget().setText(self.existing_txt)
    
        except Exception as e:
            main.error("lineedit clear" + str(e))
    def splitPath(self, path):
        try:
            char_list = ["*", "+", "(", ")", "~"]
            input = self.widget().text()
            self.existing_txt = input
            point = self.widget().cursorPosition()
            self.end_index = len(path)
            self.start_index = -1
            for i in char_list:
                index = path.find(i,point)
                if index != -1 and self.end_index > index:
                    self.end_index = index
            for i in char_list:
                index = path.rfind(i,0,point )
                if index != -1 and self.start_index < index:
                    self.start_index = index
            self.start_index += 1
            if self.start_index == self.end_index:
                return [""]
            path = path[self.start_index: self.end_index:]
            return [path]
        except Exception as e:
            alert = QMessageBox()
            alert.setWindowTitle("Warning!")
            main.error("splitPath" + str(e))
            alert.setIcon(QMessageBox.Warning)
            alert.setText("Error in splitpath!")
            alert.exec_()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      As silly as it may sound but if you do not want to have your line edit cleared when you move up and down the arrow, do not clear based on the highlighted signal.

      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
      0
      • S Offline
        S Offline
        Selvajothi
        wrote on last edited by
        #3

        If i didn't clear the QLineEdit it is appending the text in the QLineEdit for every move of arrow like onetwothree

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Selvajothi
          wrote on last edited by
          #4

          solved the problem by adding self.end_index = len(self.existing_txt) in pathFromIndex after getting the text value from QLineEdit

          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