Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Handling CSV: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Handling <span class="caps">CSV</span>=
h1. Handling CSV (file format)


This page discusses various available options for working with [http://en.wikipedia.org/wiki/Comma-separated_values csv] ''[en.wikipedia.org]'' documents in your Qt application. Please also read the general considerations outlined on the [[Handling Document Formats]] ''[qt.io]'' page.
This page discusses various available options for working with &quot;csv&amp;quot;:http://en.wikipedia.org/wiki/Comma-separated_values documents in your Qt application. Please also read the general considerations outlined on the &quot;Handling Document Formats&amp;quot;:http://wiki.qt.io/Handling_Document_Formats page.


==Using QxtCsvModel==
== Using QxtCsvModel ==


===intro===
=== intro ===


The [http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html QxtCsvModel] ''[libqxt.bitbucket.org]'' class provides a [http://doc.qt.io/qt-5.0/qtcore/qabstracttablemodel.html QAbstractTableModel] ''[qt.io]'' for <span class="caps">CSV</span> Files. This is perhaps the easiest way possible to read and write csv files without having to parse the csv format to something qt can understand. It’s as simple as using one line of code, for example the following reads the csv file:
The &quot;QxtCsvModel&amp;quot;:http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html class provides a &quot;QAbstractTableModel&amp;quot;:http://doc.qt.io/qt-5.0/qtcore/qabstracttablemodel.html for CSV Files. This is perhaps the easiest way possible to read and write csv files without having to parse the csv format to something qt can understand. It's as simple as using one line of code, for example the following reads the csv file:


===Building libqxt===
<code><br />csvmodel-&gt;setSource(fileName);<br /></code>


QxtCsvModel is a part of [http://dev.libqxt.org/libqxt/wiki/Home libqxt] ''[dev.libqxt.org]'' so just to use QxtCsvModel you’ll need to build this library. It won’t be that hard. Instructions are all provided. However, the download link that they provide in their main page does not support Qt 5 as of this writing(23/4/2013). You’ll need to go [http://dev.libqxt.org/libqxt/downloads here] ''[dev.libqxt.org]'' , select the “branches” tab and download the zip file of the master branch.
=== Building libqxt ===


A novice friendly step by step guide in building and getting libqxt to work in written [[LibQxt in QtCreator|here]] ''[qt.io]'' The following example will be using the build as done in that guide.
QxtCsvModel is a part of &quot;libqxt&amp;quot;:http://dev.libqxt.org/libqxt/wiki/Home so just to use QxtCsvModel you'll need to build this library. It won't be that hard. Instructions are all provided. However, the download link that they provide in their main page does not support Qt 5 as of this writing(23/4/2013). You'll need to go &quot;here&amp;quot;:http://dev.libqxt.org/libqxt/downloads , select the &quot;branches&amp;quot; tab and download the zip file of the master branch.


===Example: Mini csv Program===
A novice friendly step by step guide in building and getting libqxt to work in written &quot;here&amp;quot;:http://wiki.qt.io/LibQxt_in_QtCreator The following example will be using the build as done in that guide.


[[Image:_2013-04-26_12-43-19.png|minicsvprogram]]
=== Example: Mini csv Program ===


[https://bitbucket.org/bruceoutdoors/mini-qt-csv-example Mini csv program] ''[bitbucket.org]'' is built with Qt 5.0.2 MinGW, but should probably work with Qt 4. [https://bitbucket.org/bruceoutdoors/mini-qt-csv-example/get/master.zip Download from here] ''[bitbucket.org]'' . The only thing you would need to do to get it working is fix the links to the dynamic library. I built it in C:/Qt/libqxt-libqxt-Qt5/ so I directed it there. Below is a snipet of the project file:
[[Image:https://lh3.googleusercontent.com/-b5m3QxX8pyM/UXoGGXu2_lI/AAAAAAAABP4/7rkLeGfcEuY/w587-h293/_2013-04-26_12-43-19.png|minicsvprogram]]


Well, you’re also suppose to add the following lines to your qmake project file:<br /> …but it doesn’t seem to make much of a difference here… or any of the examples I’ve tried…
&quot;Mini csv program&amp;quot;:https://bitbucket.org/bruceoutdoors/mini-qt-csv-example is built with Qt 5.0.2 MinGW, but should probably work with Qt 4. &quot;Download from here&amp;quot;:https://bitbucket.org/bruceoutdoors/mini-qt-csv-example/get/master.zip . The only thing you would need to do to get it working is fix the links to the dynamic library. I built it in C:/Qt/libqxt-libqxt-Qt5/ so I directed it there. Below is a snipet of the project file:
 
<code><br />win32:CONFIG (release, debug|release): LIBS ''= -LC:/Qt/libqxt-Qt5/lib/ -lqxtcore<br />else:win32:CONFIG (debug, debug|release): LIBS''= -LC:/Qt/libqxt-Qt5/lib/ -lqxtcore
 
INCLUDEPATH ''= C:/Qt/libqxt-Qt5/src/core<br />DEPENDPATH''= C:/Qt/libqxt-Qt5/src/core<br /></code><br />Well, you're also suppose to add the following lines to your qmake project file:<br /><code><br /> CONFIG ''= qxt<br /> QXT''= core gui<br /></code><br />…but it doesn't seem to make much of a difference here… or any of the examples I've tried…


I can now call the class like so:
I can now call the class like so:


The functionality provided is very very basic. If you wish to probe deeper to what this class can do, check the [http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html QxtCsvModel documentation] ''[libqxt.bitbucket.org]'' .
<code><br />#include &lt;QxtCsvModel&amp;gt;<br /></code>
 
The functionality provided is very very basic. If you wish to probe deeper to what this class can do, check the &quot;QxtCsvModel documentation&amp;quot;:http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html .


==<span class="caps">CSV</span> reader from QtSimplify(Read <span class="caps">CSV</span> Only)==
== CSV reader from QtSimplify(Read CSV Only) ==


http://qtsimplify.blogspot.com/2013/02/dealing-with-csv-files-easy-way.html
http://qtsimplify.blogspot.com/2013/02/dealing-with-csv-files-easy-way.html
And that’s it! Feedback and discussions please go to http://forum.qt.io/viewthread/27167/

Revision as of 14:27, 23 February 2015

h1. Handling CSV (file format)

This page discusses various available options for working with "csv&quot;:http://en.wikipedia.org/wiki/Comma-separated_values documents in your Qt application. Please also read the general considerations outlined on the "Handling Document Formats&quot;:http://wiki.qt.io/Handling_Document_Formats page.

Using QxtCsvModel

intro

The "QxtCsvModel&quot;:http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html class provides a "QAbstractTableModel&quot;:http://doc.qt.io/qt-5.0/qtcore/qabstracttablemodel.html for CSV Files. This is perhaps the easiest way possible to read and write csv files without having to parse the csv format to something qt can understand. It's as simple as using one line of code, for example the following reads the csv file:

<br />csvmodel-&gt;setSource(fileName);<br />

Building libqxt

QxtCsvModel is a part of "libqxt&quot;:http://dev.libqxt.org/libqxt/wiki/Home so just to use QxtCsvModel you'll need to build this library. It won't be that hard. Instructions are all provided. However, the download link that they provide in their main page does not support Qt 5 as of this writing(23/4/2013). You'll need to go "here&quot;:http://dev.libqxt.org/libqxt/downloads , select the "branches&quot; tab and download the zip file of the master branch.

A novice friendly step by step guide in building and getting libqxt to work in written "here&quot;:http://wiki.qt.io/LibQxt_in_QtCreator The following example will be using the build as done in that guide.

Example: Mini csv Program

minicsvprogram

"Mini csv program&quot;:https://bitbucket.org/bruceoutdoors/mini-qt-csv-example is built with Qt 5.0.2 MinGW, but should probably work with Qt 4. "Download from here&quot;:https://bitbucket.org/bruceoutdoors/mini-qt-csv-example/get/master.zip . The only thing you would need to do to get it working is fix the links to the dynamic library. I built it in C:/Qt/libqxt-libqxt-Qt5/ so I directed it there. Below is a snipet of the project file:

<br />win32:CONFIG (release, debug|release): LIBS ''= -LC:/Qt/libqxt-Qt5/lib/ -lqxtcore<br />else:win32:CONFIG (debug, debug|release): LIBS''= -LC:/Qt/libqxt-Qt5/lib/ -lqxtcore

INCLUDEPATH ''= C:/Qt/libqxt-Qt5/src/core<br />DEPENDPATH''= C:/Qt/libqxt-Qt5/src/core<br />


Well, you're also suppose to add the following lines to your qmake project file:

<br /> CONFIG ''= qxt<br /> QXT''= core gui<br />


…but it doesn't seem to make much of a difference here… or any of the examples I've tried…

I can now call the class like so:

<br />#include &lt;QxtCsvModel&amp;gt;<br />

The functionality provided is very very basic. If you wish to probe deeper to what this class can do, check the "QxtCsvModel documentation&quot;:http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html .

CSV reader from QtSimplify(Read CSV Only)

http://qtsimplify.blogspot.com/2013/02/dealing-with-csv-files-easy-way.html