PDA

View Full Version : help on QList<QTableWidgetItem*>



roncriss
23rd November 2008, 22:10
how do iterate over QList retrieve the values then display them on a dialog for editing?
here is my function


void AddressBook::editItem()
{

if( !tableWidget->currentItem())
return;
else
QList<QTableWidgetItem*> selected = tableWidget->selectedItems();
for (QList<QTableWidgetItem *>::iterator it=selected.begin();it!=selected.end();it++)
{
QString item[i] = (*it);
}

editDialog dlg;
dlg.setId(item[0]);
dlg.setname(item[1]);
dlg.setemail(item[2]);
dlg.setphone(item[3]);
dlg.setgrossSalary(item[4]);


if( dlg.exec() == QDialog::Accepted )
{
double HRA = dlg.grossSalary().toDouble() * 0.2;
double transport = dlg.grossSalary().toDouble() * 0.1;
double medical = 200.0;

item[0] = dlg.id();
item[1] = dlg.name();
item[2] = dlg.email();
item[3] = dlg.phone();
item[4] = dlg.grossSalary();

item[5] .setNum( item[4].toDouble() - HRA - transport - medical,'f',2 ); //netsalary

item[6].setNum(item[5].toDouble() * 12,'f',4);
++row;
for(int col = 0; col < 7 ; col++)
{

QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(item[col]));

newItem->setTextAlignment(Qt::AlignCenter);

tableWidget->setItem(row, col, newItem);
}
}

}

when i compiled the program i got this error!




Running build steps for project myaddressbook...
Configuration unchanged, skipping QMake step.
Starting: /usr/bin/make debug -w
make: Entering directory `/home/roncriss/development/myaddressbook'
/usr/bin/make -f Makefile.Debug
make[1]: Entering directory `/home/roncriss/development/myaddressbook'
g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtNetwork -I/usr/include/QtNetwork -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -I. -Idebug -I. -o debug/addressbook.o addressbook.cpp
addressbook.cpp: In member function ‘void AddressBook::editItem()’:
addressbook.cpp:115: error: ‘selected’ was not declared in this scope
addressbook.cpp:117: error: variable-sized object ‘item’ may not be initialized
addressbook.cpp:117: warning: unused variable ‘item’
addressbook.cpp:121: error: ‘item’ was not declared in this scope
make[1]: Leaving directory `/home/roncriss/development/myaddressbook'
make: Leaving directory `/home/roncriss/development/myaddressbook'
make[1]: *** [debug/addressbook.o] Error 1
make: *** [debug] Error 2
Exited with code 2.
Error while building project myaddressbook
When executing build step 'Make'

spirit
24th November 2008, 07:04
did you include
#include <QList>?

pastor
24th November 2008, 09:19
2 roncriss: This problem occur because you forgot about {} for else. Your code should be like that:


if( !tableWidget->currentItem())
return;
else {
QList<QTableWidgetItem*> selected = tableWidget->selectedItems();

....
}