How do I set the UTF-8 encoding for reading string. example
// outData = "абцд" -> Cyrillic alphabet
QMessageBox::information(this,"title",outData,"ok");
void MainWindow::read() {
myProcess = new QProcess();
myProcess->start(QDir::currentPath() + "program.exe args args");
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
}
void MainWindow::readOutput() {
QString outData = myProcess->readAllStandardOutput();
}
Related
Im working with esp32s3 feather right now. I need to log some data when there is no WiFi connection. Write works fine for me but when I want to read line with readStringUntil(), i always get "null" at the end of read string. Here is code:
In loop:
if ((millis() - sdLast) > sdTime)
{
for (int i = 0; i < maxSensors; i++)
{
if (activeSensors[i] != "")
{
String requestData = "{\"data\":[{\"name\":\"" + sensorNames[i] + "\" ,\"temp\": \"" + actTemp[i] + "\",\"hum\": \"" + actHum[i] + "\",\"time\": \"" + actTime[i] + "\",\"scanCount\": \"" + scanCount[i] + "\"}]}\n";
appendFile(SD, "/all.txt", requestData.c_str());
sdReady = true;
}
}
sdLast = millis();
}
Function to read from file:
void readLinesSD(fs::FS &fs, const char *path)
{
File file = fs.open(path);
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
if (!file)
{
Serial.println("Failed to open file for reading");
return;
}
while (file.available())
{
buffer = file.readStringUntil('\n');
serializeJson(doc, buffer);
Serial.println(buffer);
int httpResponseCode = http.POST(buffer);
Serial.println(httpResponseCode);
doc.clear();
delay(200);
}
http.end();
file.close();
}
Append function:
void appendFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file)
{
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message))
{
Serial.println("Message appended");
}
else
{
Serial.println("Append failed");
}
file.close();
}
SO basically I want to save data to file and then, when the WiFi connection is back I want to send data to database for further presentation. When I read file i got this results:
{"data":[{"name":"P RHT 902631" ,"temp": "19.53","hum": "48","time": "1674746950","scanCount": "4"}]}null
{"data":[{"name":"P RHT 90262A" ,"temp": "19.38","hum": "50","time": "1674746957","scanCount": "4"}]}null
{"data":[{"name":"P RHT 902629" ,"temp": "19.36","hum": "49","time": "1674746958","scanCount": "5"}]}null
I tried using some special characters like "%" at the end of lines and then read line untill this special character but got same problem. When I used the same function on my other esp32 board everything was read fine. Anyone know what might cause this problem? Thanks for any help
I messed up with function to read file. I serialized for no reason. Without it, "null" disapear :)
This code downloads a video, but for some reason does not work
globals.h
QString videoDirectLink = "";
mainwindow.cpp
#include "globals.h"
void MainWindow::readOutput() {
QByteArray outData = myProcess->readAllStandardOutput(); // read from CMD
videoDirectLink = outData; // videoDirectLink Contains https://media08.vbox7.com/s/91/91f4c651car96dafe736.mp4
if (videoDirectLink.contains("https")) {
// SSL downloading
QNetworkAccessManager *manager2 = new QNetworkAccessManager(this);
connect(manager2, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
QNetworkRequest *req = new QNetworkRequest();
req->setUrl(QUrl(videoDirectLink)); //videoDirectLink
QSslConfiguration configSsl = QSslConfiguration::defaultConfiguration();
configSsl.setProtocol(QSsl::AnyProtocol);
req->setSslConfiguration(configSsl);
connect(manager2->get(*req), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
If changes req->setUrl(QUrl(videoDirectLink));
to req->setUrl(QUrl("https://media08.vbox7.com/s/91/91f4c651car96dafe736.mp4"));
or
QString n ="https://media08.vbox7.com/s/91/91f4c651car96dafe736.mp4";
req->setUrl(QUrl(n));
everything works
This is worked code for download without SSL. The principle is the same
globals.h
QString videoDirectLink = "";
mainwindow.cpp
#include "globals.h"
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
// start downloading
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(downloadFinished(QNetworkReply*)));
QString target = videoDirectLink;
QUrl url = QUrl::fromEncoded(target.toLocal8Bit());
connect(manager->get(QNetworkRequest(url)), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
I tried to add encoding, but that is not the problem.
other suggestions ?
// NOT WORKING
void MainWindow::readOutput() {
QString outData = myProcess->readAllStandardOutput(); // URL -> https://www.vbox7.com/play:91f4c651ca
videoDirectLink = outData; // videoDirectLink -> https://media28.vbox7.com/s/91/91f4c651car96dafe736.mp4
if (videoDirectLink.contains("https")) {
QNetworkAccessManager *manager2 = new QNetworkAccessManager(this);
connect(manager2, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
QNetworkRequest *req = new QNetworkRequest();
req->setUrl(QUrl(videoDirectLink)); //videoDirectLink
connect(manager2->get(*req), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
QMessageBox::information(this,"SSL downloading",videoDirectLink,"ok"); // displays https://media28.vbox7.com/s/91/91f4c651car96dafe736.mp4
}
}
studing sddm code from official git (https://github.com/sddm/sddm), I try to add this test code:
void UserModel::test() {
QString str1 = "Test";
qWarning("%s",str1);
}
but I have an error:
whithin this context.
what does it mean?
how should I do to intialise a new QString variable?
You're not posting the complete error, and you can't pass a QString directly to qWarning. To use the C format string, you need to convert it to the local encoding and pass a const char* to that, or better yet use the debug stream:
void UserModel::test() {
auto str1 = QStringLiteral("Test");
// preferred
qWarning() << str1;
// acceptable
qWarning("%s", str1.toLocal8Bit().constData());
}
I am trying to reconstruct an image from a file which is in Intel hex 386 format. After parsing the file all the data I am copying to a QByteArray and same array is used to create a QImage Object. But whatever image is which I got after reconstructing is not perfect. I am getting blue color instead of black, edges are not clear etc. The text file which I am parsing is a ram memory dump from STM32F4 controller (arm).The image is stored in RGB565 format.
Code to create the image:
{
QString strFilename;
Hex386Parser oFileParser;
strFilename = QFileDialog::getOpenFileName(this,"Select a file", QDir::homePath());
oFileParser.parseFile(strFilename, oByteArray);
QImage image(320, 240, QImage::Format_RGB16);
for (int y = 0; y < image.height(); y++)
{
memcpy(image.scanLine(y), oByteArray.constData() + y * image.bytesPerLine(),
image.bytesPerLine());
}
qDebug() <<"Size of the byte array is " <<oByteArray.size();
QLabel *label = new QLabel();
label->setPixmap(QPixmap::fromImage(image));
label->show();
}
Code to used to parse the file:
#define QT_NO_CAST_TO_ASCII
void Hex386Parser::parseFile(QString strFilename,QByteArray& ref_ByteArray)
{
QFile oFile(strFilename);
std::stringstream sstr;
QString strLength;
int unLength = 0, unAddress = 0,unDescriptor =0xFFFF,nIndex =0,nlineno=0;
if (oFile.open((QIODevice::ReadOnly | QIODevice::Text)))
{
QTextStream in(&oFile);
while (!in.atEnd())
{
QString line = in.readLine();
nIndex = 0;
nlineno++;
//unsigned char *pCharFrame = (unsigned char *)line.toStdString().c_str();
if (':' != line.at(nIndex))
{
// file corrupted
return;
}
nIndex++;
{
strLength = line.mid(nIndex, 2);
sstr << strLength.toStdString();
sstr << std::hex;
sstr >> unLength; // get length of the record
strLength.clear();
sstr.clear();
}
nIndex += 2;
unAddress = line.mid(nIndex,4).toInt(); // get address bytes
nIndex +=4;
unDescriptor = line.mid(nIndex, 2).toInt(); // get data descriptor
nIndex += 2;
switch(unDescriptor)
{
case data_record:
ref_ByteArray.append((line.mid(nIndex, unLength )));
// add data to bytearray
break;
case end_of_file_record:
break;
case extended_segment_address_record:
break;
case extended_linear_address_record:
break;
case start_linear_address_record:
break;
}
}
oFile.close();
}
}
What am I doing wrong??
The line contains hex string data representations where each byte is coded as two characters.
You want binary bytes. So, 2 * unLength symbols should be read from line. Then, that data string should converted to binary, for example:
{
case data_record:
QByteArray hex = line.mid(nIndex, 2 * unLength ).toLatin1();
QByteArray binary = QByteArray::fromHex(hex);
ref_ByteArray.append(binary);
...
}
I am working in Qt4.7 on MAC OSx. I want to insert files in QTreewidget using the Drag and Drop events. I want to add multiple files at a time. I am using this:
void MainWindow::dragEnterEvent(QDragEnterEvent * e)
{
if(e->mimeData()->hasUrls())
{
e->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent * e)
{
QTreeWidgetItem *Items = new QTreeWidgetItem(ui->treeWidget);
foreach(const QUrl &url,e->mimeData()->urls())
{
const QString &filename = url.toLocalFile();
qDebug() << "Dropped file:" << filename;
Items->setText(0,filename);
}
}
Using this, I am able to insert only one file at a time. Is there anyone who can help me out in this issue ? Your help will really appreciate.
Thanks,
Ashish.
The problem is that you create only one tree view item. However you need one per each Url you passed with the mime data:
void MainWindow::dropEvent(QDropEvent *e)
{
foreach(const QUrl &url, e->mimeData()->urls()) {
QString filename = url.toLocalFile();
qDebug() << "Dropped file:" << filename;
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, filename);
}
}