TextPreview: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=TextPreview= | [toc align_right="yes&quot; depth="3&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 />#————————————————- | |||
== | <code>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 | |||
<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 <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</code> | |||
<br />h3. main.cpp | |||
<br /><code>#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 />}</code> | |||
<br />h3. widget.cpp | |||
<br /><code>#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 />}</code> | ||
=== widget.ui === | |||
== | <code>&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Widget&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Widget&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;603&lt;/width&gt;<br /> <height&gt;492&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Widget&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout_2&quot;><br /> <item&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label&quot;><br /> <property name="font&quot;><br /> <font&gt;<br /> <pointsize&gt;14&lt;/pointsize&gt;<br /> <weight&gt;75&lt;/weight&gt;<br /> <italic&gt;false&lt;/italic&gt;<br /> <bold&gt;true&lt;/bold&gt;<br /> </font&gt;<br /> </property&gt;<br /> <property name="text&quot;><br /> <string&gt;Text Preview&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;Preview&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit&quot;><br /> <property name="text&quot;><br /> <string&gt;10&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_2&quot;><br /> <property name="text&quot;><br /> <string&gt;lines&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;288&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_2&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_3&quot;><br /> <property name="minimumSize&quot;><br /> <size&gt;<br /> <width&gt;0&lt;/width&gt;<br /> <height&gt;0&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> <property name="maximumSize&quot;><br /> <size&gt;<br /> <width&gt;70&lt;/width&gt;<br /> <height&gt;16777215&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> <property name="text&quot;><br /> <string&gt;File name: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_4&quot;><br /> <property name="text&quot;><br /> <string/&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_3&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_5&quot;><br /> <property name="text&quot;><br /> <string&gt;Character code set: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_6&quot;><br /> <property name="text&quot;><br /> <string/&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer_2&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;228&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_4&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_7&quot;><br /> <property name="text&quot;><br /> <string&gt;Text: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer_3&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;478&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QPlainTextEdit&quot; name="plainTextEdit&quot;><br /> <property name="readOnly&quot;><br /> <bool&gt;true&lt;/bool&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <layoutdefault spacing="6&quot; margin="11&quot;/&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt;</code> | ||
== Gui design (widget.ui) == | |||
[[Image: | [[Image:http://applickle.net/images/textpreview_2.png|Picture of Gui Design in Qt Creator for widget.ui]] | ||
== Example == | |||
Ran the program. | |||
[[Image:http://applickle.net/images/textpreview.png|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. | |||
[[Image:http://applickle.net/images/textpreview_1.png|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 == | |||
Revision as of 09:48, 24 February 2015
[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
&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Widget&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Widget&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;603&lt;/width&gt;<br /> <height&gt;492&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Widget&lt;/string&gt;<br /> </property&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout_2&quot;><br /> <item&gt;<br /> <layout class="QVBoxLayout&quot; name="verticalLayout&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label&quot;><br /> <property name="font&quot;><br /> <font&gt;<br /> <pointsize&gt;14&lt;/pointsize&gt;<br /> <weight&gt;75&lt;/weight&gt;<br /> <italic&gt;false&lt;/italic&gt;<br /> <bold&gt;true&lt;/bold&gt;<br /> </font&gt;<br /> </property&gt;<br /> <property name="text&quot;><br /> <string&gt;Text Preview&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout&quot;><br /> <item&gt;<br /> <widget class="QPushButton&quot; name="pushButton&quot;><br /> <property name="text&quot;><br /> <string&gt;Preview&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit&quot;><br /> <property name="text&quot;><br /> <string&gt;10&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_2&quot;><br /> <property name="text&quot;><br /> <string&gt;lines&lt;/string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;288&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_2&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_3&quot;><br /> <property name="minimumSize&quot;><br /> <size&gt;<br /> <width&gt;0&lt;/width&gt;<br /> <height&gt;0&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> <property name="maximumSize&quot;><br /> <size&gt;<br /> <width&gt;70&lt;/width&gt;<br /> <height&gt;16777215&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> <property name="text&quot;><br /> <string&gt;File name: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_4&quot;><br /> <property name="text&quot;><br /> <string/&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_3&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_5&quot;><br /> <property name="text&quot;><br /> <string&gt;Character code set: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_6&quot;><br /> <property name="text&quot;><br /> <string/&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer_2&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;228&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <layout class="QHBoxLayout&quot; name="horizontalLayout_4&quot;><br /> <item&gt;<br /> <widget class="QLabel&quot; name="label_7&quot;><br /> <property name="text&quot;><br /> <string&gt;Text: </string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> <item&gt;<br /> <spacer name="horizontalSpacer_3&quot;><br /> <property name="orientation&quot;><br /> <enum&gt;Qt::Horizontal&lt;/enum&gt;<br /> </property&gt;<br /> <property name="sizeHint&quot; stdset="0&quot;><br /> <size&gt;<br /> <width&gt;478&lt;/width&gt;<br /> <height&gt;20&lt;/height&gt;<br /> </size&gt;<br /> </property&gt;<br /> </spacer&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> <item&gt;<br /> <widget class="QPlainTextEdit&quot; name="plainTextEdit&quot;><br /> <property name="readOnly&quot;><br /> <bool&gt;true&lt;/bool&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <layoutdefault spacing="6&quot; margin="11&quot;/&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></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.