How to make the Qt designer lineEdit receive an integer or float with Python

Asked

Viewed 73 times

-1

I would like to know how to make a lineEdit of Qt designer receive a value of type integer or float and armezene that value received in a variable, because, I need to do mathematical operations with it.

By default, lineEdit takes a variable of type str.

I have the following code: >>>>>>>>>>>>>>>>>>>>>>>>>>

def chamar_segunda_tela():
    tela_total.show()
    cursor.execute('SELECT SUM(preco) FROM tb_registrados;')
    for t in cursor.fetchall():
        total = t

        tela_total.label_5.setText(f'{total[0]:>7.2f}')
        tela_total.label_6.setText(f'{total[0]:>7.2f}')

        dinheiro_recebido =  tela_total.lineEdit_3.text()

I’ve tried everything convert in every way, for example: int(screen-total.linEdit_3.text()). I’ve read the documentation, but I can’t find any specific method for integer or floating number. I tried using Qdoublevalidator unsuccessfully, I did the following:

def chamar_segunda_tela():
    tela_total.show()
    cursor.execute('SELECT SUM(preco) FROM tb_registrados;')
    for t in cursor.fetchall():
        total = t

        tela_total.label_5.setText(f'{total[0]:>7.2f}')
        tela_total.label_6.setText(f'{total[0]:>7.2f}')

        validacao_regra = QDoubleValidator(0.0, 5.0, 2)

        dinheiro_recebido =  tela_total.lineEdit_3.setValidator(validacao_regra)
        dinheiro_lol = tela_total.lineEdit_3.setText(dinheiro_recebido)

        print(dinheiro_recebido, type(dinheiro_recebido))
        print(dinheiro_lol, type(dinheiro_lol))

But then he returns me None<class: None Type> . I read about Inputmask, but I didn’t understand how to implement it. I find the documentation very confusing.

Can someone help me ??

  • explains please, you are on a screen and when you click on a button, you call the function and it has two tags (label_5 and label_6) and a data insertion line (lineEdit_3) What returns None refers to the "received money_money"?

  • Exactly, and there’s one more thing I discovered, I created a very simple interface, just with a single lineEdit, I made the default code and in this code worked the conversion. Eu fiz assim :&#xA;&#xA;def lixo():&#xA; teste_lixo = int(teste.lineEdit.text())&#xA; teste.lineEdit.setText("")&#xA; print(type(teste_lixo))&#xA;&#xA;app = QtWidgets.QApplication([])&#xA;teste = uic.loadUi("lixo.ui")&#xA;teste.lineEdit.returnPressed.connect(lixo) # Uses enter to send pushButtom test.show() app.exec() Gave this print to see if the conversion worked and worked!

  • And I have one more function called "def funcao_1( ):" in that same py file, I went to try a conversion and it also worked. That is, for some reason I didn’t find out, it is not allowing the conversion of Lineedit to type int or float. It is very strange!

  • just one more question: have you tried to take the dinheiro_recebido = tela_total.lineEdit_3.text() loop? would just take out the indentation

  • Yes, I did, Carlos, thank you very much for your attention. Do you find it interesting to publish the solution by answering my own question? I don’t know if the OS will block this.

  • Publish yes... no problem

Show 1 more comment

1 answer

0

Guys, I solved the problem, before I used this code:

def chamar_segunda_tela():
    tela_total.show()
    cursor.execute('SELECT SUM(preco) FROM tb_registrados;')
    for t in cursor.fetchall():
        total = t

        tela_total.label_5.setText(f'{total[0]:>7.2f}')
        tela_total.label_6.setText(f'{total[0]:>7.2f}')

        dinheiro_recebido = tela_total.lineEdit.text() # Esse código era o problema, dava erro e não conseguia converter de str para float

And I needed the lineEdit to be a float type actually, so I could bill and print the change on the screen. And when I tried to convert, it returned the error "(<class 'Valueerror'>, Valueerror("could not Convert string to float: ''"), <traceback Object at 0x00000227F6FF0500>)"

I tried to convert it this way:

dinheiro_recebido = float(tela_total.lineEdit.text())

I saw many users converting so without problems, but only my code gave problems, after breaking the head, a boy helped me and the solution found by her was to create another function and this time it worked, he was able to perform the conversion from str to float without major problems.

def chamar_segunda_tela(): # Essa tela exibe o total da compra e o total a pagar (que basicamente são a mesma coisa)
    cursor = mydb.cursor()
    tela_total.show()

    cursor.execute(f'SELECT SUM(preco) FROM tb_registrados;')
    for t in cursor.fetchall():
        total = t
        tela_total.label_5.setText(f'{total[0]:>7.2f}')
        tela_total.label_6.setText(f'{total[0]:>7.2f}')
    cursor.close()

def tela_pagamento(): # Essa tela é a mesma que a anterior, apenas foi necessário criar a função de receber o dinheiro e imprimir o troco em uma função separada 
    cursor = mydb.cursor()
    dinheiro_recebido = float(tela_total.lineEdit.text()) # Aqui a conversão não deu mais erro.
    tela_total.lineEdit.clear()

    cursor.execute(f'SELECT SUM(preco) FROM tb_registrados;')
    for t in cursor.fetchall():
        total = t
        print(total, type(total), type(total[0]))
        troco = dinheiro_recebido - total[0]

        tela_total.label_7.setText(f'{troco:>7.2f}')

I attached an image to view the interface working. Thank you all for your attention ! inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.