c++ - Retrieve value of QTableWidget cells which are QLineEdit widgets -
i create qlineedit,set validator , put on table code:
ui->moneytablewidget->setcellwidget(rowsnum, 1, newqlineedit);
then i've got another class manipulating table's data doing sum of every value of column. here's code:
int calculator::calculatepricessum(qtablewidget &moneytablewidget){ double total = 0; qwidget *tmplineedit; qstring *tmpstring; for(int row=0; row<moneytablewidget.rowcount(); row++){ tmplineedit = (qlineedit*)moneytablewidget.cellwidget(row,1); tmpstring = tmplineedit.text(); total += tmpstring->todouble(); } return total; }
but building fails error:
/home/testpec/src/nokia qt/moneytracker-build-simulator/../moneytracker/calculator.cpp:11: error: cannot convert ‘qlineedit*’ ‘qwidget*’ in assignment
why convertion error?
another subquestion: passing table reference saves memory right? problem? im developing nokia smartphone , think passing object value waste of memory...(sorry if dumb question i'm little rusty c++ , pointers stuff...)
when declare tmplineedit
, should declaring qlineedit*
instead of qwidget*
. loop grabs widget, casts qlineedit*
, tries put qwidget*
. also, i'd recommend using qobject_cast<qlineedit*>
(or dynamic_cast
) can ensure cast succeeded.
int calculator::calculatepricessum(qtablewidget &moneytablewidget){ double total = 0; qlineedit* tmplineedit; qstring tmpstring; for(int row=0; row < moneytablewidget.rowcount(); row++) { tmplineedit = qobject_cast<qlineedit*>(moneytablewidget.cellwidget(row,1)); if(null == tmplineedit) { // indicate failure. } tmpstring = tmplineedit->text(); total += tmpstring.todouble(); } return total; }
as second question, passing reference idea - know of classes in qt (qimage in particular) use reference counting , implicit sharing can pass around value without worrying implications of large copy operations, i'm not sure if qtablewidget in category well.
Comments
Post a Comment