'Can't assign a QString as a value for a LineEdit in Qt

I am working on a simple calculator app in Qt. I want to display an error message when the user tries to divide by zero. I have tried the code below but the output just stays as 0.

            if(dblDisplayVal == 0.0){
            QString error = "Can't divide by zero!";

            ui->display->insert(error);
        }
        else{
            result = calcVal / dblDisplayVal;
            divTrigger = false;

        }

Any idea on how I can solve this issue?



Solution 1:[1]

you should use setText function instead of insert.

this is an example:

QLineEdit *lineEdit = new QLineEdit(this);
lineEdit->setReadOnly(true);
ui->gridLayout->addWidget(lineEdit, 0, 0, 1, 1);

QString  error = "Can't divide by zero!";

lineEdit->setText("Can't divide by zero!");

and it is the output:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parisa.H.R