PDA

View Full Version : plainText object and greek



funkybomber
3rd May 2010, 22:00
Hello, I'm creating a program that needs user input in the form of a large text, which may include both english and greek words, and also numbers. The text is given through a plainText object.

While I don't have any problems with the english words and the numbers, my program crashes when I try to enter greek characters.

This is the line of code that reads the contents of the plainText object that I use:

the_str = ui->plainTextEdit->toPlainText();

Do you have any ideas?

Btw, I'm using the latest version of Qt Develop, and the program is in C++.

Lykurg
3rd May 2010, 22:04
Normaly that is no reason for a crash. So can you provide a minimal compilable programm reproducing your problem.

funkybomber
3rd May 2010, 22:20
file: sf_mainwindow.h
----------------------------


#ifndef SF_MAINWINDOW_H
#define SF_MAINWINDOW_H
#include <QMainWindow>

namespace Ui {
class sf_MainWindow;
}

class sf_MainWindow : public QMainWindow {
Q_OBJECT
public:
sf_MainWindow(QWidget *parent = 0);
~sf_MainWindow();

public slots:
void doSomething();

protected:
void changeEvent(QEvent *e);

private:
Ui::sf_MainWindow *ui;
};

#endif // SF_MAINWINDOW_H





file: main.cpp
-------------------


#include <QtGui/QApplication>
#include "sf_mainwindow.h"
#include <QTextCodec>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
sf_MainWindow w;
w.show();
return a.exec();
}




file: sf_mainwindow.cpp
-------------------------------


#include <QtGui>
#include "sf_mainwindow.h"
#include "ui_sf_mainwindow.h"

sf_MainWindow::sf_MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::sf_MainWindow)
{
ui->setupUi(this);
connect( ui->pushButton_fix, SIGNAL( clicked() ), this, SLOT( doSomething() ) );
}


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

void sf_MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}



void sf_MainWindow::doSomething()
{
#include <qstring.h>
//#include <locale>
//using namespace std;
//std::cout.imbue( std::locale("Greek") );


int i, the_count, megethos_pinaka;

QString the_str;

// Diabazoyme to dothen string se morfi ISO kai to apothikeyoyme
the_str = ui->plainTextEdit->toPlainText();



// QByteArray encodedString12 = the_str.toLocal8Bit(); // .toLocal8Bit();
// QTextCodec *codecf = QTextCodec::codecForName("ISO-8859-7");
// QString the_new_string2 = codecf->toUnicode(encodedString12);

QString selected_lang, string1, string2;
QString given_var[100];


selected_lang="greek";

//EPIDIORTHWSI EKTRWMATWN

// opoy yparxoyn dyo synexomenes teleies, bale kai mia triti
the_str.replace(QString(".."), QString("..."));


if (selected_lang=="greek"){

// opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
given_var[0]="??";
given_var[1]="?;";
given_var[2]=";?";
given_var[3]=" ?";
given_var[4]=" ;";
given_var[5]=";;";
megethos_pinaka=6;
for (i=0; i<megethos_pinaka; i++){
the_count = the_str.count(QString(given_var[i]));
while (the_count>0){
the_str.replace(QString(given_var[i]), QString(";"));
the_count = the_str.count(QString(given_var[i]));
}
}

}

// opoy yparxoyn ayta ta ektrwmata, antikatestise ta me !
given_var[0]="! .";
given_var[1]="!.";
given_var[2]=".!";
given_var[3]=". !";
given_var[4]=" !";
megethos_pinaka=5;
for (i=0; i<megethos_pinaka; i++){
the_count = the_str.count(QString(given_var[i]));
while (the_count>0){
the_str.replace(QString(given_var[i]), QString("!"));
the_count = the_str.count(QString(given_var[i]));
}
}

// opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
given_var[0]="; .";
given_var[1]=";.";
given_var[2]=".;";
given_var[3]=". ;";
given_var[4]=" ;";
megethos_pinaka=5;
for (i=0; i<megethos_pinaka; i++){
the_count = the_str.count(QString(given_var[i]));
while (the_count>0){
the_str.replace(QString(given_var[i]), QString(";"));
the_count = the_str.count(QString(given_var[i]));
}
}

// opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
given_var[0]=", .";
given_var[1]=",.";
given_var[2]=".,";
given_var[3]=". ,";
given_var[4]=" ,";
megethos_pinaka=5;
for (i=0; i<megethos_pinaka; i++){
the_count = the_str.count(QString(given_var[i]));
while (the_count>0){
the_str.replace(QString(given_var[i]), QString(","));
the_count = the_str.count(QString(given_var[i]));
}
}


the_count = the_str.count(QRegExp(",,"));
while (the_count>0){
the_str.replace(QString(",,"), QString(","));
the_count = the_str.count(QRegExp(",,"));
}

the_count = the_str.count(QRegExp("::"));
while (the_count>0){
the_str.replace(QString("::"), QString(":"));
the_count = the_str.count(QRegExp("::"));
}

the_count = the_str.count(QRegExp(";;"));
while (the_count>0){
the_str.replace(QString(";;"), QString(";"));
the_count = the_str.count(QRegExp(";;"));
}

the_count = the_str.count(QRegExp("!!"));
while (the_count>0){
the_str.replace(QString("!!"), QString("!"));
the_count = the_str.count(QRegExp("!!"));
}

the_count = the_str.count(QRegExp(" :"));
while (the_count>0){
the_str.replace(QString(" :"), QString(":"));
the_count = the_str.count(QRegExp(" :"));
}

the_count = the_str.count(QRegExp(" ."));
while (the_count>0){
the_str.replace(QString(" ."), QString("."));
the_count = the_str.count(QRegExp(" ."));
}


// PROSTHIKI KENWN GIA KALYTERI EMFANISI

//bazw keno meta apo kathe teleia (ena perasma)
the_str.replace(QString("."), QString(". "));

// bazw keno meta apo kathe komma (ena perasma) (prokyptoyn problimata stoys xronoys twn ypotitlwn, alla mporoyne na ftiaxtoyn)
the_str.replace(QString(","), QString(", "));

// bazw keno meta apo kathe anw kai katw teleia (ena perasma) (prokyptoyn problimata stoys xronoys twn ypotitlwn, alla mporoyne na ftiaxtoyn)
the_str.replace(QString(":"), QString(": "));

// bazw keno meta apo kathe erwtimatiko, elliniko (ena perasma)
the_str.replace(QString(";"), QString("; "));

// bazw keno meta apo kathe thaymastiko (ena perasma)
the_str.replace(QString("!"), QString("! "));


QString kefalaia[] = {"Α", "ΒΆ", "Β", "Γ", "Δ", "Ε", "Έ", "Ζ", "Η", "Ή", "Θ", "Ι", "Ί", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Ό", "Π", "Ρ", "Σ", "Τ", "Υ", "Ύ", "Φ", "Χ", "Ψ", "Ω", "Ώ",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
megethos_pinaka=57;
for (i=0; i<megethos_pinaka; i++){
// bazw keno meta apo kathe payla dialogoy (an einai se arxi protasis/dialogoy) (ena perasma)
//1: an h epomenh leksi arxizei me kefalaio (Agglika h Ellinika)
// the_str.replace('\n' + "-" + QString(kefalaia[i]), '\n' + "- " + QString(kefalaia[i]));
the_str+=" : "+ QString::number(i) + "~" + kefalaia[i]+ '\n';
string1=" -" + QString(kefalaia[i]);
string2=" - " + QString(kefalaia[i]);
//the_str.replace(string1, string2);
}

delete given_var;
delete kefalaia;

//emfanisi toy diorthwmenoy string
ui->plainTextEdit->setPlainText( the_str );


}

norobro
4th May 2010, 00:56
I don't speak Greek so I can't test your program. :o But I'm surprised it ever gets out of the loop starting at line 162. The period (".") has special meaning in regular expressions. It matches any character. Your loop checks for "a space + any character" and then in replace you want to replace "space + period" with a "period" so "never the twain shall meet". If you want to search for a "." using regex you have to escape it: "\\."

You don't need any of the while loops anyway. From the Qt docs:
QString & QString::replace ( const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive )
This function overloads replace().

Replaces every occurrence of the string before with the string after and returns a reference to this string.
In place of those loops just the replace() statement is necessary, i.e. line 132-136 can be replaced with:
the_str.replace(",,", ",");
HTH

Lykurg
4th May 2010, 07:43
Right now I have not so much time. Just quickly some points: Use QStringList instead of QString foo[100]; and if you use regular expressions use something like that instead of all your loops:
the_str.replace(QRegExp("\\,{2,}"), QString(","));

EDIT: And also:
the_str.replace(QRegExp("([\\.\\,\\:\\;])"), QString("\\\1 ")); for adding a space after .,;: etc. You can also comibe both expressions to only one!