K
K
Kurum2018-11-29 00:43:02
Python
Kurum, 2018-11-29 00:43:02

Why does selectedText in QLineEdit return an empty string?

I run the program, highlighting a part of the text in the text field, I press the button, and I get an empty line instead of the selected text. Why is this happening?

# Python 3. PyQt4
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore


# ГРАФИКА
class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.resize(400, 150) # шир / выс окна         

        # БЛОК РАЗМЕТКИ
        self.vbox = QtGui.QVBoxLayout()
        # ---
        self.pole_vvod = QtGui.QLineEdit('djf j.f ndsf ndk adf')
        self.vbox.addWidget(self.pole_vvod)
        # ---
        self.btn = QtGui.QPushButton('Пуск')
        self.btn.clicked.connect(self.on_click)
        self.vbox.addWidget(self.btn)
        # ---
        self.setLayout(self.vbox)
        # ---

    # ЛОГИКА
    def on_click(self):
        #text_1 = self.pole_vvod.text()
        text = self.pole_vvod.selectedText()
        #text = self.pole_vvod.setSelection(1,3)
        print(text)


# КОНЕЦ
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Window()   
    window.show()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bbkmzzzz, 2018-11-29
@Pyrym

When the button is clicked, this is what happens:
lineEdit loses focus and clears the selection -> focus is given to the button -> the button fires a click event.
lineEdit has a selectionChanged signal, it is emitted when the selection changes, catch it and call selectedText in it, or change the behavior when focus is lost.

K
Kurum, 2018-11-29
@Pyrym

working version

# Python 3. PyQt4
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore


# ГРАФИКА
class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.resize(400, 150) # шир / выс окна  
        
        # ПЕРЕМЕННЫЕ КЛАССА
        #self.sel_text = ''
        self.sel_text_lst = []      

        # БЛОК РАЗМЕТКИ
        self.vbox = QtGui.QVBoxLayout()
        # ---
        self.pole_vvod = QtGui.QLineEdit('djf j.f ndsf ndk adf')
        self.pole_vvod.selectionChanged.connect(self.tx)
        self.vbox.addWidget(self.pole_vvod)
        # ---
        self.btn = QtGui.QPushButton('Пуск')
        self.btn.clicked.connect(self.on_click)
        self.vbox.addWidget(self.btn)
        # ---
        self.setLayout(self.vbox)
        # ---

    # ЛОГИКА
    def tx(self):
        self.sel_text = self.pole_vvod.selectedText()
        #print(self.sel_text)
        self.sel_text_lst.insert(0,self.sel_text)
        if len(self.sel_text_lst) > 2:
            self.sel_text_lst.pop(2)
        
    def on_click(self):
        #text = self.sel_text
        text = self.sel_text_lst[1]
        print(text)


# КОНЕЦ
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Window()   
    window.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question