I need to pass a filename as QString with path to a function covered in a QFuture and watched by a QFutureWatcher, but only the first character of this QString will be passed to this function. Here the declaration in the *.h file:
qint64 processDataFile(const QString &fileName);
...
QFutureWatcher<qint64> *watcher;
And here the call of the function:
watcher = new QFutureWatcher<qint64>;
connect(watcher, &QFutureWatcher<qint64>::finished, this, &SmartDCPD::fileProcessingFinished);
qDebug() << log.fileName; // something like C:/users/.....
watcher->setFuture(QtConcurrent::mapped(log.fileName, std::bind(&SmartDCPD::processDataFile, this, std::placeholders::_1)));
And the code of the function:
qint64 SmartDCPD::processDataFile(const QString &fileName)
{
qDebug() << fileName; //Only C (just the first char of the whole QString)
QElapsedTimer t;
t.start();
rapidcsv::Document doc(fileName.toUtf8().constData(), rapidcsv::LabelParams(-1, -1), rapidcsv::SeparatorParams(';'));
QString stamp = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss.zzz");
doc.SetCell<std::string>(1, 2, stamp.toUtf8().constData());
doc.Save();
return t.nsecsElapsed() / 1000000;
}
What am I doing wrong?
Related
I'm reading a file on Qt 5.12 using QFile. I try to read a file from my computer but when I using the directory which read from FileDialog has "file:///" prefix. Can anyone tell me why is it wrong and how to using URL which gets form FileDialog, please?
Thanks!
QFile file("C:/Users/HuuChinhPC/Desktop/my_txt.txt"); // this work
//QFile file("file:///C:/Users/HuuChinhPC/Desktop/my_txt.txt"); //didn't work
QString fileContent;
if (file.open(QIODevice::ReadOnly) ) {
QString line;
QTextStream t( &file );
do {
line = t.readLine();
fileContent += line;
} while (!line.isNull());
file.close();
} else {
emit error("Unable to open the file");
return QString();
}
FileDialog returns a url since in QML that type of data is used, but QFile not so you must convert the QUrl to a used string toLocalFile():
Q_INVOKABLE QString readFile(const QUrl & url){
if(!url.isLocalFile()){
Q_EMIT error("It is not a local file");
return {};
}
QFile file(url.toLocalFile());
QString fileContent;
if (file.open(QIODevice::ReadOnly) ) {
QString line;
QTextStream t( &file );
do {
line = t.readLine();
fileContent += line;
} while (!line.isNull());
file.close();
return fileContent;
} else {
Q_EMIT error("Unable to open the file");
return {};
}
}
*.qml
var text = helper.readFile(fileDialog.fileUrl)
console.log(text)
You have to strip the file prefix to use the URL which gets form FileDialog:
QFile file("file:///C:/Users/HuuChinhPC/Desktop/my_txt.txt")
if (Qt.platform.os === "windows") {
return file.replace(/^(file:\/{3})|(file:)|(qrc:\/{3})|(http:\/{3})/,"")
}
else {
return file.replace(/^(file:\/{2})|(qrc:\/{2})|(http:\/{2})/,"");
}
I am using qDebug(), qInfo() and so on in combination of qInstallMessageHandler to write my logfiles. I also get an output on my batch screen, when executing my application.
I only found QT_NO_DEBUG_OUTPUT, but I want to toggle this configuration while run-time. Is there a way to prevent Qt from writing to std output?
sadly no, you only get access to the messages, but cannot prevent from beeing written to std output.
That's false in Qt 5 at the very least. The message printing code looks as follows: you can clearly see that only the message handler is being used:
if (grabMessageHandler()) {
// prefer new message handler over the old one
if (msgHandler.load() == qDefaultMsgHandler
|| messageHandler.load() != qDefaultMessageHandler) {
(*messageHandler.load())(msgType, context, message);
} else {
(*msgHandler.load())(msgType, message.toLocal8Bit().constData());
}
ungrabMessageHandler();
} else {
fprintf(stderr, "%s\n", message.toLocal8Bit().constData());
}
As you can see, the fprintf(stderr, ...) is a fallback only if recursion is detected from within the messageHandler itself.
Thus, all you need to prevent any debug output is to implement and set your own messageHandler.
To completely turn off all the debug output in Qt, the following works:
#include <QtCore>
int main() {
qDebug() << "I'm not quiet at all yet";
qInstallMessageHandler(+[](QtMsgType, const QMessageLogContext &, const QString &){});
qDebug() << "I'm very, very quiet";
}
In any case, a sensible implementation of an application-wide file logger might look as follows - it doesn't recreate the QTextStream unnecessarily; it uses QString::toUtf8() instead, and explicitly writes line endings.
#include <QtCore>
class Logger {
static struct Data {
Logger *instance;
QtMessageHandler chainedHandler;
} d;
bool m_isOpen;
QFile m_logFile;
QtMessageHandler m_oldHandler = {};
static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
if (d.instance)
d.instance->log(msg);
if (d.chainedHandler)
d.chainedHandler(type, context, msg);
}
public:
enum ChainMode { DontChain, Chain };
Logger() {
Q_ASSERT(!instance());
m_logFile.setFileName("myLog.txt");
m_isOpen = m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
d.instance = this;
}
~Logger() { uninstallHandler(); }
bool isOpen() const { return m_isOpen; }
void installHandler(ChainMode mode) {
Q_ASSERT(!m_oldHandler);
m_oldHandler = qInstallMessageHandler(handler);
if (mode == Chain)
d.chainedHandler = m_oldHandler;
}
void uninstallHandler() {
if (m_oldHandler) {
m_oldHandler = nullptr;
d.chainedHandler = nullptr;
qInstallMessageHandler(m_oldHandler);
}
}
/// This method is *not* thread-safe. Use with a thread-safe wrapper such as `qDebug`.
void log(const QString & msg) {
if (isOpen()) {
m_logFile.write(msg.toUtf8());
m_logFile.write("\n", 1);
}
}
/// Closes the log file early - this is mostly used for testing.
void endLog() {
uninstallHandler();
m_logFile.close();
}
static Logger *instance() { return d.instance; }
};
Logger::Data Logger::d;
template <typename T> QByteArray streamOutputFor(const T &data) {
QBuffer buf;
buf.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream s(&buf);
s << data << endl;
buf.close();
return buf.data();
}
QByteArray readEnd(const QString &fileName, int count) {
QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text) && file.size() >= count) {
file.seek(file.size() - count);
return file.readAll();
}
return {};
}
void test() {
auto const entry = QDateTime::currentDateTime().toString().toUtf8();
Q_ASSERT(Logger::instance()->isOpen());
qDebug() << entry.data();
Logger::instance()->endLog();
auto reference = streamOutputFor(entry.data());
auto readback = readEnd("myLog.txt", reference.size());
Q_ASSERT(!reference.isEmpty());
Q_ASSERT(readback == reference);
}
int main() {
Logger logger;
logger.installHandler(Logger::DontChain);
qDebug() << "Something or else";
test();
}
Qt 5.7
According to the documentation, QAudioDecoder does not support streaming media. But is accepts either a filename or a QIODevice as source. Came the "bright idea": let's subclass QIODevice to create streaming media support.
But I always get an error:
Unable to start decoding process. Stream contains no data.
I have checked and double checked and debugged. Can't figure out what can be the problem.
I'm only testing it, so the code below is by no means final.
header:
class InputBuffer : public QIODevice
{
public:
InputBuffer(QString fileName);
~InputBuffer();
qint64 readData(char *data, qint64 maxlen);
qint64 writeData(const char *data, qint64 len);
bool isSequential();
qint64 bytesAvailable();
qint64 size();
private:
char* bufferRef;
qint64 length;
qint64 position;
};
implementation:
#include "inputbuffer.h"
InputBuffer::InputBuffer(QString fileName)
{
QFileInfo fileInfo(fileName);
length = fileInfo.size();
position = 0;
bufferRef = (char*)malloc(length);
QFile file(fileName);
file.open(QFile::ReadOnly);
file.read(bufferRef, length);
file.close();
emit readyRead();
}
InputBuffer::~InputBuffer()
{
free(bufferRef);
}
qint64 InputBuffer::readData(char *data, qint64 maxlen)
{
if (position >= length) {
return -1;
}
qint64 readSize = qMin(maxlen, length - position);
memcpy(data, bufferRef + position, readSize);
position += readSize;
return readSize;
}
qint64 InputBuffer::writeData(const char *data, qint64 len)
{
return -1;
}
bool InputBuffer::isSequential()
{
return true;
}
qint64 InputBuffer::bytesAvailable()
{
return length - position;
}
qint64 InputBuffer::size()
{
return length;
}
usage:
inputBufferRef = new InputBuffer("/home/pp/Zenék/Test.mp3");
inputBufferRef->open(QIODevice::ReadOnly);
audioDecoderRef = new QAudioDecoder();
audioDecoderRef->setAudioFormat(audioFormat);
audioDecoderRef->setSourceDevice(inputBufferRef);
connect(audioDecoderRef, SIGNAL(bufferReady()), this, SLOT(decoderBufferReady()));
connect(audioDecoderRef, SIGNAL(finished()), this, SLOT(decoderFinished()));
connect(audioDecoderRef, SIGNAL(error(QAudioDecoder::Error)), this, SLOT(decoderError(QAudioDecoder::Error)));
audioDecoderRef->start();
None of your isSequential(), bytesAvailable(), nor size() methods override the respective method in QIODevice, because the signatures don't match. They are all missing a const qualifier. Using the C++11 override keyword when overriding virtual methods helps.
I implemented reading bytes at a serial communication in a Qt project(5.5) in Visual Studio 2013.
My code is here:
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//QCoreApplication coreApplication(argc, argv);
int argumentCount = QCoreApplication::arguments().size();
QStringList argumentList = QCoreApplication::arguments();
QTextStream standardOutput(stdout);
if (argumentCount == 1) {
standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl;
return 1;
}
QSerialPort serialPort;
QString serialPortName = argumentList.at(1);
serialPort.setPortName(serialPortName);
int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
serialPort.setBaudRate(serialPortBaudRate);
if (!serialPort.open(QIODevice::ReadOnly)) {
standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
return 1;
}
Camera cam2;
//cam2.showCamera();
cam2.showCamera();
cam2.showFullScreen();
QByteArray abc;
SerialPortReader serialPortReader(&serialPort,abc);
//return coreApplication.exec();
return app.exec();
};
serialportreader.cpp
#include "serialportreader.h"
#include <QCoreApplication>
QT_USE_NAMESPACE
SerialPortReader::SerialPortReader(QSerialPort *serialPort, QByteArray abc, QObject *parent)
: QObject(parent)
, m_serialPort(serialPort)
, m_standardOutput(stdout)
{
connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead);
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
if (!m_readData.isEmpty()) {
m_standardOutput << m_readData << endl;
m_readData.clear();
}
}
This code works successfully. I would like to set the contents of m_readData array to abc array so that I can evaluate the message in main scope. In order to implement this, I gave abc array to handleReadyRead function as a parameter. However, it gives an error.The code is here:
SerialPortReader::SerialPortReader(QSerialPort *serialPort, QByteArray abc, QObject *parent)
: QObject(parent)
, m_serialPort(serialPort)
, m_standardOutput(stdout)
{
connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead(abc));
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::handleReadyRead(QByteArray abc)
{
m_readData.append(m_serialPort->readAll());
if (!m_readData.isEmpty()) {
m_standardOutput << m_readData << endl;
m_readData.clear();
}
}
Error:
serialportreader.cpp(45): error C2102: '&' requires l-value
line45:
connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead(abc));
You can't pass the local variable to the slot this way, but what you are trying to do doesn't even require it. You could just store abc to a member variable in the constructor, and use it in the slot. BUT: you have to change it to a reference, otherwise the call to the constructor copies the array, so the original one in your main function is unaffected.
You could also just implement this array as a member variable of your SerialReader, and provide a getter function, so that the main function (or whoever is the consumer of the data) can just pull this data from the reader instance.
I am attempting to build my own custom QAbstractNetworkCache implementation for use with QNetworkAccessManager.
I am having trouble with QAbstractNetworkCache::insert(QIODevice *device); inside this method, the device always arrives with 0 bytes to read from.
As I understand, the QIODevice* that is returned from QAbstractNetworkCache::prepare(const QNetworkCacheMetaData &metaData) is going to be filled with data and used as a parameter to QAbstractNetworkCache::insert(QIODevice *device) method once QNetworkAccessManager finishes downloading.
So I have prepared a QBuffer to be this container, but whenever QAbstractNetworkCache::insert(QIODevice *device) is getting called, it always arrives with nothing in it (device->bytesAvailable() == 0)
QIODevice* NetworkCachePrivate::prepare(const QNetworkCacheMetaData &metaData) {
if (!metaData.isValid() || !metaData.url().isValid() || cacheDir.isEmpty()) return 0;
QIODevice* device = 0;
QString hash = hexMD5(metaData.url().toString());
QScopedPointer<QBuffer> buffer(new QBuffer);
if (buffer->open(QIODevice::ReadWrite))
{
qDebug() << "BUFFER READY";
device = buffer.take();
deviceMapping[device] = qMakePair(hash, metaData);
}
return device;
}
void NetworkCachePrivate::insert(QIODevice *device) {
if (deviceMapping.contains(device))
{
QPair<QString, QNetworkCacheMetaData> pair = deviceMapping[device];
QString fileName;
fileName += cacheDir;
fileName += QLatin1String("/");
fileName += pair.first;
qDebug() << "DEVICE BYTES" << device->bytesAvailable(); //ALWAYS 0!!!! :(
QFile file(fileName);
if (file.open(QIODevice::WriteOnly))
{
qint64 size = file.write(device->readAll());
if (size <= 0)
{
file.remove();
}
else
{
qDebug() << "FILE WROTE " << size;
cacheSize += size;
}
}
deviceMapping.remove(device);
delete device;
}
}
QNetworkCacheMetaData NetworkCachePrivate::metaData (const QUrl &url ) {
QString fileName;
fileName += cacheDir;
fileName += QLatin1String("/");
fileName += hexMD5(url.toString());
QNetworkCacheMetaData data;
if (!QFile::exists(fileName))
return data;
data.setUrl(url);
data.setExpirationDate(QDateTime::currentDateTime().addYears(1));
return data;
}
As peppe also pointed out in the comment, you need to seek the QBuffer to the beginning after the write and before the read as per the documentation:
QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. Example:
QBuffer buffer;
char ch;
buffer.open(QBuffer::ReadWrite);
buffer.write("Qt rocks!");
*** buffer.seek(0); ***
buffer.getChar(&ch); // ch == 'Q'
buffer.getChar(&ch); // ch == 't'
buffer.getChar(&ch); // ch == ' '
buffer.getChar(&ch); // ch == 'r'
To be more concrete for your code, you would need to insert the statement like this:
...
device.seek(0);
qDebug() << "DEVICE BYTES" << device->bytesAvailable(); //NOT 0 ANYMORE!!!! :)
...