PDA

View Full Version : "No such slot..."



willhelm
10th May 2012, 21:11
So, i'm very newbie. I've started with The Book of Qt4 but i stucked. Chapter 3, ByteConverter via QDesigner. After couple of days i managed this example to start the window and validate data in the fields but i still can't force it to convert them because...


Object::connect: No such slot QDialog::decChanged(QString)
Object::connect: (sender name: 'decEdit')
Object::connect: (receiver name: 'Dialog')
Object::connect: No such slot QDialog::hexChanged(QString)
Object::connect: (sender name: 'hexEdit')
Object::connect: (receiver name: 'Dialog')
Object::connect: No such slot QDialog::binChanged(QString)
Object::connect: (sender name: 'binEdit')
Object::connect: (receiver name: 'Dialog')

And here are my files:

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "ui_byteconverterdialog.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private:
Ui::Dialog *ui;


private slots:
// void decChanged (const QString&);
// void hexChanged (const QString&);
// void binChanged (const QString&);
};

#endif // DIALOG_H


ui_byteconverterdialog.h:

/************************************************** ******************************
** Form generated from reading UI file 'byteconverterdialog.ui'
**
** Created: Thu May 10 20:34:47 2012
** by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
************************************************** ******************************/

#ifndef UI_BYTECONVERTERDIALOG_H
#define UI_BYTECONVERTERDIALOG_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QGridLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QSpacerItem>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Dialog
{
public:
QWidget *layoutWidget;
QVBoxLayout *verticalLayout_2;
QGridLayout *gridLayout;
QLabel *label;
QVBoxLayout *verticalLayout;
QLineEdit *decEdit;
QLineEdit *hexEdit;
QLineEdit *binEdit;
QLabel *label_2;
QLabel *label_3;
QSpacerItem *verticalSpacer;
QHBoxLayout *horizontalLayout;
QSpacerItem *horizontalSpacer;
QDialogButtonBox *buttonBox;

void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(331, 261);
layoutWidget = new QWidget(Dialog);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(10, 10, 311, 241));
verticalLayout_2 = new QVBoxLayout(layoutWidget);
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
verticalLayout_2->setContentsMargins(0, 0, 0, 0);
gridLayout = new QGridLayout();
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
label = new QLabel(layoutWidget);
label->setObjectName(QString::fromUtf8("label"));

gridLayout->addWidget(label, 0, 0, 1, 1);

verticalLayout = new QVBoxLayout();
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
decEdit = new QLineEdit(layoutWidget);
decEdit->setObjectName(QString::fromUtf8("decEdit"));

verticalLayout->addWidget(decEdit);

hexEdit = new QLineEdit(layoutWidget);
hexEdit->setObjectName(QString::fromUtf8("hexEdit"));

verticalLayout->addWidget(hexEdit);

binEdit = new QLineEdit(layoutWidget);
binEdit->setObjectName(QString::fromUtf8("binEdit"));

verticalLayout->addWidget(binEdit);


gridLayout->addLayout(verticalLayout, 0, 1, 3, 1);

label_2 = new QLabel(layoutWidget);
label_2->setObjectName(QString::fromUtf8("label_2"));

gridLayout->addWidget(label_2, 1, 0, 1, 1);

label_3 = new QLabel(layoutWidget);
label_3->setObjectName(QString::fromUtf8("label_3"));

gridLayout->addWidget(label_3, 2, 0, 1, 1);


verticalLayout_2->addLayout(gridLayout);

verticalSpacer = new QSpacerItem(20, 98, QSizePolicy::Minimum, QSizePolicy::Expanding);

verticalLayout_2->addItem(verticalSpacer);

horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalSpacer = new QSpacerItem(248, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

horizontalLayout->addItem(horizontalSpacer);

buttonBox = new QDialogButtonBox(layoutWidget);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Close);
buttonBox->setCenterButtons(false);

horizontalLayout->addWidget(buttonBox);


verticalLayout_2->addLayout(horizontalLayout);

#ifndef QT_NO_SHORTCUT
label->setBuddy(decEdit);
label_2->setBuddy(hexEdit);
label_3->setBuddy(binEdit);
#endif // QT_NO_SHORTCUT
QWidget::setTabOrder(decEdit, hexEdit);
QWidget::setTabOrder(hexEdit, binEdit);

retranslateUi(Dialog);
QObject::connect(buttonBox, SIGNAL(clicked(QAbstractButton*)), Dialog, SLOT(accept()));


QIntValidator* decValidator = new QIntValidator(0, 255, decEdit);
decEdit -> setValidator(decValidator);

QRegExpValidator* hexValidator = new QRegExpValidator(QRegExp ("[0-9A-Fa-f]{1,2}"), hexEdit);
hexEdit -> setValidator(hexValidator);

QRegExpValidator* binValidator = new QRegExpValidator(QRegExp("[01]{1,8}"), binEdit);
binEdit -> setValidator(binValidator);


QObject::connect(decEdit, SIGNAL(textEdited(QString)), Dialog, SLOT(decChanged(QString)));
QObject::connect(hexEdit, SIGNAL(textChanged(QString)), Dialog, SLOT(hexChanged(QString)));
QObject::connect(binEdit, SIGNAL(textChanged(QString)), Dialog, SLOT(binChanged(QString)));

QMetaObject::connectSlotsByName(Dialog);
} // setupUi

void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Byte Converter", 0, QApplication::UnicodeUTF8));
Dialog->setProperty("on_DecEdit_Changed", QVariant(QApplication::translate("Dialog", "0", 0, QApplication::UnicodeUTF8)));
label->setText(QApplication::translate("Dialog", "Dec", 0, QApplication::UnicodeUTF8));
binEdit->setProperty("binProbe", QVariant(QApplication::translate("Dialog", "asfad", 0, QApplication::UnicodeUTF8)));
label_2->setText(QApplication::translate("Dialog", "Hex", 0, QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("Dialog", "Bin", 0, QApplication::UnicodeUTF8));
} // retranslateUi

private slots:
// void decChanged (const QString&);
// void hexChanged (const QString&);
// void binChanged (const QString&);

void decChanged (const QString &newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if (ok) {
hexEdit->setText(QString::number(num, 16));
binEdit->setText(QString::number(num, 2));
} else {
hexEdit->setText("0");
binEdit->setText("0");
}
}

void hexChanged (const QString &newValue)
{
bool ok;
int num = newValue.toInt(&ok, 16);
if (ok) {
decEdit->setText(QString::number(num, 10));
binEdit->setText(QString::number(num, 2));
} else {
decEdit->setText("");
binEdit->setText("");
}
}

void binChanged (const QString &newValue)
{
bool ok;
int num = newValue.toInt(&ok, 2);
if (ok) {
decEdit->setText(QString::number(num, 10));
hexEdit->setText(QString::number(num, 16));
} else {
decEdit->setText("");
hexEdit->setText("");
}
}


/*void on_DecEdit_Changed ( const QString& newValue )
{
bool ok;
int num = newValue.toInt ( &ok );
if ( ok ) {
QLineEdit hexEdit ( QString::number ( num, 16 ) );
QLineEdit binChanged ( QString::number ( num, 2 ) );
} else {
QLineEdit hexChanged ( "" );
QLineEdit binChanged ( "" );
}
}*/
};

namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_BYTECONVERTERDIALOG_H


dialog.cpp:

#include "dialog.h"
#include "ui_byteconverterdialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}


main.cpp:

#include <QtGui/QApplication>
#include "dialog.h"
#include "ui_byteconverterdialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog dlg;
Ui_Dialog ui;
ui.setupUi(&dlg);
dlg.setAttribute(Qt::WA_QuitOnClose);
dlg.show();

return a.exec();
}


Please help.

wysota
10th May 2012, 23:21
My guess is you added the "Q_OBJECT" macro to an existing file and you didn't rerun qmake afterwards.

ChrisW67
11th May 2012, 00:52
My guess is more fundamental: The declaration of those slots in the Dialog class are commented out and there is no implementation of them at all. Since the parent class (QDialog) also has no slots of those names that you inherit you get the warnings when the Designer Ui code tries to connect.

willhelm
13th May 2012, 18:53
I thought i know enough to start qt, but i was wrong. But i want to finish this program. I've uncomment slots and tried with moving declaration to dialog.h and dialog.cpp. Now the problem is undefined reference to Dialog::decChanged(QString const&) and others slots in moc_dialog.cpp. Please give me instructions what to do and i will be back much more experienced...

wysota
13th May 2012, 22:21
Did you rerun qmake?

willhelm
14th May 2012, 09:02
Of course i did. Several times.

wysota
14th May 2012, 13:42
Is decChanged() a slot or a signal? How is it declared in the class?

MClark7
14th May 2012, 19:31
Hi,

I'm new to Qt and C++ so I'm asking questions about this problem to learn and test my knowledge. Unfortunately I don't have the "for sure" answer.

I'm not familiar with the book you are using. Does it have you modify the ui_... .h file?

I would try uncommenting the slot declarations in the dialog.h file, then add the slot functions to the dialog.cpp file e.g.


Dialog::void decChanged (const QString &newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if (ok) {
hexEdit->setText(QString::number(num, 16));
binEdit->setText(QString::number(num, 2));
} else {
hexEdit->setText("0");
binEdit->setText("0");
}
}