Seite 1 von 1

C++ Filestream in QT umsetzen / QFileSystemWatcher

Verfasst: 17. Juli 2007 00:31
von TobiG
Hallo zusammen,

ich habe ein simples C++ Programm, was ich gern auch in QT nutzen würde. Es ist ein Programm mit dem man auf eine Datei pollen kann, hier der Source Code:

Code: Alles auswählen

void main(void)
{
	fstream f;
	char cstring[256];
	char leer[256];
	f.open("test.dat",ios::in);

	do
	{
		f.clear();
		while (!f.eof())
		{
			f.getline(cstring,sizeof(cstring));
			if(strcmp(cstring, ""))
			{
				cout << cstring << endl;
			}
		}
	}
	while(true);
	f.close();
}
Nun meine Frage, wie kann man dies am Besten den c++ fstream in QT umsetzen?


Mit freundlichem Gruss
Tobias

Verfasst: 17. Juli 2007 06:31
von Christian81
QFile & QTextStream

Verfasst: 17. Juli 2007 06:34
von isifloh
hi

Schau mal unter QFile in der Dokumentation nach.

Bsp:
QFile file("test.dat");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
if (!line.isEmpty()) qDebug() << line;
}
file.close();


mfg

Verfasst: 17. Juli 2007 14:11
von TobiG
Besten Dank, das einlesen funktioniert schon richtig gut, aber wie kann ich die Funktion so aufrufen das immer auf die Datei gelauscht wird und wenn ein anderes Programm etwas neues hinein schreibt das auch sofort angezeigt wird!?

Besten Dank
Tobias

Verfasst: 17. Juli 2007 14:13
von Christian81
QFileSystemWatcher + Signal fileChanged

QFileSystemWatcher

Verfasst: 23. Juli 2007 17:00
von TobiG
Vielen Dank! Ich habe es, nach etlichen Versuchen, implementieren können und es wird auch ohne Fehler kompiliert. Leider hat es keine Wirkung so wie ich es bis jetzt habe.

ich habe einen Button erstellt mit dem man die Funktion aktivieren kann, ich finde aber leider den/die Fehler nicht!

Hier mal meine bisherige Anwendung:

mainwindow.cpp (Teile)

Code: Alles auswählen

//MainWindow
MainWindow::MainWindow()
{
	sortedView = new QTreeView();
	sortingModel = new QSortFilterProxyModel();
	clear();
	sortedView->header()->setSortIndicator(1, Qt::AscendingOrder);
	sortedView->header()->setSortIndicatorShown(true);
	sortedView->header()->setClickable(true);
	sortedView->setRootIsDecorated(false);
	sortedView->setAlternatingRowColors(true);

	setCentralWidget(sortedView);	
	setWindowTitle(QObject::tr("Monitor"));
	setWindowIcon(QIcon(":/images/play2.png"));

	createActions();
    createMenus();
    createToolBars();
	createStatusBar();

	readSettings();
    setCurrentFile("");
	updateStatusBar();
}

//Aktivierung des QFileSystemWatcher (in createActions())
	poll2Action = new QAction(tr("Autom. &Lauschen"), this);
	poll2Action->setCheckable(true);
	poll2Action->setChecked(pollFunc());
	poll2Action->setIcon(QIcon(":/images/play2.png"));
	poll2Action->setStatusTip(tr("Automatisch auf Knoten lauschen"));
	connect(poll2Action, SIGNAL(toggled(bool)), this, SLOT(autopoll(bool)));

//Funktion zur Aktivierung
void MainWindow::autoPoll(const bool &temp)
{
	pollVar=temp;
	fileWatch = new QFileSystemWatcher();
	connect(fileWatch,SIGNAL(fileChanged(pollf)),     //pollf globale Variable für den Dateinamen
			this,SLOT(poll()));
}

void MainWindow::poll()
{
	QString streamTemp;
	QFile pollfile(pollf);
	if (!pollfile.open(QIODevice::ReadOnly | QIODevice::Text)) return;

	QTextStream in(&pollfile);
	streamTemp = in.readAll();
	dynamicReadBuffer(streamTemp);
	model = new TreeModel(streamTemp,temp,runVar);
	sortingModel->setSourceModel(model);
	sortedView->setModel(sortingModel);

	pollfile.close(); 
}

mainwindow.h (komplett)

Code: Alles auswählen

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QFileSystemWatcher>
#include <QMainWindow>
#include <QBuffer>

class QAction;
class QLabel;
class TreeModel;
class QTreeView;
class QSortFilterProxyModel;

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow();
	bool runFunc() const { return runVar; }
	bool pollFunc() const { return pollVar; }
	QBuffer bufferData;

protected:
	void closeEvent(QCloseEvent *event);

private slots:
	void newFile();
	void open();
	bool save();
	bool saveAs();
	void clear();
	void openRecentFile();
	void fileName();
	void poll();
	void autoPoll(const bool &temp);
	void runVarFunc(bool runningVar);

Q_SIGNALS:
	void fileChanged(const QString &file);

private:
	void createActions();
	void createMenus();
	void createContextMenu();
	void createToolBars();
	void createStatusBar();
	void updateStatusBar();
	void readSettings();
	void writeSettings();
	bool loadFile(const QString &fileName);
	bool saveFile(const QString &fileName);
	void setCurrentFile(const QString &fileName);
	void updateRecentFileActions();
	QString strippedName(const QString &fullFileName);
	bool okToContinue();
	bool monitorModified();
	bool dynamicReadBuffer(const QString &bufferData);
	bool readBuffer();
	bool writeBuffer();
	QStringList recentFiles;
	QString curFile;

	enum { MaxRecentFiles = 5 };
	QAction *recentFileActions[MaxRecentFiles];
	QAction *separatorAction;
	QSortFilterProxyModel *sortingModel;
	QTreeView *sortedView;
	TreeModel *model;
	QFileSystemWatcher *fileWatch;
	QLabel *locationLabel;
	QLabel *formulaLabel;
	QMenu *fileMenu;
	QMenu *editMenu;
	QMenu *screenMenu;
	QToolBar *fileToolBar;
	QToolBar *editToolBar;
	QToolBar *screenToolBar;
	QAction *newAction;
	QAction *openAction;
	QAction *saveAction;
	QAction *saveAsAction;
	QAction *exitAction;
	QAction *clearAction;
	QAction *nodeAction;
	QAction *pollAction;
	QAction *poll2Action;
	QAction *runAction;
	bool runVar;
	bool pollVar;
};
#endif
BTW: Ich habe um den QFileSystemWatcher nutzen zu können von der bisher genutzten Version (4.1.1) auf eine höhere Version umsteigen müssen, nun habe ich die z.Z. neuste Version (4.3.0) genommen. Seitdem ich dies nutze funktioniert mein QSortFilterProxyModel nicht mehr! Ich habe es so genutzt:

Code: Alles auswählen

sortedView = new QTreeView();
sortingModel = new QSortFilterProxyModel();
model = new TreeModel("",temp,runVar);
sortingModel->setSourceModel(model);
sortedView->setModel(sortingModel);
setCentralWidget(sortedView);
Ich kann die Header klicken und der Indicator wird auch angezeigt aber in der Liste tut sich nichts!

Verfasst: 23. Juli 2007 23:05
von TobiG
Also das Problem mit QSortFilterProxyModel hab ich gerade hinbekommen! :idea:

ich hatte es so:

Code: Alles auswählen

sortedView->header()->setSortIndicator(1, Qt::AscendingOrder);
sortedView->header()->setSortIndicatorShown(true);
sortedView->header()->setClickable(true);
und nun geht es so:

Code: Alles auswählen

sortedView->setSortingEnabled(true);
Nur leider klappt mein FileWatcher noch nicht, obwohl ich dabei auch noch zwei Fehler entdeckt habe.

nun sieht mein Aufruf so aus:

Code: Alles auswählen

void MainWindow::createActions()
{
	autoAction = new QAction(tr("Automatisches Lauschen"), this);
	autoAction->setCheckable(true);
	autoAction->setChecked(pollFunc());  //gibt den Wert von pollVar zurück
	autoAction->setIcon(QIcon(":/images/play2.png"));
	autoAction->setStatusTip(tr("Automatisches lauschen"));
	connect(autoAction, SIGNAL(toggled(bool)), this, SLOT(autoPoll(bool)));
}

void MainWindow::autoPoll(bool temp)
{
	pollVar=temp;
	fileWatch = new QFileSystemWatcher();
	fileWatch->addPath(pollf);    //pollf - global definierter Dateipfad
	if(pollVar){
		statusBar()->showMessage(tr("Connect gestartet"), 4000);
		connect(fileWatch,SIGNAL(fileChanged(pollf)),
				this,SLOT(poll()));
	}
	else{
		statusBar()->showMessage(tr("Connect gestoppt"), 4000);
		disconnect(fileWatch, 0, 0, 0); 
	}
}
Bei Betätigung des Buttons wird die Funktion autoPoll() aufgerufen, aber wenn es aktiviert ist passiert bei Änderung der Datei nichts!

Verfasst: 23. Juli 2007 23:06
von Christian81
connect(fileWatch,SIGNAL(fileChanged(pollf)), this,SLOT(poll()));

ich glaube das sollten wir uns nochmal anschauen :)

Verfasst: 24. Juli 2007 00:07
von TobiG
:shock: Was meinst du damit? Ist daran etwas falsch!?

Verfasst: 24. Juli 2007 06:33
von Christian81
Einen Parameter zu übergeben ist komplett falsch. Das würdest Du auch sehen, wenn Du auf die Konsole zur Laufzeit schaust (bzw. Qt += console in windows)... das hatten wir aber schon zig mal!

Verfasst: 24. Juli 2007 14:14
von TobiG
8) Besten Dank für das Augen öffnen!! :D

Funktioniert super!

Verfasst: 30. Juli 2007 21:03
von TobiG
Hallo nochmal, ich habe mein Programm jetzt mal unter Linux kompiliert, nun funktioniert der QFileSystemWatcher nicht so wie unter XP! Und zwar, wenn ich die Datei, auf die gelauscht wird änder und danach abspeichere wird mir dies in der Liste dargestellt, aber leider nicht nur einfach sondern fünffach! - Also steht 5 Mal das Gleiche drin!

Woran liegt das / wie kann ich es ändern?