Results 1 to 5 of 5

Thread: Connecting custom signal and slot

  1. #1
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Question Connecting custom signal and slot

    Hello!

    I have a small problem with connecting the custom signal and the custom slot(Object::connect: No such signal)

    Here is the code, what I have:

    datedialog.h:

    Qt Code:
    1. #ifndef DATEDIALOG_H
    2. #define DATEDIALOG_H
    3.  
    4. #include <QDialog>
    5. class QLineEdit;
    6.  
    7. class DateDialog:public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. DateDialog(QWidget *parent = 0);
    13. signals:
    14. void SendText(const QString &text);
    15.  
    16. private:
    17. void Initialization();
    18. void PlacingElements();
    19.  
    20. private slots:
    21. void EnableOKButton(const QString &text);
    22. void GetDateToLine();
    23. void BeforeShowMsg();
    24. void ShowMessageBox(const QString &text);
    25.  
    26. private:
    27. QCalendarWidget *calendar;
    28. QLineEdit *line;
    29. QPushButton *OkButton;
    30.  
    31. };
    32.  
    33. #endif // DATEDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    datedialog.cpp

    Qt Code:
    1. #include "datedialog.h"
    2. #include <QHBoxLayout>
    3. #include <QCalendarWidget>
    4. #include <QLineEdit>
    5. #include <QPushButton>
    6. #include <QIcon>
    7. #include <QString>
    8. #include <QMessageBox>
    9.  
    10. DateDialog::DateDialog(QWidget *parent):QDialog(parent)
    11. {
    12. Initialization();
    13. }
    14.  
    15. void DateDialog::Initialization()
    16. {
    17. calendar = new QCalendarWidget();
    18. line = new QLineEdit();
    19. line->setFocus();
    20. OkButton = new QPushButton(QObject::tr("Press me!"));
    21. OkButton->setDefault(true);
    22. OkButton->setEnabled(false);
    23. connect(this->calendar, SIGNAL(selectionChanged()),
    24. this, SLOT(GetDateToLine()));
    25. connect(this->line, SIGNAL(textChanged(const QString &)),
    26. this, SLOT(EnableOKButton(const QString &)));
    27. connect(this->OkButton, SIGNAL(clicked()),
    28. this, SLOT(BeforeShowMsg()));
    29. connect(this, SIGNAL(SendText(cosnt QString &)),
    30. this, SLOT(ShowMessageBox(const QString &)));
    31. PlacingElements();
    32. }
    33.  
    34. void DateDialog::PlacingElements()
    35. {
    36. QHBoxLayout *leftTop = new QHBoxLayout;
    37. leftTop->addWidget(this->line);
    38. QHBoxLayout *leftDown = new QHBoxLayout;
    39. leftDown->addWidget(OkButton);
    40.  
    41. QVBoxLayout *left = new QVBoxLayout;
    42. left->addLayout(leftTop);
    43. left->addStretch();
    44. left->addLayout(leftDown);
    45.  
    46. QVBoxLayout *right = new QVBoxLayout;
    47. right->addWidget(this->calendar);
    48.  
    49. QHBoxLayout *main = new QHBoxLayout;
    50. main->addLayout(left);
    51. main->addLayout(right);
    52.  
    53. this->setLayout(main);
    54. this->setWindowTitle(QObject::tr("Date"));
    55.  
    56. QString FilePath = "E:\\linux-penguin-computing.jpg";
    57. this->setWindowIcon(QIcon(FilePath));
    58. this->setFixedSize(sizeHint().width(), sizeHint().height());
    59. }
    60.  
    61. void DateDialog::EnableOKButton(const QString &text)
    62. {
    63. this->OkButton->setEnabled(!text.isEmpty());
    64. OkButton->setEnabled(true);
    65. }
    66.  
    67. void DateDialog::GetDateToLine()
    68. {
    69. this->line->setText(calendar->selectedDate().toString("dd/MM/yyyy dddd"));
    70. }
    71.  
    72. void DateDialog::ShowMessageBox(const QString &text)
    73. {
    74. msg.setText(text);
    75. msg.setWindowTitle(QObject::tr("Information before closing"));
    76. msg.setIcon(QMessageBox::Warning);
    77. msg.exec();
    78. this->close();
    79. }
    80.  
    81. void DateDialog::BeforeShowMsg()
    82. {
    83. if(!line->text().isEmpty())
    84. {
    85. emit SendText(line->text());
    86. }
    87. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

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

    The first question that I have is: why?
    And the second one is: How it can be rectified?

    Thank you beforehand for answers.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connecting custom signal and slot

    You have a typo - "cosnt" in line 29.

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

    DmitryNik (12th September 2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Connecting custom signal and slot

    Thank you so much! I didn't notice that typo at all.

  5. #4
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connecting custom signal and slot

    You have to use QObject::connect method.

  6. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting custom signal and slot

    Quote Originally Posted by Gokulnathvc View Post
    You have to use QObject::connect method.
    he *does* use this method... or where do you think does the "connect" in his code come from? the mistake is a simple typo.

Similar Threads

  1. Replies: 2
    Last Post: 7th August 2011, 11:50
  2. Connecting signal and slot by name
    By grayfox in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 09:00
  3. Replies: 3
    Last Post: 17th November 2010, 14:12
  4. Replies: 2
    Last Post: 9th September 2009, 00:26
  5. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 09:28

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.