Dear all,
regarding my opening post I'm sorry to everybody who read it. I was quite tired when typed those bollocks. Intention was to have watcher object in main window that listens for sqlite file for changes, so user can use UI while those changes happen in paralel...
Here how I starded it.
#ifndef DIALOG_H
#define DIALOG_H
#include "ui_dialog.h"
#include <QDialog>
#include<QtGui>
#include<iostream>
#include <QtCore>
#include <QMessageBox>
//#include <QtWidgets>
#include <QThread>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QSqlError>
namespace Ui {
class Dialog;
//const QString dbName = "/home/goran/FileTrigger/table";
}
{
Q_OBJECT
public:
bool createConnection()
{
//db.setHostName("localhost");
db.setDatabaseName("/home/goran/FileTrigger/table");
//db.setUserName("user");
//db.setPassword("pasw");
if (!db.open()) {
db.lastError().text());
return false;
}
return true;
}
void connClose(){
db.close();
}
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void fileChangedSlot(const QString&)
{
ui->label->setText("Detected File Change..."); //Detected File Change Promjena u bazi je zabilježena
//ui->label->setText("change detected...");
createConnection();
query2.exec("SELECT lot, lineNo FROM data where idData=1");
while (query2.next()) {
QString title2
= query2.
value(0).
toString();
int pLine2 = query2.value(1).toInt();
ui->lineEdit->setText(title2);
ui
->lineEdit_2
->setText
(QString::number(pLine2
));
}
connClose();
}
void dirChangedSlot(const QString&)
{
int count=0;
createConnection();
query.prepare("select lot, lineNo from data");
if(query.exec()){
//int count=0;
while (query.next()) {
qDebug() << count++;
}
query.finish();
}
ui->label->setText("Detected Directory Change...");
connClose();
}
//havent use this so far..
bool connPrinterOne(){
return true;
}
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
//bool connPrinter1;
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include "ui_dialog.h"
#include <QDialog>
#include<QtGui>
#include<iostream>
#include <QtCore>
#include <QMessageBox>
//#include <QtWidgets>
#include <QThread>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QSqlError>
namespace Ui {
class Dialog;
//const QString dbName = "/home/goran/FileTrigger/table";
}
class Dialog : public QDialog
{
Q_OBJECT
public:
QSqlDatabase db;
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//db.setHostName("localhost");
db.setDatabaseName("/home/goran/FileTrigger/table");
//db.setUserName("user");
//db.setPassword("pasw");
if (!db.open()) {
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}
void connClose(){
db.close();
db.removeDatabase(QSqlDatabase::defaultConnection);
}
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void fileChangedSlot(const QString&)
{
ui->label->setText("Detected File Change..."); //Detected File Change Promjena u bazi je zabilježena
QThread::msleep(100);
//ui->label->setText("change detected...");
createConnection();
QSqlQuery query2;
query2.exec("SELECT lot, lineNo FROM data where idData=1");
while (query2.next()) {
QString title2 = query2.value(0).toString();
int pLine2 = query2.value(1).toInt();
ui->lineEdit->setText(title2);
ui->lineEdit_2->setText(QString::number(pLine2));
}
connClose();
}
void dirChangedSlot(const QString&)
{
int count=0;
createConnection();
QSqlQuery query;
query.prepare("select lot, lineNo from data");
if(query.exec()){
//int count=0;
while (query.next()) {
qDebug() << count++;
}
query.finish();
}
ui->label->setText("Detected Directory Change...");
connClose();
}
//havent use this so far..
bool connPrinterOne(){
return true;
}
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
QFileSystemWatcher* watcher;
QLabel* label;
QString dbName;
//bool connPrinter1;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
//Coloring Qlabel...
//ui->labelOne->setAutoFillBackground(true); // IMPORTANT! I've done it in UI
ui->labelOne->setPalette(pal);
ui->label->setText("Started");
ui->lineEdit->setText("Insert series"); //Unesi lot
ui->lineEdit_2->setText("Insert date");//Unesi datum
QString pathToFile
="/home/goran/FileTrigger/table";
watcher->addPath(pathToFile);
connect(watcher,
SIGNAL(fileChanged
(const QString &)),
this,
SLOT(fileChangedSlot
(const QString &)));
//connect(watcher,SIGNAL(directoryChanged(const QString&)),this,SLOT(dirChangedSlot(const QString&)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
ui->label->setText("");
ui->lineEdit->setText("clear previous text");
ui->lineEdit_2->setText("clear previous text");
}
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//Coloring Qlabel...
//ui->labelOne->setAutoFillBackground(true); // IMPORTANT! I've done it in UI
QPalette pal = ui->labelOne->palette();
pal.setColor(QPalette::Window, QColor(Qt::green));
ui->labelOne->setPalette(pal);
ui->label->setText("Started");
ui->lineEdit->setText("Insert series"); //Unesi lot
ui->lineEdit_2->setText("Insert date");//Unesi datum
QFileSystemWatcher *watcher = new QFileSystemWatcher();
QString pathToFile ="/home/goran/FileTrigger/table";
watcher->addPath(pathToFile);
connect(watcher,SIGNAL(fileChanged(const QString &)),this,SLOT(fileChangedSlot(const QString &)));
//connect(watcher,SIGNAL(directoryChanged(const QString&)),this,SLOT(dirChangedSlot(const QString&)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
ui->label->setText("");
ui->lineEdit->setText("clear previous text");
ui->lineEdit_2->setText("clear previous text");
}
To copy to clipboard, switch view to plain text mode
Bookmarks