Hi... I am writing program which consist from two exactly same parts, so I decided to make one object and then use it twice


sifrator.cpp
Qt Code:
  1. #include <iostream>
  2. #include <QtGui>
  3. #include <QLocale>
  4. #include <QFile>
  5. #include <QByteArray>
  6. #include <QChar>
  7.  
  8. #include "sifrator.h"
  9.  
  10. using namespace std;
  11.  
  12. EncrypterBody::EncrypterBody(QWidget *parent)
  13. : QWidget(parent)
  14. {
  15.  
  16. int LABEL_STYLE = QFrame::Sunken | QFrame::Panel;
  17.  
  18. // Zadavani klicu
  19. QLineEdit *keyPublic = new QLineEdit();
  20. keyPublic->setMinimumWidth(TEXT_W);
  21. keyPublic->setMaximumWidth(TEXT_W);
  22. keyPublic->setMaxLength(50);
  23. keyPublic->setText(trUtf8("Veřejný klÃ*č"));
  24.  
  25. // Name Label souboru
  26. openFileName = new QLabel;
  27. openFileName->setFrameStyle(LABEL_STYLE);
  28. openFileName->setText(trUtf8("Cesta k otevřenému souboru"));
  29.  
  30. // Textarea
  31. text = new QTextEdit();
  32. text->setText(trUtf8("Text"));
  33. text->canPaste();
  34. text->setTabStopWidth(500);
  35. text->setMinimumSize(TEXT_W - BUTT_W - 5, TEXT_H);
  36. text->setMaximumSize(TEXT_W - BUTT_W - 5, TEXT_H);
  37.  
  38. // Buttónky
  39. buttonTranslate = new QPushButton(trUtf8("Přeložit"));
  40. buttonTranslate->setMinimumSize(BUTT_W, BUTT_H);
  41. buttonTranslate->setMaximumSize(BUTT_W, BUTT_H);
  42. connect(buttonTranslate, SIGNAL(clicked()), this, SLOT(textEncryp()));
  43.  
  44. QPushButton *buttonSave = new QPushButton(trUtf8("Uložit"));
  45. buttonSave->setMinimumSize(BUTT_W, BUTT_H);
  46. buttonSave->setMaximumSize(BUTT_W, BUTT_H);
  47. connect(buttonSave, SIGNAL(clicked()), this, SLOT(saveFile()));
  48.  
  49. QPushButton *buttonOpen = new QPushButton(trUtf8("NačÃ*st"));
  50. buttonOpen->setMinimumSize(BUTT_W, BUTT_H);
  51. buttonOpen->setMaximumSize(BUTT_W, BUTT_H);
  52. connect(buttonOpen, SIGNAL(clicked()), this, SLOT(openFile()));
  53.  
  54. // UspořádánÃ* layoutu
  55. QGridLayout *texts_buttons = new QGridLayout;
  56. texts_buttons->addWidget(openFileName, 0, 0);
  57. texts_buttons->addWidget(text, 1, 0, 4, 1);
  58. texts_buttons->addWidget(buttonTranslate, 1, 1);
  59. texts_buttons->addWidget(buttonSave, 2, 1);
  60. texts_buttons->addWidget(buttonOpen, 3, 1);
  61. texts_buttons->setRowMinimumHeight(4, TEXT_H - (3 * BUTT_H + 8));
  62.  
  63. QVBoxLayout *layout = new QVBoxLayout;
  64. layout->addWidget(keyPublic);
  65. layout->addLayout(texts_buttons);
  66. layout->setSizeConstraint(QLayout::SetFixedSize);
  67. setLayout(layout);
  68.  
  69. }
  70.  
  71. ////////////
  72. // FUNKCE //
  73. ////////////
  74.  
  75. void EncrypterBody::openFile()
  76. {
  77. QFileDialog::Options options;
  78. QString selectedFilter;
  79.  
  80. QString fileName = QFileDialog::getOpenFileName(this, trUtf8("Vyber textový soubor"), openFileName->text(), trUtf8("Textové soubory (*.txt)"));
  81.  
  82. if (!fileName.isEmpty())
  83. {
  84. if (fileName != openFileName->text())
  85. {
  86. openFileName->setText(fileName);
  87. }
  88.  
  89. readFile();
  90. }
  91. }
  92.  
  93. void EncrypterBody::readFile()
  94. {
  95. text->clear();
  96.  
  97. QFile file(openFileName->text());
  98. file.open(QIODevice::ReadOnly | QIODevice::Text);
  99.  
  100. while (!file.atEnd())
  101. {
  102. QString line = QString::fromUtf8(file.readLine());
  103. text->append(line);
  104. }
  105.  
  106. file.close();
  107. }
  108.  
  109. void EncrypterBody::saveFile()
  110. {
  111. QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Vyber textový soubor"), openFileName->text(), trUtf8("Textové soubory (*.txt)"));
  112.  
  113. openFileName->setText(fileName);
  114.  
  115. QFile file(fileName);
  116. file.open(QIODevice::WriteOnly | QIODevice::Text);
  117.  
  118. QTextStream out(&file);
  119. out << text->toPlainText();
  120.  
  121. file.close();
  122. }
  123.  
  124. QString EncrypterBody::textEncryption()
  125. {
  126. //some unimportant code for encrypting which returns some QString
  127. return textEncrypted;
  128. }
  129.  
  130. void EncrypterBody::setText(QString string)
  131. {
  132. text->setText(string);
  133. }
  134.  
  135. /*void EncrypterBody::clickedTranslation(QString string)
  136. {
  137. if (buttonTranslate->clicked())
  138. {
  139. emit clickedTranslation;
  140. }
  141. }*/
To copy to clipboard, switch view to plain text mode 

sifrator.h
Qt Code:
  1. #ifndef SIFRATOR_H
  2. #define SIFRATOR_H
  3.  
  4. #include <QWidget>
  5. #include <QString>
  6.  
  7. const int PISMO_V = 12;
  8. const int BUTT_H = 25;
  9. const int BUTT_W = 70;
  10. const int TEXT_H = 200;
  11. const int TEXT_W = 550;
  12.  
  13. QT_BEGIN_NAMESPACE
  14. class QLabel;
  15. class QTextEdit;
  16. QT_END_NAMESPACE
  17.  
  18. class EncrypterBody : public QWidget
  19. {
  20. Q_OBJECT
  21.  
  22. public:
  23. EncrypterBody(QWidget *parent = 0);
  24. //
  25. QString textEncryption();
  26.  
  27. public slots:
  28. void setText(QString string);
  29.  
  30. /*signals:
  31. void clickedTranslation(QString string);*/
  32.  
  33. private slots:
  34. void openFile();
  35. //
  36. void readFile();
  37. //
  38. void saveFile();
  39.  
  40. private:
  41. QTextEdit *text;
  42. //
  43. QLabel *openFileName;
  44. //
  45. QPushButton *buttonTranslate;
  46. };
  47.  
  48. #endif
To copy to clipboard, switch view to plain text mode 


main.cpp
Qt Code:
  1. #include <QApplication>
  2.  
  3. #include "sifrator.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication sifrator(argc, argv);
  8.  
  9. EncrypterBody partEncryption;
  10. EncrypterBody partDecryption;
  11.  
  12. /*QString string = partEncryption.textEncryption();
  13. QObject::connect(&partEncryption, SIGNAL(clickedTrasnlate(QString)), &partDecryption, SLOT(setText(QString)));*/
  14.  
  15.  
  16. partEncryption.show();
  17. partDecryption.show();
  18.  
  19. return sifrator.exec();
  20. }
To copy to clipboard, switch view to plain text mode 

So what I am tring to do is, when I click buttonTranslation in object partEncryption I want text in text (QTextEdit) in partDecription to be set to partEncryption.textEncryption() which returns string.
Thanks in advance for any advice.

Petr