PDA

View Full Version : Remove a string from QTextEdit help



giblit
25th March 2013, 00:17
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: ??????;

ChrisW67
25th March 2013, 00:27
Perhaps: QTextEdit::plainText(), QString::remove(), QTextEdit::setPlainText()

giblit
25th March 2013, 00:31
Thanks for advice. It requires a const QRegExp &rx. I will try and see what a QRegExp is then try it.

ChrisW67
25th March 2013, 02:24
There is more than one QString::remove() overload.

giblit
25th March 2013, 03:21
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:
textEdit->toPlainText().remove(selectedItem,Qt::CaseInsensit ive); 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.

ChrisW67
25th March 2013, 03:27
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.

giblit
25th March 2013, 05:11
Thanks man figured it out if anyone else wants to know here is my code:

QString selectedFoods;
when I press the button:

if(textEdit->isEmpty()){
selectedFoods.append(selectedItems);
} else {
selectedFoods.append(", "+selectedItems);
}
on the remove button:


selectedFoods.remove(selectedItem);
textEdit->setText(selectedFoods);


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
if(selectedFoods.size()<1){
selectedFoods.remove(", "+selectedItems);
} 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
textEdit.setText(", "+selectedFoods)

Added after 1 15 minutes:

Edit fixed all my problems here is the code to remove just 1 item =]

int index = selectedFoods.indexOf(selectedItem);
int length = selectedItem.length();
if(selectedItem.length() == selectedFoods.length() && index == 0){
selectedFoods.replace(index,length,"");
} else if(selectedItem.length() < selectedFoods.length() && index == 0){
selectedFoods.replace(index,length+2,"");
} else if(selectedItem.length() < selectedFoods.length() && index != 0){
selectedFoods.replace(index-2,length+2,"");
}
textEdit->setText(selectedFoods);

Thanks for all the help