TextPreview

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

Jump to: navigation, search

[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

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