PDA

View Full Version : QString.remove() Why not working?



Noob
5th May 2013, 09:31
I've got my slot:



void MainWindow::fdelete()
{
ui->lineEdit->text().remove(ui->lineEdit->text().length() , 1);
}

And i can't figure out, why this is not working? I want to delete the last character on the right. So it has to be the last one added.
The text().length() should return the amount of characters inside the string, so this should be the position of the last one, am I wrong?

thanks for answer.

anda_skoa
5th May 2013, 10:57
How did you determine that it is not working? You did not store the result of the remove() operation anywhere.

Cheers,
_

Noob
5th May 2013, 11:07
well, I thought the remove operation is being carry out directly on the text object of the lineEdit, but i get it khnow, the text() function returns the text value, it is not giving the acces directly.

So it has to be done this way:


void MainWindow::deleteClicked()
{
if(ui->lineEdit->text().length() == 1) ui->lineEdit->setText("0");
QString string_line = ui->lineEdit->text();
string_line.remove(string_line.length()-1, 1);
ui->lineEdit->setText(string_line);
}

So we have to take 1 from the string length;

wysota
6th May 2013, 18:04
There is also QString::chop() if you want to end up with a simpler code.


QString text = ui->lineEdit->text();
text.chop(1);
ui->lineEdit->setText(text);