PDA

View Full Version : Remove last item with QStingList



pcoliver
5th January 2012, 10:38
Hello all,

As always I turn to the experts. I have little time programming in Qt4 and I have yet a lot of documentation to review progress on my projects, but hey I guess that happened to us all.

I have a problem not solve it.

I have a list of this type 01000000000,02000000000,03000000000 defined with QStringList.
The problem I have is that it removes the last element of the list 03000000000. If I've managed to eliminate the first and second but not the ulitmo. I require your knowledge to see if you can give me some solution, pq stand for two days with this nonsense, I know I'm clumsy.

I leave part of the code to see if you can lend a hand to me.
What I do is first find the total number of items in the list.
Locate the item that interests me and that begins to erase 02.
Delete the item.

I tried to delete the item RemoveAt (), with removeLast, but if the item does not get ulitmo delete it.

I leave the code and thank you very much for your help.


***************************************

#define REGPATH_XXXXXXXX "HKEY_LOCAL_MACHINE\\SOFTWARE\\XXXXX"
int TotalListaN;
QStringList listOODelN;
int PosicionOODelN=0;
int IndiceOODelN=0;
QString MostrarMensaje ="";
QString sAplicacionesOODelN = settings::getRegAppInstaladas();
int TotalElementosListaN = 0;
int ntN=0;
int IndiceOON2=0;
int PosicionOON2=0;

QSettings settings(REGPATH_XXXXXXXX,QSettings::NativeFormat) ;

// listOODelN.sort();

if(sAplicacionesOODelN.size() > 0)
{
listOODelN = sAplicacionesOODelN.split(",");
}
QStringList::const_iterator constIteratorOODelN;

//Vamos a averiguar donde esta la cadena 02000 en la lista
for (constIteratorOODelN = listOODelN.constBegin(); constIteratorOODelN != listOODelN.constEnd(); ++constIteratorOODelN){

TotalElementosListaN=ntN;
}

int vListCount;
vListCount = listOODelN.count();
int i;
bool encontrado(false);
int PosicionLista=-1;

QMessageBox::information ( 0,"TOTAL ELEMENTOS LISTA", "El numero total de elementos en la lista es: "+QString::number(vListCount));

for (i=0; i
{
QMessageBox::information(0,"Contenido Lista i ", listOODelN[i],"Ok",0,0,0);
QMessageBox::information ( 0,"Posicion Elemento", "La posicion en la lista es: "+QString::number(i));

if (listOODelN[i].startsWith("02")){
PosicionOON2=i;
encontrado=true;
QMessageBox::information(0,"Encontrado Valor en Posicion :", QString::number(i));
PosicionLista=i;
}

}

int vListCountReal=vListCount-1;
foreach (QString str, listOODelN) {
//if (str.startsWith("02")){QMessageBox::information(0, "String Parser", listOODelN[i],"Ok",0,0,0);
//QMessageBox::information(0,"VALORES A MOSTRAR:","Valor encontrado",QString::bool(encontrado));
QMessageBox::information(0,"Total Valores Lista vlistcount :", QString::number(vListCount));
QMessageBox::information(0,"Posicion en la lista PosicionLista:", QString::number(PosicionLista));

if (str.startsWith("02") && encontrado==true){
if (PosicionLista=vListCountReal)
{
QMessageBox::information(0,"VALOR ENCONTRADO ","ENCONTRADO OK");
QMessageBox::information(0,"valor a borrar ","Posicion Lista:", QString::number(PosicionLista), "vListCountReal:",QString::number(vListCount-1));
listOODelN.append( );
listOODelN.removeAt(PosicionLista);
}else
{
PosicionOODelN=PosicionLista;
QMessageBox::information(0,"ANTES DE BORRAR . POSICION EN LA LISTA",QString::number(PosicionOODelN));
listOODelN.removeAt(PosicionOODelN);
//QMessageBox::information(0,"DEBUG AGENTE",listOODel.at(PosicionOO));
sAplicacionesOODelN = listOODelN.join(",");
// QMessageBox::information(0,"BORRAMOS CONTENIDO LISTA",sAplicacionesOODel1);
}
}
else {
settings.setValue("Apl", sAplicacionesOODelN);

}
IndiceOODelN++;

}

}//Fin Procedimiento BorrarContenidoListaOO()

****************************************

marcvanriet
5th January 2012, 12:04
Hi,

Some comments :
- use the QUOTE tags when you put code in a message
- "ulitmo" is not an English word. What do you mean by that ?
- your code would be easier to read for us if it contained English comments and function names
- if you just dump a piece of code, pleas add comments as to where the part is that doesn't work

removeLast(), removeAt() etc. all work without problems on a QStringList


for (i=0; i
{

This is clearly code that doesn't even compile. Only post real code.

Regards,
Marc

ChrisW67
5th January 2012, 22:49
- use the QUOTE tags when you put code in a message

Even better, use the
... tags


- "ulitmo" is not an English word. What do you mean by that ?

I suspect it is mistyped "ultimo", Italian for "last".

pcoliver
10th January 2012, 10:25
Hello all,

Here's the source code. Now the comments are in English. I have not managed to remove the last item that is stored in the list.


I hope you can help me.

Thanks all


#define REGPATH_XXXXXXXX "HKEY_LOCAL_MACHINE\\SOFTWARE\\XXXXX"
int TotalListaN;
QStringList listOODelN;
int PosicionOODelN=0;
int IndiceOODelN=0;
QString MostrarMensaje ="";
QString sAplicacionesOODelN = settings::getRegAppInstaladas();
int TotalElementosListaN = 0;
int ntN=0;
int IndiceOON2=0;
int PosicionOON2=0;

QSettings settings(REGPATH_XXXXXXXX,QSettings::NativeFormat) ;



if(sAplicacionesOODelN.size() > 0)
{
listOODelN = sAplicacionesOODelN.split(",");
}
QStringList::const_iterator constIteratorOODelN;

//find String 02000 into the list
for (constIteratorOODelN = listOODelN.constBegin(); constIteratorOODelN != listOODelN.constEnd(); ++constIteratorOODelN){

TotalElementosListaN=ntN;
}

int vListCount;
vListCount = listOODelN.count();
int i;
bool encontrado(false);
int PosicionLista=-1;

QMessageBox::information ( 0,"TOTAL LIST ELEMENTS", "The total elements into the list is: "+QString::number(vListCount));

for (i=0; i
{
QMessageBox::information(0,"Content list i ", listOODelN[i],"Ok",0,0,0);
QMessageBox::information ( 0,"Element Position", "The position into the list : "+QString::number(i));

if (listOODelN[i].startsWith("02")){
PosicionOON2=i;
encontrado=true;
QMessageBox::information(0,"Find value :", QString::number(i));
PosicionLista=i;
}

}

int vListCountReal=vListCount-1;
foreach (QString str, listOODelN) {
QMessageBox::information(0,"Total Elements into the list vlistcount :", QString::number(vListCount));
QMessageBox::information(0,"Position List PosicionLista:", QString::number(PosicionLista));

if (str.startsWith("02") && encontrado==true){
if (PosicionLista=vListCountReal)
{
QMessageBox::information(0,"Value found ","FOUND OK");
QMessageBox::information(0,"value delete ","Position List:", QString::number(PosicionLista), "vListCountReal:",QString::number(vListCount-1));
listOODelN.append( );
listOODelN.removeAt(PosicionLista);
}else
{
PosicionOODelN=PosicionLista;
QMessageBox::information(0,"before delete . LIST POSITION",QString::number(PosicionOODelN));
listOODelN.removeAt(PosicionOODelN);

sAplicacionesOODelN = listOODelN.join(",");

}
}
else {
settings.setValue("Apl", sAplicacionesOODelN);

}
IndiceOODelN++;

}

}

marcvanriet
10th January 2012, 11:40
Use the CODE tags

for (i=0; i
{
Only post real code. The above is just blatently wrong.

^NyAw^
10th January 2012, 11:45
Hi,

Explain us in a better way what is your problem.
I'm not able to understand if you have a QStringList with only one element "01000000000,02000000000,03000000000", or this QStringList contains three elements,...
If you have a QStringList with N elements you can remove the last element using QStringList::removeLast() method.