TextPreview: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=TextPreview=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]<br />[[Category:Learning::Demos_and_Examples]]<br />[[Category:snippets]]
 
= 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.
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.
Line 5: Line 7:
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.
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==
== Souce code ==
 
=== TextPreview.pro ===
 
#————————————————-<br />#<br /># Project created by QtCreator 2013-09-04T00:11:05<br />#<br />#————————————————-


===TextPreview.pro===
<code>QT ''= core gui
<br />greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets


<nowiki>#————————————————————————-</nowiki><br /> #
TARGET = TextPreview<br />TEMPLATE = app


# Project created by QtCreator 2013-09-04T00:11:05
SOURCES ''= main.cpp widget.cpp
<br />HEADERS''= widget.h


<br /> # #————————————————————————-
FORMS ''= widget.ui
<br />unix|win32: LIBS''= <s>luchardet</code>
<br />h3. widget.h
<br /><code>#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</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


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


===main.cpp===
// 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 /> }


===widget.cpp===
// 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 /> }


===widget.ui===
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


==Gui design (widget.ui)==
// 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>


[[Image:textpreview_2.png|Picture of Gui Design in Qt Creator for widget.ui]]
=== widget.ui ===


==Example==
<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>


Ran the program.
== Gui design (widget.ui) ==


[[Image:textpreview.png|Picture of a widget window when the TextPreview program is launched.]]
[[Image:http://applickle.net/images/textpreview_2.png|Picture of Gui Design in Qt Creator for widget.ui]]


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


[[Image:textpreview_1.png|Picture of the widget window after pushing the preview button and choosing a text file.]]
Ran the program.


The file’s path and character set are shown, and the first 2 lines, as specified, are shown in the plain text box.
[[Image:http://applickle.net/images/textpreview.png|Picture of a widget window when the TextPreview program is launched.]]


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


# [https://github.com/suiato/Qt/tree/master/TextPreview TextPreview at GitHub] ''[github.com]''
[[Image:http://applickle.net/images/textpreview_1.png|Picture of the widget window after pushing the preview button and choosing a text file.]]


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


* [[:Category:Learning|Learning]]
== Links ==
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
* [[:Category:snippets|snippets]]

Revision as of 09:48, 24 February 2015

[toc align_right="yes&quot; depth="3&quot;]

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