Results 1 to 5 of 5

Thread: Problem with signals & slots

  1. #1
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with signals & slots

    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

  2. #2
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with signals & slots

    So when buttonTranslation is clicked, SIGNAL(clickedTrasnlate(QString)) need to be emitted, I think u could add

    Qt Code:
    1. : : textEncryp()
    2. {
    3. .....
    4. emit clickedTranslate(textEncrypted);// if this is the right string you want to pass
    5. return something;
    6. }
    To copy to clipboard, switch view to plain text mode 
    in SLOT(textEncryp())

    BTW, you might miss the slot key word before textEncryp(), maybe you want to write:

    Qt Code:
    1. private slots:
    2. QString textEncryp();
    To copy to clipboard, switch view to plain text mode 

    Then when the button is clicked, signal clickedTranslate( QString) will be emitted and connect this signal to any slot you want to response;

  3. The following user says thank you to nifei for this useful post:

    Palmik (17th January 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with signals & slots

    Thank you very much, now I feel, that this is the right way. But still it does not work :/
    Dunno why, maybe and hopefully it is just a silly typo.

    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. public slots:
    26. void setText(QString string);
    27.  
    28. signals:
    29. void clickedTranslate(QString string);
    30.  
    31. private slots:
    32. void openFile();
    33. //
    34. void readFile();
    35. //
    36. void saveFile();
    37. //
    38. void textEncryption();
    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 

    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(textEncryption()));
    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. // Načte jméno souboru do text()u QLabelu
    76. void EncrypterBody::openFile()
    77. {
    78. QFileDialog::Options options;
    79. QString selectedFilter;
    80.  
    81. QString fileName = QFileDialog::getOpenFileName(this, trUtf8("Vyber textový soubor"), openFileName->text(), trUtf8("Textové soubory (*.txt)"));
    82.  
    83. if (!fileName.isEmpty())
    84. {
    85. if (fileName != openFileName->text())
    86. {
    87. openFileName->setText(fileName);
    88. }
    89.  
    90. readFile();
    91. }
    92. }
    93.  
    94. void EncrypterBody::readFile()
    95. {
    96. text->clear();
    97.  
    98. QFile file(openFileName->text());
    99. file.open(QIODevice::ReadOnly | QIODevice::Text);
    100.  
    101. while (!file.atEnd())
    102. {
    103. QString line = QString::fromUtf8(file.readLine());
    104. text->append(line);
    105. }
    106.  
    107. file.close();
    108. }
    109.  
    110. void EncrypterBody::saveFile()
    111. {
    112. QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Vyber textový soubor"), openFileName->text(), trUtf8("Textové soubory (*.txt)"));
    113.  
    114. openFileName->setText(fileName);
    115.  
    116. QFile file(fileName);
    117. file.open(QIODevice::WriteOnly | QIODevice::Text);
    118.  
    119. QTextStream out(&file);
    120. out << text->toPlainText();
    121.  
    122. file.close();
    123. }
    124.  
    125. //clicked() SIGNAL is connected to this SLOT which is emmiting another SIGNAL
    126. void EncrypterBody::textEncryption()
    127. {
    128. QString textNormal = text->toPlainText();
    129. textNormal = textNormal.simplified();
    130.  
    131. QString textEncrypted;
    132. QTextStream out(&textEncrypted);
    133.  
    134. for (int o = 0; o < textNormal.size(); o++)
    135. {
    136. QChar cChar = textNormal.data()[o];
    137. short cNumber = cChar.unicode();
    138.  
    139. for (int i = 1; i < 3; i++)
    140. {
    141. if (cNumber < pow(10, i))
    142. {
    143. textEncrypted.append('0');
    144. }
    145. }
    146. out << cNumber;
    147. }
    148. emit clickedTranslate(textEncrypted);
    149. }
    150.  
    151. void EncrypterBody::setText(QString string)
    152. {
    153. text->setText(string);
    154. }
    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.  
    10. // Here I created two instances of Encrypter body
    11. EncrypterBody partEncryption;
    12. EncrypterBody partDecryption;
    13.  
    14. // Here I am trying to achieve this: When I click buttonTranslate in partEncryption part I want change text in text (QTextEdit) in partDecription instance, however this does not seem to work.
    15. QObject::connect(&partEncryption, SIGNAL(clickedTrasnlate(QString)), &partDecryption, SLOT(setText(QString)));
    16.  
    17.  
    18. partEncryption.show();
    19. partDecryption.show();
    20.  
    21. return sifrator.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with signals & slots

    When I added CONFIG+=console to *.pro file a then recompiled it and runned the output was:
    Object::connect: No such signal EncrypterBody::clickedTrasnlate(QString)

    It made me just more confused than I was :P
    Edit: I found it.... the problem was of course typo I misspelled "clickedTranslate" in main
    Now it is working as intended

    However the application makes 2 windows (2 objects of one class) How should I moddify the layout to make it appear in one window? Can you direct to some documentation connected with this topic?
    Last edited by Palmik; 17th January 2009 at 17:48.

  6. #5
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with signals & slots

    Quote Originally Posted by Palmik View Post
    However the application makes 2 windows (2 objects of one class) How should I moddify the layout to make it appear in one window? Can you direct to some documentation connected with this topic?
    No wonder, you're creating two QWidgets and showing both in your main.cpp. Just create a QDialog or QMainWindow with an appropriate layout, add those widgets to the layout and show the dialog/mainwindow. See Layout Management for how to work with layouts.

Similar Threads

  1. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  2. Signals And Slots problem
    By ldiamond in forum Newbie
    Replies: 7
    Last Post: 23rd March 2008, 00:11
  3. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39
  4. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35

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.