Jump to content

TextPreview

From Qt Wiki
Revision as of 09:48, 24 February 2015 by Maintenance script (talk | contribs)

[toc align_right="yes" depth="3"]

TextPreview

This is an example of Qt Gui Application to read a specified number of lines or all lines from a text file, whose character set is automatically detected and displayed.

The program was developed with Qt Creator 2.7.1, and ran successfully with Qt 4.8.4 on Ubuntu 13.04, linking to a universal character detection library libuchardet available in package as libuchardet-dev. Lines for debugging are intentionally left so as to be helpful in checking the program.

Souce code

TextPreview.pro

  1. ————————————————-
    #
    # Project created by QtCreator 2013-09-04T00:11:05
    #
    #————————————————-
QT ''= core gui
<br />greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets

TARGET = TextPreview<br />TEMPLATE = app

SOURCES ''= main.cpp widget.cpp
<br />HEADERS''= widget.h

FORMS ''= widget.ui
<br />unix|win32: LIBS''= <s>luchardet


h3. widget.h


#ifndef WIDGET_H<br />#define WIDGET_H
<br />#include &lt;QWidget&amp;gt;
<br />namespace Ui {<br />class Widget;<br />}
<br />class Widget : public QWidget<br />{<br /> Q_OBJECT
<br />public:<br /> explicit Widget(QWidget *parent = 0);<br /> ~Widget();
<br />private slots:<br /> void on_pushButton_clicked();
<br />private:<br /> Ui::Widget *ui;<br />};
<br />#endif // WIDGET_H


h3. main.cpp


#include &quot;widget.h&amp;quot;<br />#include &lt;QApplication&amp;gt;
<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> Widget w;
<br /> w.setWindowTitle(&quot;Text Preview&amp;quot;);
<br /> w.show();
<br /> return a.exec&amp;amp;#40;&amp;#41;;<br />}


h3. widget.cpp


#include &quot;widget.h&amp;quot;<br />#include &quot;ui_widget.h&amp;quot;
<br />#include &lt;QLabel&amp;gt;<br />#include &lt;QString&amp;gt;<br />#include &lt;QFileDialog&amp;gt;<br />#include &lt;QDir&amp;gt;<br />#include &lt;QMessageBox&amp;gt;<br />#include &lt;QTextCodec&amp;gt;<br />#include &lt;QTextDecoder&amp;gt;
<br />#include &lt;uchardet/uchardet.h&amp;gt;
<br />#include &lt;QDebug&amp;gt;
<br />Widget::Widget(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Widget)<br />{<br /> ui-&gt;setupUi(this);<br />}
<br />Widget::~Widget()<br />{<br /> delete ui;<br />}
<br />void Widget::on_pushButton_clicked()<br />{<br /> // Get file name<br /> QString fileName = QFileDialog::getOpenFileName(this,<br /> tr(&quot;Open text file&amp;quot;), QDir::homePath(), tr(&quot;Text files ('''.txt)&quot;));<br /> ui</s>&gt;label_4-&gt;setText(fileName); // Show file name

// Open text file<br /> QFile file&amp;amp;#40;fileName&amp;amp;#41;;<br /> if(!file.open(QIODevice::ReadOnly | QIODevice::Text))<br /> return;

// Read lines<br /> int i = 0;<br /> QString lm = ui-&gt;lineEdit-&gt;text();<br /> int lineMax = lm.toInt(); // Maximum number of lines to be read<br /> qDebug() &lt;&lt; &quot;lineMax = &quot; &lt;&lt; QString::number(lineMax);<br /> QByteArray temp(&quot;&quot;);<br /> QByteArray line(&quot;&quot;);<br /> while (i++ &lt; lineMax &amp;&amp; !file.atEnd())<br /> {<br /> line = file.readLine();<br /> temp = temp.append(line);<br /> }

// Detect character code set<br /> uchardet_t ucd = uchardet_new();<br /> if(uchardet_handle_data(ucd, temp, 20) != 0) // first 20 bytes used for detection<br /> {<br /> QMessageBox msg;<br /> msg.setText(&quot;Failed to detect character set from data.UTF-8.&quot;);<br /> msg.exec&amp;amp;#40;&amp;#41;;<br /> }

uchardet_data_end(ucd);<br /> const char *charSetName = uchardet_get_charset(ucd);<br /> QString csn = charSetName;<br /> qDebug() &lt;&lt; &quot;Detected character set: &quot; &lt;&lt; csn;<br /> uchardet_delete(ucd);<br /> ui-&gt;label_6-&gt;setText(charSetName); // Show character code set

// Preview text<br /> QTextCodec *codec = QTextCodec::codecForName(charSetName); // Set codec<br /> QTextDecoder *decoder = codec-&gt;makeDecoder(); // Decode<br /> QString str = decoder-&gt;toUnicode(temp); // Get text<br /> ui-&gt;plainTextEdit-&gt;setPlainText(str); // Show text<br />}

widget.ui

&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;
<ui version="4.0&quot;>
<class&gt;Widget&lt;/class&gt;
<widget class="QWidget&quot; name="Widget&quot;>
<property name="geometry&quot;>
<rect&gt;
<x&gt;0&lt;/x&gt;
<y&gt;0&lt;/y&gt;
<width&gt;603&lt;/width&gt;
<height&gt;492&lt;/height&gt;
</rect&gt;
</property&gt;
<property name="windowTitle&quot;>
<string&gt;Widget&lt;/string&gt;
</property&gt;
<layout class="QVBoxLayout&quot; name="verticalLayout_2&quot;>
<item&gt;
<layout class="QVBoxLayout&quot; name="verticalLayout&quot;>
<item&gt;
<widget class="QLabel&quot; name="label&quot;>
<property name="font&quot;>
<font&gt;
<pointsize&gt;14&lt;/pointsize&gt;
<weight&gt;75&lt;/weight&gt;
<italic&gt;false&lt;/italic&gt;
<bold&gt;true&lt;/bold&gt;
</font&gt;
</property&gt;
<property name="text&quot;>
<string&gt;Text Preview&lt;/string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<layout class="QHBoxLayout&quot; name="horizontalLayout&quot;>
<item&gt;
<widget class="QPushButton&quot; name="pushButton&quot;>
<property name="text&quot;>
<string&gt;Preview&lt;/string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLineEdit&quot; name="lineEdit&quot;>
<property name="text&quot;>
<string&gt;10&lt;/string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLabel&quot; name="label_2&quot;>
<property name="text&quot;>
<string&gt;lines&lt;/string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<spacer name="horizontalSpacer&quot;>
<property name="orientation&quot;>
<enum&gt;Qt::Horizontal&lt;/enum&gt;
</property&gt;
<property name="sizeHint&quot; stdset="0&quot;>
<size&gt;
<width&gt;288&lt;/width&gt;
<height&gt;20&lt;/height&gt;
</size&gt;
</property&gt;
</spacer&gt;
</item&gt;
</layout&gt;
</item&gt;
<item&gt;
<layout class="QHBoxLayout&quot; name="horizontalLayout_2&quot;>
<item&gt;
<widget class="QLabel&quot; name="label_3&quot;>
<property name="minimumSize&quot;>
<size&gt;
<width&gt;0&lt;/width&gt;
<height&gt;0&lt;/height&gt;
</size&gt;
</property&gt;
<property name="maximumSize&quot;>
<size&gt;
<width&gt;70&lt;/width&gt;
<height&gt;16777215&lt;/height&gt;
</size&gt;
</property&gt;
<property name="text&quot;>
<string&gt;File name: </string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLabel&quot; name="label_4&quot;>
<property name="text&quot;>
<string/&gt;
</property&gt;
</widget&gt;
</item&gt;
</layout&gt;
</item&gt;
<item&gt;
<layout class="QHBoxLayout&quot; name="horizontalLayout_3&quot;>
<item&gt;
<widget class="QLabel&quot; name="label_5&quot;>
<property name="text&quot;>
<string&gt;Character code set: </string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<widget class="QLabel&quot; name="label_6&quot;>
<property name="text&quot;>
<string/&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<spacer name="horizontalSpacer_2&quot;>
<property name="orientation&quot;>
<enum&gt;Qt::Horizontal&lt;/enum&gt;
</property&gt;
<property name="sizeHint&quot; stdset="0&quot;>
<size&gt;
<width&gt;228&lt;/width&gt;
<height&gt;20&lt;/height&gt;
</size&gt;
</property&gt;
</spacer&gt;
</item&gt;
</layout&gt;
</item&gt;
<item&gt;
<layout class="QHBoxLayout&quot; name="horizontalLayout_4&quot;>
<item&gt;
<widget class="QLabel&quot; name="label_7&quot;>
<property name="text&quot;>
<string&gt;Text: </string&gt;
</property&gt;
</widget&gt;
</item&gt;
<item&gt;
<spacer name="horizontalSpacer_3&quot;>
<property name="orientation&quot;>
<enum&gt;Qt::Horizontal&lt;/enum&gt;
</property&gt;
<property name="sizeHint&quot; stdset="0&quot;>
<size&gt;
<width&gt;478&lt;/width&gt;
<height&gt;20&lt;/height&gt;
</size&gt;
</property&gt;
</spacer&gt;
</item&gt;
</layout&gt;
</item&gt;
<item&gt;
<widget class="QPlainTextEdit&quot; name="plainTextEdit&quot;>
<property name="readOnly&quot;>
<bool&gt;true&lt;/bool&gt;
</property&gt;
</widget&gt;
</item&gt;
</layout&gt;
</item&gt;
</layout&gt;
</widget&gt;
<layoutdefault spacing="6&quot; margin="11&quot;/&gt;
<resources/&gt;
<connections/&gt;
</ui&gt;

Gui design (widget.ui)

Picture of Gui Design in Qt Creator for widget.ui

Example

Ran the program.

Picture of a widget window when the TextPreview program is launched.

Changed the number of lines to 2, and hit the Preview button. Chose a text file in a dialog box.

Picture of the widget window after pushing the preview button and choosing a text file.

The file's path and character set are shown, and the first 2 lines, as specified, are shown in the plain text box.

Links