TextPreview: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Formatting)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]<br />[[Category:Learning::Demos_and_Examples]]<br />[[Category:snippets]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
 
[[Category:Learning::Demos_and_Examples]]
[[Category:snippets]]


= TextPreview =
= TextPreview =
Line 11: Line 15:
=== TextPreview.pro ===
=== TextPreview.pro ===


#————————————————-<br />#<br /># Project created by QtCreator 2013-09-04T00:11:05<br />#<br />#————————————————-
#————————————————-
#
# Project created by QtCreator 2013-09-04T00:11:05
#
#————————————————-
 
 
 
<code>QT += core gui
 
greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets
 
TARGET = TextPreview
TEMPLATE = app
 
SOURCES  += main.cpp widget.cpp
 
HEADERS+= widget.h
 
FORMS += widget.ui
 
unix|win32: LIBS+= -luchardet</code>
 
=== widget.h ===
<code>#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
 
namespace Ui {
class Widget;
}
 
class Widget : public QWidget
{
Q_OBJECT
 
public:
explicit Widget(QWidget *parent = 0);
~Widget();
 
private slots:
void on_pushButton_clicked();
 
private:
Ui::Widget *ui;
};
 
#endif // WIDGET_H</code>
 
=== main.cpp ===
<code>#include "widget.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
 
w.setWindowTitle("Text Preview");
 
w.show();
 
return a.exec();
}</code>
 
=== widget.cpp ===
<code>#include "widget.h"
#include "ui_widget.h"
 
#include <QLabel>
#include <QString>
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>
#include <QTextCodec>
#include <QTextDecoder>
 
#include <uchardet/uchardet.h>


<code>QT ''= core gui
#include <QDebug>
<br />greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets


TARGET = TextPreview<br />TEMPLATE = app
Widget::Widget(QWidget '''parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}


SOURCES ''= main.cpp widget.cpp
Widget::~Widget()
<br />HEADERS''= widget.h
{
delete ui;
}


FORMS ''= widget.ui
void Widget::on_pushButton_clicked()
<br />unix|win32: LIBS''= <s>luchardet</code>
{
<br />h3. widget.h
// Get file name
<br /><code>#ifndef WIDGET_H<br />#define WIDGET_H
QString fileName = QFileDialog::getOpenFileName(this,
<br />#include &lt;QWidget&amp;gt;
tr("Open text file"), QDir::homePath(), tr("Text files ('''.txt)"));
<br />namespace Ui {<br />class Widget;<br />}
ui->label_4->setText(fileName); // Show file name
<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</code>
<br />h3. main.cpp
<br /><code>#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 />}</code>
<br />h3. widget.cpp
<br /><code>#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;
// Open text file
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
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 /> }
// Read lines
int i = 0;
QString lm = ui->lineEdit->text();
int lineMax = lm.toInt(); // Maximum number of lines to be read
qDebug() << "lineMax = " << QString::number(lineMax);
QByteArray temp("");
QByteArray line("");
while (i++ < lineMax && !file.atEnd())
{
line = file.readLine();
temp = temp.append(line);
}


// 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 /> }
// Detect character code set
uchardet_t ucd = uchardet_new();
if(uchardet_handle_data(ucd, temp, 20) != 0) // first 20 bytes used for detection
{
QMessageBox msg;
msg.setText("Failed to detect character set from data.UTF-8.");
msg.exec();
}


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
uchardet_data_end(ucd);
const char *charSetName = uchardet_get_charset(ucd);
QString csn = charSetName;
qDebug() << "Detected character set: " << csn;
uchardet_delete(ucd);
ui->label_6->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 />}</code>
// Preview text
QTextCodec *codec = QTextCodec::codecForName(charSetName); // Set codec
QTextDecoder *decoder = codec->makeDecoder(); // Decode
QString str = decoder->toUnicode(temp); // Get text
ui->plainTextEdit->setPlainText(str); // Show text
}</code>


=== widget.ui ===
=== widget.ui ===


<code>&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;</code>
<code><?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></code>


== Gui design (widget.ui) ==
== Gui design (widget.ui) ==

Latest revision as of 11:33, 19 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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. ————————————————-
  2. Project created by QtCreator 2013-09-04T00:11:05
  3. ————————————————-


QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets

TARGET = TextPreview
TEMPLATE = app

SOURCES  += main.cpp widget.cpp

HEADERS+= widget.h

FORMS += widget.ui

unix|win32: LIBS+= -luchardet

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
 Q_OBJECT

public:
 explicit Widget(QWidget *parent = 0);
 ~Widget();

private slots:
 void on_pushButton_clicked();

private:
 Ui::Widget *ui;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 Widget w;

 w.setWindowTitle("Text Preview");

 w.show();

 return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QString>
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>
#include <QTextCodec>
#include <QTextDecoder>

#include <uchardet/uchardet.h>

#include <QDebug>

Widget::Widget(QWidget '''parent) :
 QWidget(parent),
 ui(new Ui::Widget)
{
 ui->setupUi(this);
}

Widget::~Widget()
{
 delete ui;
}

void Widget::on_pushButton_clicked()
{
 // Get file name
 QString fileName = QFileDialog::getOpenFileName(this,
 tr("Open text file"), QDir::homePath(), tr("Text files ('''.txt)"));
 ui->label_4->setText(fileName); // Show file name

// Open text file
 QFile file(fileName);
 if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
 return;

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

// Detect character code set
 uchardet_t ucd = uchardet_new();
 if(uchardet_handle_data(ucd, temp, 20) != 0) // first 20 bytes used for detection
 {
 QMessageBox msg;
 msg.setText("Failed to detect character set from data.UTF-8.");
 msg.exec();
 }

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

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

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.

Links