Linker Issues with QString? - qt

I'm having trouble compiling a qt project using msvc2012. For some reason, it says that there is an unresolved symbol that equates to a QString function, which I can't understand.
Here's my .pro file ( this program is commandline only, only using qt for convenience
QT += core gui widgets
TARGET = klc2abuspro
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
key.cpp \
klayout.cpp \
deadkey.cpp
HEADERS += \
key.h \
klayout.h \
deadkey.h
OTHER_FILES +=
and here is where the linker issue refers to:
{
QString file;
if(argc < 2)
{
qDebug() << "No file specified. Defaulting to russian.klc";
file = "russian.klc";
}
else
file = argv[1];
QFile input(file);
if(!input.open(QIODevice::ReadOnly | QIODevice::Text))
qDebug() << "File could not be opened";
QTextStream in(&input);
QString str;
KLayout * layout = new KLayout(file.split(".", QString::SkipEmptyParts)[0]);
while(!in.atEnd() && !(str = in.readLine()).contains("LAYOUT"));
in.readLine(); in.readLine(); in.readLine();in.readLine();
bool currChunk = true;
while(currChunk)
{
str = in.readLine();
if(str == "")
{
currChunk = false;
break;
}
QStringList parts = str.split("\t",QString::SkipEmptyParts );
layout->addKey(new Key(parts[0].toUInt(0,16),parts[3], parts[4]));
}
input.close();
//Starting deadkey finder
if(!input.open(QIODevice::ReadOnly | QIODevice::Text))
return -1;
QTextStream pass(&input);
while(!pass.atEnd() && !(str = pass.readLine()).contains("KEYNAME_DEAD"));
pass.readLine();
DeadKey * dkeys[10];
int i = 0;
while((str = pass.readLine()) != "") {
//qDebug() << "Line: " << str;
QStringList parts = str.split("\t",QString::SkipEmptyParts );
//1 is the name, 0 is the unicode value of the character
layout->addDeadKey(dkeys[i] = new DeadKey(parts[1], parts[0]));
i++;
}
//qDebug() << layout->printDeadKeys();
input.close();
// Find deadkey modifiers
bool cont = true;
i = 0;
input.open(QIODevice::ReadOnly | QIODevice::Text);
do {
while(!pass.atEnd() && !(str = pass.readLine()).contains("DEADKEY") && !(str.contains("KEYNAME")));
if(str.contains("KEYNAME")) {
cont = false;
break;
}
pass.readLine();
while((str = pass.readLine()) != "") {
//qDebug() << "Line: " << str;
QStringList parts = str.split("\t",QString::SkipEmptyParts );
//1 is the name, 0 is the unicode value of the character
//qDebug() << "Key to modify: " << parts[0] << "key result: " << parts[1];
dkeys[i]->addModifier(QChar(parts[0].toUInt(0,16)), QChar(parts[1].toUInt(0,16)));
}
i++;
}while(cont);
layout->implementModifiers();
//qDebug() <<layout->printMods();
layout->exportToQML(file.replace(".klc",".qml"));
return 0;
}
The linker error
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: class QString & __cdecl QString::operator=(class QString &&)" (__imp_??4QString##QEAAAEAV0#$$QEAV0##Z) referenced in function main

It seems that i also met this problem when I build the project with Qtcreator. but when i use the vs2012 to open the ".pro" project and build it. this error will disappear. Maybe you can try this method.

Related

QDataStream not working as expected

I am storing some data in QDataStream and immediately taking the data, but the count is showing zero while retriving. code looks fine but unexpected behaviour
//Overloading
QDataStream& operator<< (QDataStream& writeTO, const CascadeJobInfo& data)
{
writeTO << data.m_infoJobType << data.m_connectionName << data.m_submitJobId << data.m_submitJobStat;
return writeTO;
}
QDataStream& operator>> (QDataStream& readIn, CascadeJobInfo& data)
{
readIn >> data.m_infoJobType >> data.m_connectionName >> data.m_submitJobId >> data.m_submitJobStat;
return readIn;
}
void Fun()
{
// Code Starts here
projectFileName = /*Path to folder*/
QFile file(projectFileName);
file.open(QFile::ReadWrite);
file.close();
QDataStream dStream(&file);
int jobLstCount = /*Get the Count, assume 4*/
dStream << jobLstCount;
for(int i = 0; i < jobLstCount; i++)
{
JobInfo.m_infoJobType = jobFlowItem->getJobType();
JobInfo.m_connectionName = submitItem->connectionName();
JobInfo.m_submitJobId = submitItem->jobID();
JobInfo.m_submitJobStat = submitItem->jobState();
// All valid data stored here
}
file.close();
QDataStream dStreamOut(&file);
dStreamOut >> jobLstCount; /*Count returns zero here insted of 4*/
CascadeJobInfo jobInfo;
// Why jobLstCount is getting zero here
for(int i = 0 ; i < jobLstCount ; i++)
{
dStreamOut >> jobInfo;
}
}
file.open(QFile::ReadWrite);
file.close(); <--- HERE
QDataStream dStream(&file);
You are closing the file as soon as you open it, so basically you are working with an invalid file descriptor which won't work. Put file.close() at the end of the code when you are done.

How to clear QFuture results in QFutureWatcher

I tried to block multiple file at once, then copy them to another location.
Both source and destination files should be blocked simultaneously. Therefore I can't use static QFile::copy() function.
To hold and move files I use QSharedPointer< QFile > due to QFile is neither copyable nor moveable.
To perform whole the operation entirely I use QtConcurrent framework. Namely: QtConcurrent::mappedReduced and QFutureWatcher.
To open all pairs of files I use map functor, then to sequentially copy them I use reduce functor.
using PFile = QSharedPointer< QFile >;
using PFileList = QList< PFile >;
template< typename Result, typename Functor >
struct FunctorWithResultType
: std::decay_t< Functor >
{
using result_type = Result;
FunctorWithResultType(Functor & functor)
: std::decay_t< Functor >{std::forward< Functor >(functor)}
{ ; }
};
template< typename Result, typename Functor >
FunctorWithResultType< Result, Functor >
addResultType(Functor && functor)
{
return {functor};
}
class Test
{
public :
QDir currentDirectory;
QFutureWatcher< PFileList > fileCopyFutureWatcher;
// ...
};
// ...
Test::Test()
{
auto onFilesCopied = [&]
{
PFileList copiedFiles = fileCopyFutureWatcher.result();
qCInfo(usbDevice) << "Files copy operation from drive finished.";
qCInfo(usbDevice) << copiedFiles.size();
for (PFile const & file : copiedFiles) {
//file->close(); // I want copiedFiles to be closed automatically at the end of current scope
}
};
connect(&fileCopyFutureWatcher, &fileCopyFutureWatcher.finished, onFilesCopied);
}
// ...
void Test::onDeviceAdded(QString deviceName)
{
qCInfo(usbDevice) << "USB device" << deviceName << "is added.";
if (!fileCopyFutureWatcher.isFinished()) {
return;
}
QDir sourceDirectory{deviceName};
if (!sourceDirectory.cd("dir")) {
qCInfo(usbDevice) << "Drive" << deviceName << "does not contain dir subdirectory.";
return;
}
auto destinationDirectory = currentDirectory;
if (!destinationDirectory.mkpath("fileCache")) {
qCCritical(usbDevice) << "Can't create fileCache subdirectory in"
<< destinationDirectory.absolutePath()
<< "directory";
return;
}
if (!destinationDirectory.cd("fileCache")) {
qCCritical(usbDevice) << "Can't change directory to fileCache subdirectory in"
<< destinationDirectory.absolutePath()
<< "directory";
return;
}
struct PFilePair
{
PFile source, destination;
};
auto openSourceAndDestinationFiles = [&, destinationDirectory] (QFileInfo const & fileInfo) -> PFilePair
{
auto source = PFile::create(fileInfo.absoluteFilePath());
QFile & sourceFile = *source;
if (!sourceFile.open(QFile::ReadOnly)) {
qCCritical(usbDevice) << "Can't open file" << sourceFile.fileName()
<< "to read:" << sourceFile.errorString();
return {};
}
auto destination = PFile::create(destinationDirectory.absoluteFilePath(fileInfo.fileName()));
QFile & destinationFile = *destination;;
if (!destinationFile.open(QFile::ReadWrite | QFile::Truncate)) {
qCCritical(usbDevice) << "Can't open file" << destinationFile.fileName()
<< "to write:" << destinationFile.errorString();
return {};
}
return {qMove(source), qMove(destination)};
};
auto copyFiles = [&] (PFileList & files, PFilePair const & filePair)
{
if (filePair.source.isNull() || filePair.destination.isNull()) {
return;
}
QFile & sourceFile = *filePair.source;
QFile & destinationFile = *filePair.destination;
qCInfo(usbDevice) << sourceFile.fileName() << "->" << destinationFile.fileName();
constexpr int size = (1 << 20); // 1MiB
QByteArray buffer{size, 0};
char * const data = buffer.data();
while (!sourceFile.atEnd()) {
auto bytesRead = sourceFile.read(data, size);
if (bytesRead < 0) {
qCCritical(usbDevice) << "Can't read file" << sourceFile.fileName()
<< ":" << sourceFile.errorString();
return;
}
auto bytesWritten = destinationFile.write(data, bytesRead);
while (bytesWritten < bytesRead) {
auto sizeWritten = destinationFile.write(data + bytesWritten, bytesRead - bytesWritten);
if (sizeWritten < 0) {
qCCritical(usbDevice) << "Can't write file" << destinationFile.fileName()
<< ":" << destinationFile.errorString();
return;
}
bytesWritten += sizeWritten;
}
Q_ASSERT(bytesWritten == bytesRead);
}
Q_ASSERT(sourceFile.size() == destinationFile.size());
destinationFile.flush();
files.append(filePair.destination);
};
QStringList nameFilters;
nameFilters << "file.dat";
// many other entries
auto entryInfoList = sourceDirectory.entryInfoList(nameFilters, (QDir::Readable | QDir::Files));
fileCopyFutureWatcher.setFuture(QtConcurrent::mappedReduced< PFileList >(qMove(entryInfoList), addResultType< PFilePair >(openSourceAndDestinationFiles), copyFiles));
}
// ...
After reading result in QFutureWatcher::finished event all the destination files are still opened and blocked by my application. Therefore I can conclude that copies of QSharedPointer< QFile > are still exists. I suspect that they leave in QFuture inside QFutureWatcher. How can I clear all of them (i.e. how to cause QFile::~QFile() to close all files) without calling QFutureWatcher::setFuture with fake QFuture instance?
To achieve this I need to steal result from QFuture, not to copy.
My workaround looks like following:
using PFile = QSharedPointer< QFile >;
using PFileList = QList< PFile >;
template< typename Result, typename Functor >
struct FunctorWithResultType
: std::decay_t< Functor >
{
using result_type = Result;
FunctorWithResultType(Functor & functor)
: std::decay_t< Functor >{std::forward< Functor >(functor)}
{ ; }
};
template< typename Result, typename Functor >
FunctorWithResultType< Result, Functor >
addResultType(Functor && functor)
{
return {functor};
}
class Test
{
public :
QDir currentDirectory;
// ...
};
// ...
void Test::onDeviceAdded(QString deviceName)
{
qCInfo(usbDevice) << "USB device" << deviceName << "is added.";
QDir sourceDirectory{deviceName};
if (!sourceDirectory.cd("dir")) {
qCInfo(usbDevice) << "Drive" << deviceName << "does not contain dir subdirectory.";
return;
}
auto destinationDirectory = currentDirectory;
if (!destinationDirectory.mkpath("fileCache")) {
qCCritical(usbDevice) << "Can't create fileCache subdirectory in"
<< destinationDirectory.absolutePath()
<< "directory";
return;
}
if (!destinationDirectory.cd("fileCache")) {
qCCritical(usbDevice) << "Can't change directory to fileCache subdirectory in"
<< destinationDirectory.absolutePath()
<< "directory";
return;
}
struct PFilePair
{
PFile source, destination;
};
auto openSourceAndDestinationFiles = [&, destinationDirectory] (QFileInfo const & fileInfo) -> PFilePair
{
auto source = PFile::create(fileInfo.absoluteFilePath());
QFile & sourceFile = *source;
if (!sourceFile.open(QFile::ReadOnly)) {
qCCritical(usbDevice) << "Can't open file" << sourceFile.fileName()
<< "to read:" << sourceFile.errorString();
return {};
}
auto destination = PFile::create(destinationDirectory.absoluteFilePath(fileInfo.fileName()));
QFile & destinationFile = *destination;;
if (!destinationFile.open(QFile::ReadWrite | QFile::Truncate)) {
qCCritical(usbDevice) << "Can't open file" << destinationFile.fileName()
<< "to write:" << destinationFile.errorString();
return {};
}
return {qMove(source), qMove(destination)};
};
auto copyFiles = [&] (PFileList & files, PFilePair const & filePair)
{
if (filePair.source.isNull() || filePair.destination.isNull()) {
return;
}
QFile & sourceFile = *filePair.source;
QFile & destinationFile = *filePair.destination;
qCInfo(usbDevice) << sourceFile.fileName() << "->" << destinationFile.fileName();
constexpr int size = (1 << 20); // 1MiB
QByteArray buffer{size, 0};
char * const data = buffer.data();
while (!sourceFile.atEnd()) {
auto bytesRead = sourceFile.read(data, size);
if (bytesRead < 0) {
qCCritical(usbDevice) << "Can't read file" << sourceFile.fileName()
<< ":" << sourceFile.errorString();
return;
}
auto bytesWritten = destinationFile.write(data, bytesRead);
while (bytesWritten < bytesRead) {
auto sizeWritten = destinationFile.write(data + bytesWritten, bytesRead - bytesWritten);
if (sizeWritten < 0) {
qCCritical(usbDevice) << "Can't write file" << destinationFile.fileName()
<< ":" << destinationFile.errorString();
return;
}
bytesWritten += sizeWritten;
}
Q_ASSERT(bytesWritten == bytesRead);
}
Q_ASSERT(sourceFile.size() == destinationFile.size());
destinationFile.flush();
files.append(filePair.destination);
};
auto & fileCopyFutureWatcher = *new QFutureWatcher< PFileList >{this};
auto onFilesCopied = [&]
{
fileCopyFutureWatcher.deleteLater();
PFileList copiedFiles = fileCopyFutureWatcher.result();
qCInfo(usbDevice) << "Files copy operation from drive finished.";
qCInfo(usbDevice) << copiedFiles.size();
for (PFile const & file : copiedFiles) {
// ...
}
};
connect(&fileCopyFutureWatcher, &fileCopyFutureWatcher.finished, onFilesCopied);
QStringList nameFilters;
nameFilters << "*.dat";
auto entryInfoList = sourceDirectory.entryInfoList(nameFilters, (QDir::Readable | QDir::Files));
fileCopyFutureWatcher.setFuture(QtConcurrent::mappedReduced< PFileList >(qMove(entryInfoList), addResultType< PFilePair >(openSourceAndDestinationFiles), copyFiles));
}
// ...
I almost sure, that fileCopyFutureWatcher instance will be deleted reliably.

QFile,QFile::open: File access not specified, Opening file failed

Simple problem, I just can't open/create a file. It's supposed to save some settings in an xml-file to a given path.
I call the method like this:
xmlwriter->write_settings("./settings.xml");
int XmlWriter::write_settings(QString path)
{
qDebug() << "Path is: " + path;
QDomDocument document;
QDomElement root = document.createElement("settings");
document.appendChild(root);
QDomElement node;
node.setAttribute("name", "Its me!");
node.setAttribute("series", "25");
node.setAttribute("PMT", "200");
root.appendChild(node);
QFile file(path);
if(!file.open(QIODevice::ReadWrite, QIODevice::Text))
{
qDebug() << "Opening file failed!";
return 1;
}
else
{
QTextStream stream(&file);
stream << document.toString();
file.close();
qDebug() << "wrote file to " + path;
return 0;
}
}
You don't pass parameters correctly, so you probably invoke a polymorphic version of QFile::open
Try this:
QFile file(path);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug() << "Opening file failed!";
return 1;
}
else
{
QTextStream stream(&file);
stream << document.toString();
file.close();
qDebug() << "wrote file to " + path;
return 0;
}

Qt: QGLShaderProgram turn off log messages

Is there a way to turn of log messages when calling QGLShaderProgram::link()?
http://qt-project.org/doc/qt-4.8/qglshaderprogram.html#link
Messages look like:
QGLShader::link: "Vertex shader(s) linked, fragment shader(s) linked.
"
Qt code looks like this:
src/opengl/qglshaderprogram.cpp:893
glLinkProgram(program);
value = 0;
glGetProgramiv(program, GL_LINK_STATUS, &value);
d->linked = (value != 0);
value = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
d->log = QString();
if (value > 1) {
char *logbuf = new char [value];
GLint len;
glGetProgramInfoLog(program, value, &len, logbuf);
d->log = QString::fromLatin1(logbuf);
QString name = objectName();
if (name.isEmpty())
qWarning() << "QGLShader::link:" << d->log;
else
qWarning() << "QGLShader::link[" << name << "]:" << d->log;
delete [] logbuf;
}
return d->linked;
}
So it seems the only possible solution is to redirect qWarning() as done in: How to redirect qDebug, qWarning, qCritical etc output?
qInstallMsgHandler([](QtMsgType , const char* ) { }); // empty message handler
bool result = program.link();
qInstallMsgHandler(0); // restore default message handling

File copy operation does not work in a separate thread?

I am trying to run code of copying files in other thread so that it may not freeze the GUI of the application.
I found that it does not seem to work in a separate thread.
Why is it not working ?
void CopyOperation::run()
{
CopyFilesToFolder(list,sFolder);
}
bool CopyOperation::CopyFilesToFolder(const QStringList &oFileList,const QString
&sTargetFolder)
{
if(sTargetFolder.isEmpty())
{
status = false;
return false;
}
QDir dir(sTargetFolder);
if(!dir.exists()) dir.mkdir(sTargetFolder);
QString sOldDirPath = dir.currentPath();
//if(!dir.setCurrent(sTargetFolder)) return false;
QFile file;
status = true;
foreach(QString sFileName,oFileList)
{
file.setFileName(sFileName);
QFileInfo info(sFileName);
QString newfile = sTargetFolder + "/" + info.fileName();
qDebug() << "\n name = " << newfile;
if(!QFile::copy(sFileName,newfile))
//if(!file.copy(newfile))
{
qDebug() << "\n File copy failed .. " + file.fileName() + " Error : " + file.errorString();
status = false;
break;
}
}
qDebug() << "\n Result .. " << file.errorString() << "code " << file.error();
//dir.setCurrent(sOldDirPath);
return status;
}
Since you didn't post code, I just can try to guess what is the problem. Qt has a static function:
bool copy ( const QString & fileName, const QString & newName )
There is also a copy which is not static:
bool copy ( const QString & newName )
Both of them will fail if file defined by newName already exists, ie. existing file will not be overwritten. Also, maybe path doesn't exists. Without some portion of code is difficult to guess what is the problem.

Resources