Results 1 to 4 of 4

Thread: QString.remove() Why not working?

  1. #1
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QString.remove() Why not working?

    I've got my slot:

    Qt Code:
    1. void MainWindow::fdelete()
    2. {
    3. ui->lineEdit->text().remove(ui->lineEdit->text().length() , 1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. The following user says thank you to Noob for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString.remove() Why not working?

    How did you determine that it is not working? You did not store the result of the remove() operation anywhere.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:


  5. #3
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QString.remove() Why not working?

    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:

    Qt Code:
    1. void MainWindow::deleteClicked()
    2. {
    3. if(ui->lineEdit->text().length() == 1) ui->lineEdit->setText("0");
    4. QString string_line = ui->lineEdit->text();
    5. string_line.remove(string_line.length()-1, 1);
    6. ui->lineEdit->setText(string_line);
    7. }
    To copy to clipboard, switch view to plain text mode 

    So we have to take 1 from the string length;
    Last edited by Noob; 5th May 2013 at 11:14.

  6. The following user says thank you to Noob for this useful post:


  7. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString.remove() Why not working?

    There is also QString::chop() if you want to end up with a simpler code.

    Qt Code:
    1. QString text = ui->lineEdit->text();
    2. text.chop(1);
    3. ui->lineEdit->setText(text);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following 2 users say thank you to wysota for this useful post:

    anda_skoa (8th May 2013)

Similar Threads

  1. Replies: 7
    Last Post: 24th September 2012, 07:17
  2. How to remove whitespace in qstring in beginning?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 20th August 2012, 08:16
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Replies: 3
    Last Post: 21st January 2011, 19:40
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.