PDA

View Full Version : QFileSystemWatcher - how to set mainWindow visible instead ofwith QFSW



GoranSimunic
29th January 2016, 17:41
Hi all,
how to set MainWindow ui visible where I create widget elements instead of with QFileSystemWatcher gui which pops up when I use object = new QFileSystemWatcher();
Hope you understand my problem. My UI /where I have visual elements) never shows up, only QFSWatcher window pops up when Im trying to listen signals for directory or file changes.

ChrisW67
29th January 2016, 20:12
QFileSystemWatcher is in the QtCore module: it has no GUI.

You will have to explain what you are doing, preferrably with the relevant code (in
... tags), and what you are seeing

GoranSimunic
7th February 2016, 13:12
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";
}

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



#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");
}