PDA

View Full Version : Connecting custom signal and slot



DmitryNik
12th September 2011, 09:46
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:


#ifndef DATEDIALOG_H
#define DATEDIALOG_H

#include <QDialog>
class QPushButton;
class QLineEdit;
class QCalendarWidget;

class DateDialog:public QDialog
{
Q_OBJECT

public:
DateDialog(QWidget *parent = 0);
signals:
void SendText(const QString &text);

private:
void Initialization();
void PlacingElements();

private slots:
void EnableOKButton(const QString &text);
void GetDateToLine();
void BeforeShowMsg();
void ShowMessageBox(const QString &text);

private:
QCalendarWidget *calendar;
QLineEdit *line;
QPushButton *OkButton;

};

#endif // DATEDIALOG_H


datedialog.cpp



#include "datedialog.h"
#include <QHBoxLayout>
#include <QCalendarWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QIcon>
#include <QString>
#include <QMessageBox>

DateDialog::DateDialog(QWidget *parent):QDialog(parent)
{
Initialization();
}

void DateDialog::Initialization()
{
calendar = new QCalendarWidget();
line = new QLineEdit();
line->setFocus();
OkButton = new QPushButton(QObject::tr("Press me!"));
OkButton->setDefault(true);
OkButton->setEnabled(false);
connect(this->calendar, SIGNAL(selectionChanged()),
this, SLOT(GetDateToLine()));
connect(this->line, SIGNAL(textChanged(const QString &)),
this, SLOT(EnableOKButton(const QString &)));
connect(this->OkButton, SIGNAL(clicked()),
this, SLOT(BeforeShowMsg()));
connect(this, SIGNAL(SendText(cosnt QString &)),
this, SLOT(ShowMessageBox(const QString &)));
PlacingElements();
}

void DateDialog::PlacingElements()
{
QHBoxLayout *leftTop = new QHBoxLayout;
leftTop->addWidget(this->line);
QHBoxLayout *leftDown = new QHBoxLayout;
leftDown->addWidget(OkButton);

QVBoxLayout *left = new QVBoxLayout;
left->addLayout(leftTop);
left->addStretch();
left->addLayout(leftDown);

QVBoxLayout *right = new QVBoxLayout;
right->addWidget(this->calendar);

QHBoxLayout *main = new QHBoxLayout;
main->addLayout(left);
main->addLayout(right);

this->setLayout(main);
this->setWindowTitle(QObject::tr("Date"));

QString FilePath = "E:\\linux-penguin-computing.jpg";
this->setWindowIcon(QIcon(FilePath));
this->setFixedSize(sizeHint().width(), sizeHint().height());
}

void DateDialog::EnableOKButton(const QString &text)
{
this->OkButton->setEnabled(!text.isEmpty());
OkButton->setEnabled(true);
}

void DateDialog::GetDateToLine()
{
this->line->setText(calendar->selectedDate().toString("dd/MM/yyyy dddd"));
}

void DateDialog::ShowMessageBox(const QString &text)
{
QMessageBox msg;
msg.setText(text);
msg.setWindowTitle(QObject::tr("Information before closing"));
msg.setIcon(QMessageBox::Warning);
msg.exec();
this->close();
}

void DateDialog::BeforeShowMsg()
{
if(!line->text().isEmpty())
{
emit SendText(line->text());
}
}


main.cpp:



#include <QtGui/QApplication>
#include "datedialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

DateDialog date;
date.exec();

return app.exec();
}


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

Thank you beforehand for answers.

stampede
12th September 2011, 09:53
You have a typo - "cosnt" in line 29.

DmitryNik
12th September 2011, 10:42
Thank you so much! I didn't notice that typo at all.

Gokulnathvc
12th September 2011, 14:13
You have to use QObject::connect method.

FelixB
12th September 2011, 14:15
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.