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

How to enter Logical expression in QLineEdit with QCompleter?



  • 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_()

  • Lifetime Qt Champion

    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.



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



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


Log in to reply