TextPreview: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[toc align_right= | [toc align_right="yes" depth="3"] | ||
[[Category:Learning::Demos_and_Examples]] | |||
[[Category:snippets]] | |||
= TextPreview = | = TextPreview = | ||
Line 11: | Line 13: | ||
=== TextPreview.pro === | === TextPreview.pro === | ||
#————————————————- | #————————————————- | ||
# | |||
# Project created by QtCreator 2013-09-04T00:11:05 | |||
# | |||
#————————————————- | |||
<code>QT ''= core gui | <code>QT ''= core gui | ||
TARGET = TextPreview | greaterThan(QT_MAJOR_VERSION, 4): QT''= widgets | ||
TARGET = TextPreview | |||
TEMPLATE = app | |||
SOURCES ''= main.cpp widget.cpp | SOURCES ''= main.cpp widget.cpp | ||
HEADERS''= widget.h | |||
FORMS ''= widget.ui | FORMS ''= widget.ui | ||
// | unix|win32: LIBS''= -luchardet</code> | ||
h3. 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> | |||
h3. 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> | |||
h3. 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> | |||
#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 | // 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 | // 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); | 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 | // 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> | <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) == |
Revision as of 10:08, 25 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
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
h3. 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
h3. 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();
}
h3. 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.