updating a column rows (condition Barcode) using QMap<QString,Int>
-
Hi ~~
I have a problem Scanning Barcodes ... I want when the user scan a product twice or more it i will add +1 to the quantity column of that product (Barcode,QR Code) each time even if the user scan it and scan other products and he scan it again ..
note: i don't have problem receiving data from barcode scannerQMap<QString,int> countOfStrings; for(int i=0;i<BarcodeScanValues.count();i++) { countOfStrings[BarcodeScanValues[i]]++; } qDebug()<<countOfStrings; qDebug()<<countOfStrings.values(); qDebug()<<countOfStrings.keys(); ProductCounter=countOfStrings.values(); // QStringList productBarcode=countOfStrings.keys();
/* ("7613036309493", "4600680000640", "8133578000377", "7613036445818", "7613036445818") QMap(("4600680000640", 1)("8133578000377", 1)("7613036309493", 1)("7613036445818", 2)) (1, 1, 1, 2) ("4600680000640", "6133978000377", "7613036309493", "7613036445818")
*/
any idea about this algorithm how to use this to update QTablewidget ? many thanks <3
![](this picture is quite similar to the one i have only i don't have widget all are QTablewidget cells and i have a Barcode column so the algorithm should find the barcode inside the table if it's already exist it add +1 to it's row quantity and it update the price ...
any advices? or i must save as temp database? then updating values? -
QTableWidget maintains an internal QAbstractItemModel that contains the data that is being presented. You can use QAbstractItemModel::match() to find the QModelIndex of items matching a value. So, something like this (untested):
QAbstractItemModel *model = tableWidget->model(); QModelIndex startIndex = model->index(0, barcodeColumnNumber); QModelIndexList matches = model->match(startIndex, Qt::EditRole, barcodeValue, 1, Qt::MatchExactly); if (matches.count() > 0) { QModelIndex match = matches.at(0); match = match.siblingAtColumn(countColumn); int count = match.data(Qt::EditRole).toInt() + 1; model->setData(match, count); }
-
Thank You so much Bro You Are Life Save I wish All the best happy life for you <3
note : it works better than i ever imagine <3