Results 1 to 5 of 5

Thread: plainText object and greek

  1. #1
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default plainText object and greek

    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++.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: plainText object and greek

    Normaly that is no reason for a crash. So can you provide a minimal compilable programm reproducing your problem.

  3. #3
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plainText object and greek

    file: sf_mainwindow.h
    ----------------------------
    Qt Code:
    1. #ifndef SF_MAINWINDOW_H
    2. #define SF_MAINWINDOW_H
    3. #include <QMainWindow>
    4.  
    5. namespace Ui {
    6. class sf_MainWindow;
    7. }
    8.  
    9. class sf_MainWindow : public QMainWindow {
    10. Q_OBJECT
    11. public:
    12. sf_MainWindow(QWidget *parent = 0);
    13. ~sf_MainWindow();
    14.  
    15. public slots:
    16. void doSomething();
    17.  
    18. protected:
    19. void changeEvent(QEvent *e);
    20.  
    21. private:
    22. Ui::sf_MainWindow *ui;
    23. };
    24.  
    25. #endif // SF_MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 





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

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "sf_mainwindow.h"
    3. #include <QTextCodec>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. sf_MainWindow w;
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 



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

    Qt Code:
    1. #include <QtGui>
    2. #include "sf_mainwindow.h"
    3. #include "ui_sf_mainwindow.h"
    4.  
    5. sf_MainWindow::sf_MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::sf_MainWindow)
    8. {
    9. ui->setupUi(this);
    10. connect( ui->pushButton_fix, SIGNAL( clicked() ), this, SLOT( doSomething() ) );
    11. }
    12.  
    13.  
    14. sf_MainWindow::~sf_MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void sf_MainWindow::changeEvent(QEvent *e)
    20. {
    21. QMainWindow::changeEvent(e);
    22. switch (e->type()) {
    23. case QEvent::LanguageChange:
    24. ui->retranslateUi(this);
    25. break;
    26. default:
    27. break;
    28. }
    29. }
    30.  
    31.  
    32.  
    33. void sf_MainWindow::doSomething()
    34. {
    35. #include <qstring.h>
    36. //#include <locale>
    37. //using namespace std;
    38. //std::cout.imbue( std::locale("Greek") );
    39.  
    40.  
    41. int i, the_count, megethos_pinaka;
    42.  
    43. QString the_str;
    44.  
    45. // Diabazoyme to dothen string se morfi ISO kai to apothikeyoyme
    46. the_str = ui->plainTextEdit->toPlainText();
    47.  
    48.  
    49.  
    50. // QByteArray encodedString12 = the_str.toLocal8Bit(); // .toLocal8Bit();
    51. // QTextCodec *codecf = QTextCodec::codecForName("ISO-8859-7");
    52. // QString the_new_string2 = codecf->toUnicode(encodedString12);
    53.  
    54. QString selected_lang, string1, string2;
    55. QString given_var[100];
    56.  
    57.  
    58. selected_lang="greek";
    59.  
    60. //EPIDIORTHWSI EKTRWMATWN
    61.  
    62. // opoy yparxoyn dyo synexomenes teleies, bale kai mia triti
    63. the_str.replace(QString(".."), QString("..."));
    64.  
    65.  
    66. if (selected_lang=="greek"){
    67.  
    68. // opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
    69. given_var[0]="??";
    70. given_var[1]="?;";
    71. given_var[2]=";?";
    72. given_var[3]=" ?";
    73. given_var[4]=" ;";
    74. given_var[5]=";;";
    75. megethos_pinaka=6;
    76. for (i=0; i<megethos_pinaka; i++){
    77. the_count = the_str.count(QString(given_var[i]));
    78. while (the_count>0){
    79. the_str.replace(QString(given_var[i]), QString(";"));
    80. the_count = the_str.count(QString(given_var[i]));
    81. }
    82. }
    83.  
    84. }
    85.  
    86. // opoy yparxoyn ayta ta ektrwmata, antikatestise ta me !
    87. given_var[0]="! .";
    88. given_var[1]="!.";
    89. given_var[2]=".!";
    90. given_var[3]=". !";
    91. given_var[4]=" !";
    92. megethos_pinaka=5;
    93. for (i=0; i<megethos_pinaka; i++){
    94. the_count = the_str.count(QString(given_var[i]));
    95. while (the_count>0){
    96. the_str.replace(QString(given_var[i]), QString("!"));
    97. the_count = the_str.count(QString(given_var[i]));
    98. }
    99. }
    100.  
    101. // opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
    102. given_var[0]="; .";
    103. given_var[1]=";.";
    104. given_var[2]=".;";
    105. given_var[3]=". ;";
    106. given_var[4]=" ;";
    107. megethos_pinaka=5;
    108. for (i=0; i<megethos_pinaka; i++){
    109. the_count = the_str.count(QString(given_var[i]));
    110. while (the_count>0){
    111. the_str.replace(QString(given_var[i]), QString(";"));
    112. the_count = the_str.count(QString(given_var[i]));
    113. }
    114. }
    115.  
    116. // opoy yparxoyn ayta ta ektrwmata, antikatestise ta me ;
    117. given_var[0]=", .";
    118. given_var[1]=",.";
    119. given_var[2]=".,";
    120. given_var[3]=". ,";
    121. given_var[4]=" ,";
    122. megethos_pinaka=5;
    123. for (i=0; i<megethos_pinaka; i++){
    124. the_count = the_str.count(QString(given_var[i]));
    125. while (the_count>0){
    126. the_str.replace(QString(given_var[i]), QString(","));
    127. the_count = the_str.count(QString(given_var[i]));
    128. }
    129. }
    130.  
    131.  
    132. the_count = the_str.count(QRegExp(",,"));
    133. while (the_count>0){
    134. the_str.replace(QString(",,"), QString(","));
    135. the_count = the_str.count(QRegExp(",,"));
    136. }
    137.  
    138. the_count = the_str.count(QRegExp("::"));
    139. while (the_count>0){
    140. the_str.replace(QString("::"), QString(":"));
    141. the_count = the_str.count(QRegExp("::"));
    142. }
    143.  
    144. the_count = the_str.count(QRegExp(";;"));
    145. while (the_count>0){
    146. the_str.replace(QString(";;"), QString(";"));
    147. the_count = the_str.count(QRegExp(";;"));
    148. }
    149.  
    150. the_count = the_str.count(QRegExp("!!"));
    151. while (the_count>0){
    152. the_str.replace(QString("!!"), QString("!"));
    153. the_count = the_str.count(QRegExp("!!"));
    154. }
    155.  
    156. the_count = the_str.count(QRegExp(" :"));
    157. while (the_count>0){
    158. the_str.replace(QString(" :"), QString(":"));
    159. the_count = the_str.count(QRegExp(" :"));
    160. }
    161.  
    162. the_count = the_str.count(QRegExp(" ."));
    163. while (the_count>0){
    164. the_str.replace(QString(" ."), QString("."));
    165. the_count = the_str.count(QRegExp(" ."));
    166. }
    167.  
    168.  
    169. // PROSTHIKI KENWN GIA KALYTERI EMFANISI
    170.  
    171. //bazw keno meta apo kathe teleia (ena perasma)
    172. the_str.replace(QString("."), QString(". "));
    173.  
    174. // bazw keno meta apo kathe komma (ena perasma) (prokyptoyn problimata stoys xronoys twn ypotitlwn, alla mporoyne na ftiaxtoyn)
    175. the_str.replace(QString(","), QString(", "));
    176.  
    177. // bazw keno meta apo kathe anw kai katw teleia (ena perasma) (prokyptoyn problimata stoys xronoys twn ypotitlwn, alla mporoyne na ftiaxtoyn)
    178. the_str.replace(QString(":"), QString(": "));
    179.  
    180. // bazw keno meta apo kathe erwtimatiko, elliniko (ena perasma)
    181. the_str.replace(QString(";"), QString("; "));
    182.  
    183. // bazw keno meta apo kathe thaymastiko (ena perasma)
    184. the_str.replace(QString("!"), QString("! "));
    185.  
    186.  
    187. QString kefalaia[] = {"Α", "¶", "Β", "Γ", "Δ", "Ε", "Έ", "Ζ", "Η", "Ή", "Θ", "Ι", "Ί", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Ό", "Π", "Ρ", "Σ", "Τ", "Υ", "Ύ", "Φ", "Χ", "Ψ", "Ω", "Ώ",
    188. "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"};
    189. megethos_pinaka=57;
    190. for (i=0; i<megethos_pinaka; i++){
    191. // bazw keno meta apo kathe payla dialogoy (an einai se arxi protasis/dialogoy) (ena perasma)
    192. //1: an h epomenh leksi arxizei me kefalaio (Agglika h Ellinika)
    193. // the_str.replace('\n' + "-" + QString(kefalaia[i]), '\n' + "- " + QString(kefalaia[i]));
    194. the_str+=" : "+ QString::number(i) + "~" + kefalaia[i]+ '\n';
    195. string1=" -" + QString(kefalaia[i]);
    196. string2=" - " + QString(kefalaia[i]);
    197. //the_str.replace(string1, string2);
    198. }
    199.  
    200. delete given_var;
    201. delete kefalaia;
    202.  
    203. //emfanisi toy diorthwmenoy string
    204. ui->plainTextEdit->setPlainText( the_str );
    205.  
    206.  
    207. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by funkybomber; 3rd May 2010 at 22:31.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: plainText object and greek

    I don't speak Greek so I can't test your program. 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:
    Qt Code:
    1. the_str.replace(",,", ",");
    To copy to clipboard, switch view to plain text mode 
    HTH
    Last edited by norobro; 4th May 2010 at 02:45. Reason: typo

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: plainText object and greek

    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:
    Qt Code:
    1. the_str.replace(QRegExp("\\,{2,}"), QString(","));
    To copy to clipboard, switch view to plain text mode 

    EDIT: And also:
    Qt Code:
    1. the_str.replace(QRegExp("([\\.\\,\\:\\;])"), QString("\\\1 "));
    To copy to clipboard, switch view to plain text mode 
    for adding a space after .,;: etc. You can also comibe both expressions to only one!
    Last edited by Lykurg; 4th May 2010 at 07:46. Reason: updated contents

Similar Threads

  1. Replies: 15
    Last Post: 31st January 2020, 18:25
  2. greek language
    By eem in forum Qt Tools
    Replies: 0
    Last Post: 6th July 2009, 16:15
  3. greek symbols
    By genick bar-meir in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2007, 22:44
  4. problem with storing greek chars to a buffer (os linux)
    By nass in forum General Programming
    Replies: 3
    Last Post: 3rd January 2007, 16:54
  5. Extract method on plaintext with QRegExp
    By jlbrd in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2006, 13:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.