Results 1 to 7 of 7

Thread: Remove a string from QTextEdit help

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Remove a string from QTextEdit help

    Is it possible to remove a string from a QTextEdit? I am adding them using a QTreeWidget and QPushButton when I press on the QTreeWidgetItem it turns the item into a string then when I press on my QPushButton it puts the string into a QTextEdit, but what I would like to do is when I click on any QTreeWidgetItems it will turn it into a string then I press another QPushButton and it will see if that "string" is in the QTextEdit and if it is it will remove it.

    An Example:
    Select item "Apple" from QTreeWidgetItem.
    Creates string: QString item = "Apple";
    Click on my QPushButton ("ADD").
    AddsPlainText: QTextEdit->setPlainText(item);

    Select item "Apple" from QTreeWidgetItem.
    Creates string: QString item = "Apple";
    Click on my QPushButton ("REMOVE");
    finds string in QTextEdit: ?????????;
    removes string from QTextEdit: ??????;

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Remove a string from QTextEdit help

    Perhaps: QTextEdit::plainText(), QString::remove(), QTextEdit::setPlainText()

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Remove a string from QTextEdit help

    Thanks for advice. It requires a const QRegExp &rx. I will try and see what a QRegExp is then try it.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Remove a string from QTextEdit help

    There is more than one QString::remove() overload.

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Remove a string from QTextEdit help

    You are correct lol sorry most of time I just use the first overload. Didn't realize this one: QString & remove ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) but I don't think I am doing it coreectly I tried:
    Qt Code:
    1. textEdit->toPlainText().remove(selectedItem,Qt::CaseInsensitive);
    To copy to clipboard, switch view to plain text mode 
    where selectedItem is my QString. Guess I'll play around for a bit and try to figure it out.


    EDIT::
    after reading a little more on the class. the remove function is to remove characters/strings from a QString what I am trying to do is remove a string from a textEdit. so what I'm thinking I have to do is when I press the add button I send all the Strings to another string and then put that large string into the textEdit and then remove them from the large string i'll let you know if it works. thanks again for the suggestion.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Remove a string from QTextEdit help

    At its simplest you extract the string from the text edit, remove the bits you want, put the string back in text edit. Code it as three steps, make it work and then, if you really need to, combine steps.

  7. #7
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Remove a string from QTextEdit help

    Thanks man figured it out if anyone else wants to know here is my code:
    Qt Code:
    1. QString selectedFoods;
    To copy to clipboard, switch view to plain text mode 
    when I press the button:
    Qt Code:
    1. if(textEdit->isEmpty()){
    2. selectedFoods.append(selectedItems);
    3. } else {
    4. selectedFoods.append(", "+selectedItems);
    5. }
    To copy to clipboard, switch view to plain text mode 
    on the remove button:
    Qt Code:
    1. selectedFoods.remove(selectedItem);
    2. textEdit->setText(selectedFoods);
    To copy to clipboard, switch view to plain text mode 

    there is one slight problem though but I'm sure I can figure a way to solve it unless anyone has any suggestions.
    it removes all the strings with those characters for example if I have a string of "appleapple" and i remove "apple" it will make the first string "" instead of just "apple" probably a way to remove the first string behind the cursor and another bug is with the ", " prob need to do another if statement like
    Qt Code:
    1. if(selectedFoods.size()<1){
    2. selectedFoods.remove(", "+selectedItems);
    3. }
    To copy to clipboard, switch view to plain text mode 
    or something similar to that.

    actually to fix that second bug with the ", " I can instead of putting that in the string just put it in the button press code like
    Qt Code:
    1. textEdit.setText(", "+selectedFoods)
    To copy to clipboard, switch view to plain text mode 


    Added after 1 15 minutes:


    Edit fixed all my problems here is the code to remove just 1 item =]
    Qt Code:
    1. int index = selectedFoods.indexOf(selectedItem);
    2. int length = selectedItem.length();
    3. if(selectedItem.length() == selectedFoods.length() && index == 0){
    4. selectedFoods.replace(index,length,"");
    5. } else if(selectedItem.length() < selectedFoods.length() && index == 0){
    6. selectedFoods.replace(index,length+2,"");
    7. } else if(selectedItem.length() < selectedFoods.length() && index != 0){
    8. selectedFoods.replace(index-2,length+2,"");
    9. }
    10. textEdit->setText(selectedFoods);
    To copy to clipboard, switch view to plain text mode 

    Thanks for all the help
    Last edited by giblit; 25th March 2013 at 05:11.

Similar Threads

  1. Replies: 10
    Last Post: 17th July 2014, 11:52
  2. Replies: 3
    Last Post: 29th June 2012, 16:13
  3. std formatted string with QTextEdit
    By nileshsince1980 in forum Qt Programming
    Replies: 14
    Last Post: 25th February 2010, 07:46
  4. Remove a string from QStringList
    By bismitapadhy in forum Newbie
    Replies: 4
    Last Post: 6th July 2009, 10:28
  5. Replies: 4
    Last Post: 21st June 2009, 19:06

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
  •  
Qt is a trademark of The Qt Company.