I have a such problem:
I develop a simple database (PostgreSql).
One of the features that it keeps in column (column name: "ascii") a big text data (2000 rows) that reads from file (datatype in the table is BYTEA).
Here is the example of the code where I insert the data:
QFile *asciiFile = new QFile(ui->ascii_lineEdit->text());
asciiFile->open(QIODevice::ReadOnly);
QByteArray asciiArray = asciiFile->readAll();
qDebug() << asciiArray;
QSqlQuery *sourceQuery = new QSqlQuery();
sourceQuery->prepare("INSERT INTO source (image, image_material, archive, ascii, about) VALUES (:image, :image_material, :archive, :ascii, :about)");
sourceQuery->bindValue(":image", graphArray.toBase64());
sourceQuery->bindValue(":image_material", materialArray.toBase64());
sourceQuery->bindValue(":archive", archiveArray.toBase64());
sourceQuery->bindValue(":ascii", asciiArray.toBase64());
sourceQuery->bindValue(":about", ui->about_textEdit->toPlainText());
sourceQuery->exec();
Once more, the datatype of the column("ascii") that keeps is BYTEA.
Now I try to read this data (near 2000 lines) and they not displayed as well.
I try this method:
QByteArray asciiArray = QSqlQuery query = connector->getSourceAscii(item_id);
query.next();
QByteArray asciiArray = QByteArray::fromBase64(query.value("ascii").toByteArray());
QString *result = new QString(asciiArray);
qDebug() << *result;
It must be a lines of the digits but I have something like this:
�w߇�k��ۇ������z���������燸��wӇ�k�{�����w��};��{��x{��k�{k�{k�xk�x߾���{k�{�����������wㇸ��
The examples of the lines that I need:
2490 0,21979421377182 4,82690520584583E-02
2491 0,226718083024025 4,33071963489056E-02
Have you any suggestion about this? Thanks.
Related
I'm using the pleora sdk to capture images from an external camera and I am able to successfully write the data to tiff image files on disk. My next step is to change the data storage to SQLite instead of disk files.
I have PvBuffer *lBuffer pointer working fine. Now I need to convert that data to a format I can use to write to SQLite. I'm using Qt on linux so the QByteArray would be very convenient.
This is kind of a specific question for the pleora sdk and Qt. I'm hoping someone has experience with this.
PvRawData *rawData = lBuffer->GetRawData();
QByteArray ba;
//Need to copy the data from rawData to ba.
Thank you in advance.
I found an answer and wanted to post in case anybody else has something similar. I uses the reintepret_cast method.
data = lBuffer->GetDataPointer()
imgSize = lBuffer->GetPayloadSize();
const char *d = reinterpret_cast<char *>(data);
QByteArray ba(d, imgSize);
QSqlQuery q = QSqlQuery( db );
q.prepare("INSERT INTO imgData (image) values (:imageData)");
q.bindValue(":imageData", ba);
if ( !q.exec() )
qDebug() << "Error inserting image into table: " << q.lastError() << endl;
else
qDebug() << "Query executed properly" << endl;
How I can to insert the value from txt file in to the Qlist...
QList<QString> list_StRead;
list_StRead.insert();
I can sorting txt file ... its mean that my file is a line by line. than after the insert to the Qlist I want to write in to Qtabelewidget. How I must to do?? u must to be completely understand. see the img file
tnx for all....
Here is the pseudo code.
Please try it. (Not tested, code written in notepad. excuse me any syntax errors).
//Your list
QList<QString> list_StRead;
//Text stream object read data and insert in list.
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine(); //read one line at a time
list_StRead.push_back(line);
}
//loop your list
for(int i =0; i<list_StRead.size(); i++)
{
//Add by create your tablewidget item and append it.
YourTableWidget->setItem(rowNumber,colNumber,new QTableWidgetItem(list_StRead.at(i)))
}
I have a table node={id,name}, and a table segment={id,nodeFrom,nodeTo} in a SQLite db, where node.id and segment.id are AUTOINCREMENT fields.
I'm creating a QSqlTableModel for Node, as follows:
nodeModel = new QSqlTableModel(this,db);
nodeModel->setTable("Node");
nodeModel->setEditStrategy(QSqlTableModel::OnFieldChange);
and I use the following code for inserting nodes:
int addNode(QString name) {
QSqlRecord newRec = nodeModel->record();
newRec.setGenerated("id",false);
newRec.setValue("name",name);
if (not nodeModel->insertRecord(-1,newRec))
qDebug() << nodeModel->lastError();
if (not nodeModel->submit())
qDebug() << nodeModel->lastError();
return nodeModel->query().lastInsertId().toInt();
}
This seems to work. Now, for segments I define a QSqlRelationalTableModel, as follows:
segModel = new QSqlRelationalTableModel(this,db);
segModel->setTable("Segment");
segModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
segModel->setRelation(segModel->fieldIndex("nodeFrom"),
QSqlRelation("Node","id","name"));
segModel->setRelation(segModel->fieldIndex("nodeTo"),
QSqlRelation("Node","id","name"));
And then I have the following code for inserting segments:
int addSegment(int nodeFrom, int nodeTo) {
QSqlRecord newRec = segModel->record();
newRec.setGenerated("id",false);
newRec.setValue(1,nodeFrom);
newRec.setValue(2,nodeTo);
if (not segModel->insertRecord(-1,newRec)) // (*)
qDebug() << segModel->lastError();
if (not segModel->submitAll())
qDebug() << segModel->lastError(); // (*)
}
I can add successfully 280 nodes using addNode(). I can also add segments sucessfully if nodeFrom<=256 and nodeTo<=256. For any segment referencing a node greater or equal to 256 I get a
QSqlError("19", "Unable to fetch row", "Segment.nodeTo may not be NULL")
in one of the lines marked with a (*) of the addSegment function.
I've googled and found out that people are having other (apparently unrelated) problems when they hit the magical 256 record count. No solution seems to work with this particular problem.
What am I doing wrong?
Thanks!
The reason of this error lies in the void QRelation::populateDictionary() method which uses such a loop for (int i=0; i < model->rowCount(); ++i). If you use the database that does not report the size of the query back (e.g. SQLite), the rowCount() method will return this magical 256 value.
You can solve this by populating the relation model before using data(...) or setData(...). At first you can try with:
setRelation(nodeFromCol, QSqlRelation("Node", "id", "name"));
QSqlTableModel *model = relationModel(nodeFromCol);
while(model->canFetchMore())
model->fetchMore();
Try this way to fix
newRec.setValue(1,QVariant(nodeFrom));
newRec.setValue(2,QVariant(nodeTo));
I have a QByteArray i create manually:
QByteArray hexArray(QByteArray::fromHex("495676"));
If this was encoded ASCII it would be "IVv".
If I want to get a single byte of data from that array.
I can do that like this:
qDebug() << messageToBeSent_raw[0];
However, that outputs I, which is correct but I would like to get 49. What I'm looking for is an equivalent of the QByteArray::toHex() just for a single byte. Is there a way to do it?
You can use QString::number.
qDebug() << QString::number(hexArray[0], 16);
As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.
The q_size is 0x2500 (or the other endianness order - 0x0025).
What's the problem here ? I'm pretty sure q_size holds the data I need.
Something like this should work, using a data stream to read from the buffer:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
The toInt method parses a int if the QByteArray contains a string with digits. You want to interpret the raw bits as an integer. I don't think there is a method for that in QByteArray, so you'll have to construct the value yourself from the single bytes. Probably something like this will work:
int size = (static_cast<unsigned int>(q_size[0]) & 0xFF) << 8
+ (static_cast<unsigned int>(q_size[1]) & 0xFF);
(Or the other way around, depending on Endianness)
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
bool ok;
q_size.toHex().toInt(&ok, 16);
works for me
I had great problems in converting serial data (QByteArray) to integer which was meant to be used as the value for a Progress Bar, but solved it in a very simple way:
QByteArray data = serial->readall();
QString data2 = tr(data); //converted the byte array to a string
ui->QProgressBar->setValue(data2.toUInt()); //converted the string to an unmarked integer..
This works for me:
QByteArray array2;
array2.reserve(4);
array2[0] = data[1];
array2[1] = data[2];
array2[2] = data[3];
array2[3] = data[4];
memcpy(&blockSize, array2, sizeof(int));
data is a qbytearray, from index = 1 to 4 are array integer.
Create a QDataStream that operates on your QByteArray. Documentation is here
Try toInt(bool *ok = Q_NULLPTR, int base = 10) const method of QByteArray Class.
QByteArray Documentatio: http://doc.qt.io/qt-5/QByteArray.html