TextPreview: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Formatting)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=TextPreview=
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
 
[[Category:Learning::Demos_and_Examples]]
[[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 11:
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 ===
 
#————————————————-
#
# 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>


===TextPreview.pro===
=== widget.cpp ===
<code>#include "widget.h"
#include "ui_widget.h"


<nowiki>#————————————————————————-</nowiki><br /> #
#include <QLabel>
#include <QString>
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>
#include <QTextCodec>
#include <QTextDecoder>


# Project created by QtCreator 2013-09-04T00:11:05
#include <uchardet/uchardet.h>
 
#include <QDebug>
 
Widget::Widget(QWidget '''parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
 
Widget::~Widget()
{
delete ui;
}


<br /> # #————————————————————————-
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


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


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


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


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


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


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


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


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]]

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