Skip to content

Commit 1ebe7bf

Browse files
committed
Return numeric values from model when appropriate
When the EditRole is requested, the column type is numeric, and the stored value is numeric, the returned variant is a number (double or long-long). That allows differentiating the value for a SQL copy/paste operation. See issue #1952.
1 parent 9832a52 commit 1ebe7bf

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/ExtendedTableWidget.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,20 @@ void ExtendedTableWidget::copyMimeData(const QModelIndexList& fromIndices, QMime
684684
// Text data
685685
QByteArray text = bArrdata.toByteArray();
686686

687-
if (inSQL)
688-
result.append(sqlb::escapeString(text));
689-
else {
687+
if (inSQL) {
688+
// Escape string only if it isn't a number.
689+
switch(bArrdata.type()) {
690+
case QVariant::Double:
691+
case QVariant::Int:
692+
case QVariant::LongLong:
693+
case QVariant::UInt:
694+
case QVariant::ULongLong:
695+
result.append(text);
696+
break;
697+
default:
698+
result.append(sqlb::escapeString(text));
699+
}
700+
} else {
690701
result.append(text);
691702
// Table cell data: text
692703
if (text.contains('\n') || text.contains('\t'))

src/sqlitetablemodel.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,18 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
361361
}
362362
}
363363
} else if(role == Qt::EditRole) {
364-
if(!row_available)
364+
if(!row_available || data.isNull())
365365
return QVariant();
366-
return decode(data);
366+
QVariant decodedData = decode(data);
367+
// For the edit role, return the data according to its column type if possible.
368+
if(m_vDataTypes.at(column) == SQLITE_INTEGER &&
369+
decodedData.canConvert(QMetaType::LongLong)) {
370+
decodedData.convert(QMetaType::LongLong);
371+
} else if(m_vDataTypes.at(column) == SQLITE_FLOAT &&
372+
decodedData.canConvert(QMetaType::Double)) {
373+
decodedData.convert(QMetaType::Double);
374+
}
375+
return decodedData;
367376
} else if(role == Qt::FontRole) {
368377
QFont font = m_font;
369378
if(!row_available || data.isNull() || isBinary(data))

0 commit comments

Comments
 (0)