TextPreview
[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
- ————————————————-
#
# 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 <QWidget&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 "widget.h&quot;<br />#include <QApplication&gt;
<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> Widget w;
<br /> w.setWindowTitle("Text Preview&quot;);
<br /> w.show();
<br /> return a.exec&amp;#40;&#41;;<br />}
h3. widget.cpp
#include "widget.h&quot;<br />#include "ui_widget.h&quot;
<br />#include <QLabel&gt;<br />#include <QString&gt;<br />#include <QFileDialog&gt;<br />#include <QDir&gt;<br />#include <QMessageBox&gt;<br />#include <QTextCodec&gt;<br />#include <QTextDecoder&gt;
<br />#include <uchardet/uchardet.h&gt;
<br />#include <QDebug&gt;
<br />Widget::Widget(QWidget '''parent) :<br /> QWidget(parent),<br /> ui(new Ui::Widget)<br />{<br /> ui->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("Open text file&quot;), QDir::homePath(), tr("Text files ('''.txt)"));<br /> ui</s>>label_4->setText(fileName); // Show file name
// Open text file<br /> QFile file&amp;#40;fileName&amp;#41;;<br /> if(!file.open(QIODevice::ReadOnly | QIODevice::Text))<br /> return;
// Read lines<br /> int i = 0;<br /> QString lm = ui->lineEdit->text();<br /> int lineMax = lm.toInt(); // Maximum number of lines to be read<br /> qDebug() << "lineMax = " << QString::number(lineMax);<br /> QByteArray temp("");<br /> QByteArray line("");<br /> while (i++ < lineMax && !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("Failed to detect character set from data.UTF-8.");<br /> msg.exec&amp;#40;&#41;;<br /> }
uchardet_data_end(ucd);<br /> const char *charSetName = uchardet_get_charset(ucd);<br /> QString csn = charSetName;<br /> qDebug() << "Detected character set: " << csn;<br /> uchardet_delete(ucd);<br /> ui->label_6->setText(charSetName); // Show character code set
// Preview text<br /> QTextCodec *codec = QTextCodec::codecForName(charSetName); // Set codec<br /> QTextDecoder *decoder = codec->makeDecoder(); // Decode<br /> QString str = decoder->toUnicode(temp); // Get text<br /> ui->plainTextEdit->setPlainText(str); // Show text<br />}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>603</width>
<height>492</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Text Preview</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Preview</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>10</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>lines</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>288</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>70</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>File name: </string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Character code set: </string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>228</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Text: </string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>478</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
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.