Qt C++:Signal and Slot Argument are not compatible - c++

I don't know what the issue with this code, the syntax I have used for connecting the signals and sort looks correct but for some reason I keep getting this error
error: static assertion failed: Signal and slot arguments are not compatible.
# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
^
this is the code for the header file
include <QString>
#include <QStringList>
#include <QTextStream>
class filereader : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString filename READ getFileName WRITE setFileName NOTIFY fileNameChanged)
Q_PROPERTY(QString output READ getOutput)
Q_PROPERTY(QString error READ getErrorString)
explicit filereader(QObject *parent = 0);
QString getFileName();
QString getOutput();
QString getErrorString();
void setFileName(const QString &fileName);
Q_INVOKABLE void readFile(); // this allows us to use this is as a function so we could do something like onClicked{ <text_id>.text = <file_reader_id>.readFile() }
signals:
void fileNameChanged();
void outputChanged(QString &Output);
void gotError(QString &&err);
void errorSignal();
protected slots:
void handleOut(QString &output);
void handleError(QString &err);
private:
QString fileName;
QString output;
QString error;
};
#endif // FILEREADER_H
this is the code for the source file
#include "filereader.h"
filereader::filereader(QObject *parent):
QObject(parent)
{
connect(this,&filereader::gotError,this,&filereader::handleError);
connect(this,&filereader::outputChanged,this,&filereader::handleOut);
}
QString filereader::getFileName()
{
return this->fileName;
}
QString filereader::getOutput()
{
return this->output;
}
QString filereader::getErrorString()
{
return this->error;
}
void filereader::setFileName(const QString &fileName)
{
if(this->fileName == fileName)
return;
this->fileName = fileName;
emit fileNameChanged();
}
void filereader::readFile()
{
QFile file(this->fileName);
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readAll();
outputChanged(line);
qDebug() << line;
}
file.close();
file.flush(); // this flushes the file stream
}else{
gotError("Could not open the file!");
}
}
void filereader::handleOut(QString &output)
{
if(this->output == output)
return;
this->output = output;
}
void filereader::handleError(QString &err)
{
if(this->error == err)
return;
this->error = err;
emit errorSignal();
}
I'm new to Qt and Qml so please help me understand the issue. Thanks :)

Related

Qt signals & slots, Slot is not called on signal emit

I've been having this problem for few weeks, still didn't found a solution anywhere. I did everything I could to find the problem, everything looks good but for some reason it doesn't work.
I tested the connection, between the loginFailed() signal, and handleLoginFailed() slot, it returned True, printed the object info, it shows the connections. Tried different Qt::connectionTypes, nothing works.
The two objects are defined bellow.
LoginService.h
#ifndef LOGINSERVICE_H
#define LOGINSERVICE_H
#include <QObject>
#include <QVariant>
#include "libs/qubeapi/QubeAPI.h"
#include "libs/qubeapi/LoginCommand.h"
#include "libs/qubeapi/APIBridge.h"
#include "libs/mge-users/User.h"
#include "libs/mge-users/permissionfactory.h"
#include "Utils/serverstate.h"
#include "Utils/singleton.h"
#include "Utils/baseservice.h"
class LoginService : public QObject
{
Q_OBJECT
QubeAPI* api;
APIBridge* bridge;
LoginCommand* loginCmd;
QString m_userType;
QString m_firstName;
QString m_lastName;
User *m_user;
public:
explicit LoginService(QObject *parent = nullptr);
Q_PROPERTY(QString userType READ userType WRITE setUserType NOTIFY userTypeChanged)
Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY firstNameChanged)
Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY lastNameChanged)
Q_PROPERTY(User* user READ user WRITE setUser NOTIFY userChanged)
const QString &userType() const;
void setUserType(const QString &newUserType);
const QString &firstName() const;
void setFirstName(const QString &newFirstName);
const QString &lastName() const;
void setLastName(const QString &newLastName);
User *user() const;
void setUser(User *newUser);
signals:
void success(QString token);
void loginFailed();
void userTypeChanged();
void firstNameChanged();
void lastNameChanged();
void staffLogout();
void userTypeRecived();
void userChanged();
public slots:
void requestLogin(QString userName, QString password);
void loginResponse(QString jsonData);
void doClose();
void prepareGetUserType();
void loadUserType(QString jsonData);
};
#endif // LOGINCONTROLLER_H
The function that emits the signal:
void LoginService::loginResponse(QString jsonData)
{
QSignalSpy spy(this,SIGNAL(loginFailed()));
if(jsonData == "") {
emit Singleton<ServerState>::GetInstance().offlineServer();
return;
}
QJsonDocument doc = QJsonDocument::fromJson(jsonData.toUtf8());
QString token = doc["data"]["tokenAuth"]["token"].toString();
if(!token.isEmpty()) {
UserSession::getInstance()->setAuthToken(token);
emit success(token);
prepareGetUserType();
} else {
qDebug() << "Urmeaza eroare";
QString errorMsg = doc["errors"][0]["message"].toString();
emit loginFailed();
qDebug() << spy.count();
}
}
The controller class that has the slot:
#ifndef LOGINCONTROLLER_H
#define LOGINCONTROLLER_H
#include <Utils/basecontroller.h>
#include <Utils/enginefactory.h>
#include <Utils/singleton.h>
#include <QQuickWindow>
#include <QQuickItem>
#include <QThread>
#include <QProperty>
#include <QQmlProperty>
#include "loginservice.h"
#include "draggableviewhelper.h"
#include <QMouseEvent>
class LoginController : public BaseController
{
Q_OBJECT
private:
QQuickWindow* window;
QQuickItem* login;
QQuickItem* userTextField;
QQuickItem* passwordTextField;
DraggableViewHelper* draggableViewHelper;
QQuickItem* mouseArea;
LoginService* service;
QPointF lastMousePosition;
public:
explicit LoginController(QObject *parent = nullptr);
// BaseController interface
void initialize();
// void setService(BaseService* s);
void setService(LoginService* s);
void setLastMousePosition(QPointF lastMousePosition);
public slots:
void handleLoginResponseSuccess(QString loginResponse);
void handleLoginResponseFailed();
void handlePositionChanged();
void handleLogout();
};
#endif // LOGINCONTROLLER_H
The initializer methods
void LoginController::initialize()
{
this->engine = Singleton<EngineFactory*>::GetInstance()->createEngine("qrc:/qml/LoginWindow.qml");
this->window = qobject_cast<QQuickWindow*>(this->engine->rootObjects().first());
// Create a draggable view helper, use window as input
// Get the view from the engine
this->draggableViewHelper = new DraggableViewHelper(this->window);
this->window->setFlags(Qt::Window | Qt::FramelessWindowHint);
this->login = window->findChild<QQuickItem*>("login");
this->userTextField = QQmlProperty::read(this->login,"user").value<QQuickItem*>();
this->passwordTextField = QQmlProperty::read(this->login,"password").value<QQuickItem*>();
// Set the mouseArea with the mainMouseArea item of the window
this->mouseArea = window->findChild<QQuickItem*>("mainMouseArea");
// Connect onClicked from mouse area, to handleMove of this
QObject::connect(this->mouseArea,SIGNAL(positionMoved()),this,SLOT(handlePositionChanged()));
}
void LoginController::setService(LoginService *s)
{
this->service = s;//new LoginService;
connect(this->service,SIGNAL(loginFailed()),this,SLOT(handleLoginResponseFailed()));
connect(this->service,SIGNAL(success(QString)),this,SLOT(handleLoginResponseSuccess(QString)));
connect(login,SIGNAL(doLogin(QString,QString)),this->service,SLOT(requestLogin(QString,QString)));
this->dumpObjectInfo();
this->service->dumpObjectInfo();
}
The slot
void LoginController::handleLoginResponseFailed()
{
qDebug() << "Failed";
QQmlProperty::write(this->userTextField,"state",QVariant::fromValue(QString{"default-error"}));
QQmlProperty::write(this->passwordTextField,"state",QVariant::fromValue(QString{"password-error"}));
}
This is the main.cpp file, where the objects are created
QApplication app1(argc, argv);
LoginController* loginCtrl = new LoginController;
LoginService* loginService = new LoginService;
QObject::connect(loginService, &LoginService::userTypeRecived,[&](){
qDebug() << "Failed to login";
});
qDebug() << loginService;
loginCtrl->initialize();
loginCtrl->setService(loginService);
//This will trigger the signal
// loginService->loginResponse("{\"errors\":[{\"message\":\"Please enter valid credentials\",\"locations\":[{\"line\":1,\"column\":12}],\"path\":[\"tokenAuth\"]}],\"data\":{\"tokenAuth\":null}}");
return app1.exec();

Segmentation Fault when accessing private class variable

The problem I'm having has been discussed multiple times, but I still don't understand how to fix the issue I'm having. I used to program in C++ a lot but switched over to Java for a while due to work. I'm getting back into C++ and I'm now having a lot of issues with memory management/pointers/references etc. This problem stems from that.
For context, these are files for a Qt 5.14 project that I'm working on.
The problem is that I have a private class variable named loggerLevel and the method that creates the segmentation fault error is getLevel which just returns the value of the loggerLevel. The that is supposed to be stored in the variable is an enum named Level that is defined in the header of the class.
I don't know if the problem stems from my lack of knowledge or if I am misunderstanding something about how classes work in C++, or if its something completely different. In any case, if anyone can help me out that would be great :)
--- Source code below ---
logger.cpp
#include "logger.h"
QString debugHTML = "<font color=\"gray\">";
QString infoHTML = "<font color=\"black\">";
QString warningHTML = "<font color=\"yellow\">";
QString errorHTML = "<font color=\"orange\">";
QString criticalHTML = "<font color=\"red\">";
QString endHTML = "</font><br>";
Logger::Logger(QObject *parent,
QString fileName,
QTextEdit *editor) : QObject(parent)
{
m_editor = editor;
m_showDate = true;
loggerLevel = Level::INFO;
if (!fileName.isEmpty()) {
file = new QFile;
file->setFileName(fileName);
file->open(QIODevice::Append | QIODevice::Text);
}
}
void Logger::write(const QString &value) {
QString text = value;
if (m_showDate) {
text = QDateTime::currentDateTime()
/*.toString("dd.MM.yyyy hh:mm:ss ") + text;*/
.toString("hh:mm:ss: ") + text;
}
QTextStream out(file);
out.setCodec("UTF-8");
if (file != 0) {
out << text;
} else {
//TODO: add QMessageBox here with error
}
//Adds HTML color/formatting
switch(loggerLevel)
{
case DEBUG: text = debugHTML + text; break;
case INFO: text = infoHTML + text; break;
case WARNING: text = warningHTML + text; break;
case ERROR: text = errorHTML + text; break;
case CRITICAL: text = criticalHTML + text; break;
default: text = infoHTML + text; break;
}
text = text + endHTML;
if (m_editor != 0) {
m_editor->insertHtml(text);
} else {
//TODO: add QMessageBox here with error
}
}
void Logger::write(const Level &level, const QString &value) {
Level prevLoggerLevel = Logger::getLevel();
Logger::setLevel(level);
write(value);
Logger::setLevel(prevLoggerLevel);
}
//--------Setters--------
void Logger::setLevel(const Level &level) {
loggerLevel = level;
}
void Logger::setShowDateTime(bool value) {
m_showDate = value;
}
//--------Getters--------
Logger::Level Logger::getLevel() {
return loggerLevel;
}
Logger::~Logger() {
if (file != 0) {
file->close();
}
}
logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include <QObject>
#include <QPlainTextEdit>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
class Logger : public QObject
{
Q_OBJECT
public:
explicit Logger(QObject *parent,
QString fileName = 0,
QTextEdit *editor = 0);
~Logger();
void setShowDateTime(bool value);
enum Level
{
DEBUG,
INFO,
WARNING,
ERROR,
CRITICAL
};
private:
QFile *file;
QTextEdit *m_editor;
bool m_showDate;
Level loggerLevel;
signals:
public slots:
void write(const QString &value);
void write(const Level &level, const QString &value);
void setLevel(const Level &level);
Level getLevel();
};
#endif // LOGGER_H
MainWindow.cpp
#include <string>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingsdialog.h"
#include "logger.h"
Logger *logger;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString logFileName = "log.txt";
Logger *logger = new Logger(this, logFileName, ui->loggerOutput);
logger->write(Logger::Level::INFO, "Logger Initilized!");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_toolButton_clicked()
{
SettingsDialog settingsDialog;
settingsDialog.setModal(true);
settingsDialog.exec();
}
//Left Side of controller
void MainWindow::on_s_leftJoystickX_Throttle_sliderMoved(int position)
{ ui->l_leftJoystickX->setText(QStringLiteral("X-Axis: %1%").arg(position)); }
void MainWindow::on_s_leftJoystickY_Throttle_sliderMoved(int position)
{ ui->l_leftJoystickY->setText(QStringLiteral("Y-Axis: %1%").arg(position)); }
void MainWindow::on_s_leftTrigger_Throttle_sliderMoved(int position)
{ ui->l_leftTrigger->setText(QStringLiteral("Trigger: %1%").arg(position)); }
void MainWindow::on_s_joystickThrottle_sliderMoved(int position)
{ ui->l_joystickThrottle->setText(QStringLiteral("Throttle: %1%").arg(position)); }
//Right Side of controller
void MainWindow::on_s_rightJoystickX_Throttle_sliderMoved(int position)
{ ui->l_rightJoystickX->setText(QStringLiteral("X-Axis: %1%").arg(position)); }
void MainWindow::on_s_rightJoystickY_Throttle_sliderMoved(int position)
{ ui->l_rightJoystickY->setText(QStringLiteral("Y-Axis: %1%").arg(position)); }
void MainWindow::on_s_rightTrigger_Throttle_sliderMoved(int position)
{ ui->l_rightTrigger->setText(QStringLiteral("Trigger: %1%").arg(position)); }
void MainWindow::on_s_keyboardThrottle_sliderMoved(int position)
{ ui->l_keyboardThrottle->setText(QStringLiteral("Throttle: %1%").arg(position)); }
void MainWindow::on_b_keyboardAction_1_clicked()
{
logger->write(Logger::Level::CRITICAL, "test");
}
There are more files, but I believe they are not relevant to the problem, if needed I can post the other files aswell.
-----Solution!-----
#churill Solution explains what needs to be done to fix this problem but I also will detail it below to complete this question.
Since I had already made a forward declaration of logger
Logger *logger
at the top of MainWindow.cpp, creating a new Logger object
Logger *logger = new Logger(this, logFileName, ui->loggerOutput);
is unnecessary so calling the forward declarations variable name instead of creating a new Logger object
logger = new Logger(this, logFileName, ui->loggerOutput);
fixes the problem!
From first glance:
Logger *logger = new Logger(this, logFileName, ui->loggerOutput);
creates an object that has nothing to do with the global variable logger which is never initialized. Maybe you meant to write only
logger = new Logger(this, logFileName, ui->loggerOutput);
to initialize this global variable.
To somewhat address the title of your question: Yes you actually can call functions on invalid pointers, but then the this-pointer is not valid, thus accessing a member variable causes the seg-fault.

QObject: Cannot create children

I want to communicate via the serial port in Qt (Qt 5.4.0, Ubuntu 14.04.3), and for decreasing the workload I want to shift the communication part into a second thread. Thus I created the following files:
serial_controller.cpp:
#include "serial_controller.h"
serial_controller_worker::serial_controller_worker(const QString &portname, int waitTimeout, int BaudRate)
{
this->portName = portname;
this->waitTimeout = waitTimeout;
this->baudrate = BaudRate;
this->serial.setPortName(this->portName);
this->serial.setBaudRate(this->baudrate);
if (!serial.open(QIODevice::ReadWrite))
{
emit error(tr("Can't open %1, error code %2").arg(portName).arg(serial.error()));
qDebug() << tr("Can't open %1, error code %2").arg(portName).arg(serial.error());
return;
}
else
{
emit error(tr("Opened %1").arg(portName));
qDebug() << tr("Opened %1").arg(portName);
}
}
serial_controller_worker::~serial_controller_worker()
{
this->serial.close();
}
void serial_controller_worker::process_data()
{
bool newData = false;
bool run = false;
this->mutex.lock();
newData = this->sendNewData;
run = this->recvLoop;
this->mutex.unlock();
if(run == false)
{
qDebug() << "Run is false, returning!";
return;
}
else
{
if(newData == true)
{
qDebug() << "TransAction started!";
QByteArray requestData = request.toLocal8Bit();
qDebug() << "Writing data: " << requestData;
serial.write(requestData);
qDebug() << "Data written";
if(serial.waitForBytesWritten(waitTimeout))
{
if(serial.waitForReadyRead(waitTimeout))
{
qDebug() << "Waiting for data!";
QByteArray responseData = serial.readAll();
while(serial.waitForReadyRead(10))
responseData += serial.readAll();
QString response(responseData);
QByteArray response_arr = response.toLocal8Bit();
qDebug() << "Response is: " << response_arr.toHex();
emit this->response(response);
}
else
{
qDebug() << "Wait read response timeout";
emit this->timeout(tr("Wait read response timeout %1").arg(QTime::currentTime().toString()));
}
}
else
{
qDebug() << "Wait write request timeout!";
emit this->timeout(tr("Wait write request timeout %1").arg(QTime::currentTime().toString()));
}
mutex.lock();
this->sendNewData = false;
mutex.unlock();
}
QThread::msleep(10);
this->process_data();
}
}
void serial_controller_worker::transaction(const QString &request)
{
mutex.lock();
this->sendNewData = true;
this->recvLoop = true;
this->request = request;
mutex.unlock();
this->process_data();
}
//Serial_controller functions
serial_controller::serial_controller(const QString &portName, int waitTimeout, int BaudRate)
{
serial_controller_worker *newWorker = new serial_controller_worker(portName, waitTimeout, BaudRate);
newWorker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, newWorker, &QObject::deleteLater);
connect(this, &serial_controller::newTransaction, newWorker, &serial_controller_worker::transaction);
connect(newWorker, &serial_controller_worker::response, this, &serial_controller::response_slot);
workerThread.start();
}
serial_controller::~serial_controller()
{
workerThread.quit();
workerThread.wait();
}
void serial_controller::transaction(const QString &request)
{
emit this->newTransaction(request);
}
void serial_controller::response_slot(QString response)
{
emit this->response(response);
}
serial_controller.h:
#include <QObject>
#include <QThread>
#include <QVector>
#include <memory>
#include <QtSerialPort/QtSerialPort>
class serial_controller_worker: public QObject
{
Q_OBJECT
private:
QString portName;
QString request;
int waitTimeout;
QMutex mutex;
QWaitCondition cond;
int baudrate;
QSerialPort serial;
bool quit;
bool sendNewData = false;
bool recvLoop = false;
public slots:
void transaction(const QString &request);
signals:
void response(QString s);
void error(const QString &s);
void timeout(const QString &s);
public:
serial_controller_worker(const QString &portname, int waitTimeout, int BaudRate);
~serial_controller_worker();
void process_data(void);
};
class serial_controller: public QObject
{
Q_OBJECT
private:
QThread workerThread;
QString portName;
QString request;
int waitTimeout;
QMutex mutex;
QWaitCondition cond;
int baudrate;
QSerialPort serial;
public:
serial_controller(const QString &portName, int waitTimeout, int BaudRate);
~serial_controller();
public slots:
void transaction(const QString &request);
void response_slot(QString response);
signals:
void newTransaction(const QString &request);
void response(QString s);
void error(const QString &s);
void timeout(const QString &s);
};
Now I have the problem that
a) when calling serial_controller_worker::process_data(), I get the output
Writing data: "..."
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x2533e60), parent's thread is QThread(0x2486850), current thread is QThread(0x25341e0)
Data written
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
Then I get the answer, but afterwards I am not able to call serial_controller_worker::transaction() anymore, I simply get no notice that it is executed. Why? What am I doing wrong here?
Edit 1: Solution to problem 1 is:
Replacing
QSerialPort serial;
with
QSerialPort *serial;
serial = new QSerialPort(this);
Still the second problem is unsolved.
Ok, found the answer.
The solution to the first problem is:
I have to replace
QSerialPort serial;
with
QSerialPort *serial;
serial = new QSerialPort(this);
The solution to the second problem:
Remove the line
this->process_data();
from the function process_data()

QThreadPool : Qt::QueuedConnection fails

I have a C++ Qt program that uses QThreadPool to process files in no more than MAX_THREAD_COUNT threads. And I'm having troubles with Qt::QueuedConnection. It looks like signals got lost. But with Qt::DirectConnection it works pretty perfect.
I'd be thankful if someone will explain what's going on.
UPDATE: This code works well in Qt, but not in Visual Studio 2005. In VS it stops after the first thread finished (but works with Qt::DirectConnection)
That's how it looks:
Manager.h:
#ifndef MANAGER_H
#define MANAGER_H
#include <QObject>
#include <QThreadPool>
#include <QDebug>
#include "threadbody.h"
class Manager : public QObject
{
Q_OBJECT
private:
QList<QString> processedfiles;
QList<QString> resultfiles;
bool ok;
QString lasterror;
public slots:
void addToProcessedFiles(const QString& filename);
void addToResultFiles(const QString& filename);
void setErrorMessage(const QString& message);
void startNewThread();
public:
bool startManager(const QString& firstfile);
explicit Manager(QObject *parent = 0);
~Manager();
};
#endif // MANAGER_H
ThreadBody.h:
#ifndef THREADBODY_H
#define THREADBODY_H
#include <QTemporaryFile>
#include <QRunnable>
#include <QTextStream>
#define MAX_THREAD_COUNT 4
class ThreadBody : public QObject, public QRunnable
{
Q_OBJECT
private:
QString filename;
public:
ThreadBody(const QString& filename);
~ThreadBody();
void run();
signals:
void sendResult(const QString& filename);
void sendFileName(const QString& filename);
void taskDone();
void threadError(const QString& message);
};
#endif // THREADBODY_H
Manager.cpp:
Manager::Manager(QObject *parent) :
QObject(parent)
{
ok=true;
QThreadPool::globalInstance()->setMaxThreadCount(MAX_THREAD_COUNT);
}
void Manager::addToProcessedFiles(const QString& filename)
{
processedfiles.push_back(filename);
if(QThreadPool::globalInstance()->activeThreadCount()<MAX_THREAD_COUNT)
startNewThread();
}
void Manager::addToResultFiles(const QString& filename)
{
resultfiles.push_back(filename);
}
void Manager::setErrorMessage(const QString& message)
{
ok=false;
lasterror=message;
}
void Manager::startNewThread()
{
if(processedfiles.isEmpty())
return;
if(!ok)
return;
QString filename=processedfiles.takeFirst();
ThreadBody* task=new ThreadBody(filename);
connect(task,SIGNAL(taskDone()),this,SLOT(startNewThread()),Qt::QueuedConnection);
connect(task,SIGNAL(sendResult(const QString&)),this,SLOT(addToResultFiles(const QString&)),Qt::QueuedConnection);
connect(task,SIGNAL(sendFileName(const QString&)),this,SLOT(addToProcessedFiles(const QString&)),Qt::QueuedConnection);
connect(task,SIGNAL(threadError(const QString&)),this,SLOT(setErrorMessage(const QString&)),Qt::QueuedConnection);
qDebug()<<"thread starts";
QThreadPool::globalInstance()->start(task);
}
bool Manager::startManager(const QString& firstfile)
{
processedfiles.push_back(firstfile);
startNewThread();
QThreadPool::globalInstance()->waitForDone();
return ok;
}
Manager::~Manager(){}
ThreadBody.cpp:
#include "threadbody.h"
#include <QThreadPool>
ThreadBody::ThreadBody(const QString& filename)
{
this->filename=filename;
}
ThreadBody::~ThreadBody(){}
void ThreadBody::run()
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
QString message=QString("Thread can't open file=%1: %2").arg(filename).arg(file.errorString());
emit threadError(message);
emit taskDone();
return;
}
QTextStream in(&file);
QTemporaryFile newProcessedFile;
newProcessedFile.setAutoRemove(false);
if(!newProcessedFile.open())
{
QString message="Thread can't open temporary file";
emit threadError(message);
emit taskDone();
return;
}
QTextStream out(&newProcessedFile);
QTemporaryFile resultFile;
resultFile.setAutoRemove(false);
if(!resultFile.open())
{
QString message="Thread can't open temporary file";
emit threadError(message);
emit taskDone();
return;
}
QTextStream resultOut(&resultFile);
while(!in.atEnd())
{
//!---time consuming task---!//
//example//
QString currentLine=in.readLine();
out<<currentLine;
resultOut<<currentLine;
}
resultFile.close();
newProcessedFile.close();
file.remove();
emit sendResult(resultFile.fileName());
emit sendFileName(newProcessedFile.fileName());
emit taskDone();
}
main.cpp:
#include <QCoreApplication>
#include "manager.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Manager manager;
QString filename="C:/airtest/ask.cpp";
manager.startManager(filename);
return a.exec();
}

Error in QSqlTableModel inherited table

I have this class that inherits from QSqlTableModel and it brokes after calling the submitAll() slot, after calling insertPoint some times. Here is the code.
Thanks for the help.
Regards.
#ifndef VWLANDMARKTABLEMODEL_H
#define VWLANDMARKTABLEMODEL_H
#include <QSqlTableModel>
class GraphicsPointLandmarkItem;
class VWLandmarkTableModel : public QSqlTableModel
{
Q_OBJECT
public:
enum Column {
IdColumn=0,
IdImageColumn=1,
UsernameColumn=2,
NameColumn=3,
DefinitionColumn=4
};
VWLandmarkTableModel(QObject *parent=0);
int idImage() const { return m_idImage; }
QString username() const { return m_username; }
void setIdImage(int idImage);
void setUsername(const QString &username);
virtual bool select();
bool insertPoint(GraphicsPointLandmarkItem *item);
signals:
protected:
virtual void setFilter(const QString &filter)
{ QSqlTableModel::setFilter(filter); }
virtual bool insertRowIntoTable(const QSqlRecord &values);
protected slots:
void setDefaults(int row, QSqlRecord &r);
private:
QString m_username;
int m_idImage;
};
#endif // VWLANDMARKTABLEMODEL_H
#include "vwlandmarktablemodel.h"
#include <QtGui>
#include <QtSql>
#include <QtCore>
#include "graphicspointlandmarkitem.h"
VWLandmarkTableModel::VWLandmarkTableModel(QObject *parent)
: QSqlTableModel(parent)
{
setTable("vwLandmark");
setEditStrategy(OnManualSubmit);
connect(this, SIGNAL(primeInsert(int,QSqlRecord&)),
SLOT(setDefaults(int,QSqlRecord&)));
}
void VWLandmarkTableModel::setIdImage(int idImage)
{ m_idImage=idImage; }
void VWLandmarkTableModel::setUsername(const QString &username)
{ m_username=username; }
bool VWLandmarkTableModel::select()
{
Q_ASSERT(!username().isEmpty());
QString stmt="IdImage = %1 AND Username = '%2'";
setFilter(stmt.arg(idImage()).arg(username()));
return QSqlTableModel::select();
}
bool VWLandmarkTableModel::insertPoint(GraphicsPointLandmarkItem *item)
{
QString text;
QTextStream out(&text);
int row=rowCount();
bool ok;
ok=insertRow(row); Q_ASSERT(ok);
QModelIndex idx=index(row, DefinitionColumn);
out<<*item;
ok=setData(idx, text);
Q_ASSERT(ok);
idx=index(row, NameColumn);
ok=setData(idx, tr("New point landmark"));
Q_ASSERT(ok);
return ok;
}
bool VWLandmarkTableModel::insertRowIntoTable(const QSqlRecord &values)
{
QString sql="EXEC InsertLandmark :IdImage,:Definition";
QSqlQuery q;
bool ok;
ok=q.prepare(sql); Q_ASSERT(ok);
q.bindValue(":IdImage", values.value("IdImage"));
q.bindValue(":Definition", values.value("Definition"));
q.setForwardOnly(true);
if(!q.exec()) {
setLastError(q.lastError());
return false;
}
qDebug()<<"hola mundo";
return true;
}
void VWLandmarkTableModel::setDefaults(int row, QSqlRecord &r)
{
r.setValue("IdImage", idImage());
r.setValue("Name", "Unnamed landmark");
r.setValue("Username", username());
}
The problem was in my custom select. Calling setFilter() causes a infinite calling loop