Reputation: 126
I'm trying to access an int value stored in my QTreeWidgetItem. This is what I'm currently trying, but it's not working.
void RecordWidget::recordWorkerStatusChanged(WorkerStatus status) {
status.insertIntoTree(ui->treeWidget);
qint64 qp = ui->treeWidget->topLevelItem(0)->child(2)->QTreeWidgetItem::data(0, Qt::UserRole).toInt();
}
The data I'm trying to access is the child at index 2, that is the avg bitrate. I want to access the value (QString::number((info.bytes*8*1000.0/info.currentTime)
void insertIntoTree(QTreeWidget *tree) {
tree->clear();
for (int i = 0; i < streams.count(); i++) {
quint64 key = streams.keys().at(i);
const StreamInfo &info = streams.value(key);
QTreeWidgetItem* parent = makeItem(StreamId::calcName(key), true);
tree->addTopLevelItem(parent);
parent->addChild(makeItem(QString(tr("size %1 MB")).arg(QString::number(info.bytes/1000000.0, 'f', 2))));
parent->addChild(makeItem(QString(tr("duration %1")).arg(QDateTime::fromTime_t(info.currentTime/1000).toUTC().toString("HH:mm:ss"))));
parent->addChild(makeItem(QString(tr("avg bitrate %1 Mbit/s")).arg(QString::number((info.bytes*8*1000.0/info.currentTime)/1000000.0, 'f', 2))));
parent->addChild(makeItem(QString(tr("protocol %1")).arg(info.protocolName())));
parent->addChild(makeItem(QString(tr("bitrate mode %1")).arg(info.bitrateModeName())));
parent->addChild(makeItem(QString(tr("%1 TS/IP")).arg(info.tsPerIp)));
if(info.networkJitters != 0){
parent->addChild(makeItem(QString(tr("%1 µs IAT deviation ")).arg(info.networkJitters)));
}
parent->addChild(makeItem(QString(tr("%1 PIDs")).arg(info.pidMap.size())));
quint64 tsErrCount = info.tsErrors.totalErrors();
QTreeWidgetItem* tserrors = makeItem(QString(tr("TsErrors (%1)")).arg(tsErrCount));
parent->addChild(tserrors);
if (tsErrCount > 0) tserrors->setExpanded(true);
QMapIterator<TsErrors::ErrorType, qint64> it(info.tsErrors.errorCounter);
while(it.hasNext()) {
it.next();
makeTsError(tserrors, it.value(), it.key());
}
parent->setExpanded(true);
tree->clearSelection();
parent->setSelected(true);
}
}
};
QTreeWidgetItem* makeItem(QString text, bool parent = false) {
QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(text));
item->setDisabled(!parent);
return item;
}
Upvotes: 1
Views: 1440
Reputation:
QTreeWidgetItem::data(0, Qt::UserRole)
fetches data set with QTreeWidgetItem::setData(0, Qt::UserRole, yourData)
. You are creating items with formatted strings and expecting QTreeWidgetItem::data
to recover the original data used to format those strings. That is, after
QString(tr("avg bitrate %1 Mbit/s")).arg(QString::number((info.bytes*8*1000.0/info.currentTime)/1000000.0, 'f', 2))))
the calculated value is baked into the QString
. You would need to parse the string, e.g. "avg bitrate 10 Mbit/s" to extract the value again.
Instead, consider passing the original data to your makeItem
helper and do the string formatting there. This will enable makeItem
to store the original data with QTreeWidgetItem::setData
.
For example:
// Amend makeItem call as follows:
auto value = (info.bytes*8*1000.0/info.currentTime)/1000000.0;
parent->addChild(
makeItem(QString(tr("avg bitrate %1 Mbit/s")).arg(QString::number(value, 'f', 2),
value
));
// Amend makeItem as follows:
QTreeWidgetItem* makeItem(QString text, QVariant value, bool parent = false) {
QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(text));
item->setData(0, Qt::UserRole, value);
item->setDisabled(!parent);
return item;
}
Note that you can check QVariant::toInt
conversion success:
// Amend:
qint64 qp = ui->treeWidget->topLevelItem(0)->child(2)->QTreeWidgetItem::data(0, Qt::UserRole).toInt();
// To:
auto item = ui->treeWidget->topLevelItem(0)->child(2);
auto itemData = item->QTreeWidgetItem::data(0, Qt::UserRole);
bool success;
qint64 qp = itemData.toLongLong(&success);
// Now you can check _success_ for whether the conversion worked.
Upvotes: 2