PDA

View Full Version : another problem with QObject::connect()



Syrian Lucianos
28th June 2017, 22:01
hello everyone...
i've got a QWidget application to convert from decimal to binary.
i just don't understand why this error keeps appearing in the console:
QObject::connect: No such slot QLabel::__dec2bin_()
why is this happening?
here's all the contents (the project is called NSC):

nsc.h:


#ifndef NSC_H
#define NSC_H

#include <QWidget>
#include <QMainWindow>
#include <Qt>
#include <QLabel>
#include <QLineEdit>
#include <QCommandLinkButton>
#include <QScrollArea>
#include <QVBoxLayout>

namespace Ui {
class NSC;
}

class NSC : public QWidget
{
Q_OBJECT

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

QLabel *title;
QLineEdit *decimal_entry;
QCommandLinkButton *dec2bin;
QCommandLinkButton *dec2oct;
QCommandLinkButton *dec2hex;
QLabel *result;
QLabel *footer;
QVBoxLayout *layout;
QWidget *mainWindow;

public slots:
void __dec2bin_();

private:
Ui::NSC *ui;
};

#endif // NSC_H


main.cpp:


#include "nsc.h"
#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <Qt>
#include <QLabel>
#include <QLineEdit>
#include <QCommandLinkButton>
#include <QScrollArea>
#include <QVBoxLayout>


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

QLabel *title=new QLabel;
title->setGeometry(0,0,400,30);
title->setStyleSheet("font-size:20px;");
title->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
title->setText("Numerical Systems Converter");

QLineEdit *decimal_entry=new QLineEdit;
decimal_entry->setGeometry(0,50,400,30);
decimal_entry->setClearButtonEnabled(true);
decimal_entry->setStyleSheet("font-size:20px;");
decimal_entry->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
decimal_entry->setPlaceholderText("Enter Your Decimal Number Here...");

QCommandLinkButton *dec2bin=new QCommandLinkButton;
dec2bin->setGeometry(0,90,400,50);
dec2bin->setStyleSheet("font-size:20px;");
dec2bin->setText("Convert To Binary");

QLabel *result=new QLabel;
result->setGeometry(0,250,400,30);
result->setStyleSheet("font-size:20px;");
result->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
result->setText("The Result Should Appear Here...");

QLabel *footer=new QLabel;
footer->setGeometry(0,300,400,80);
footer->setStyleSheet("font-size:20px;");
footer->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
footer->setText("Designed And Programmed By:<br> <a href='http://www.facebook.com/eyad.syrialover'>Syrian Lucianos</a>");
footer->setTextFormat(Qt::RichText);
footer->setTextInteractionFlags(Qt::TextBrowserInteraction );
footer->setOpenExternalLinks(true);

QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(title);
layout->addWidget(decimal_entry);
layout->addWidget(dec2bin);
layout->addWidget(result);
layout->addWidget(footer);

QWidget *mainWindow=new QWidget;
mainWindow->setLayout(layout);
mainWindow->setFixedSize(400,400);

QObject::connect(dec2bin,SIGNAL(clicked()),result, SLOT(__dec2bin_()));

mainWindow->show();

return a.exec();
}

void NSC::__dec2bin_() {
QString dec2bin_result="";
for (int i=(decimal_entry->text()).toInt();i>0;i=i/2) {
dec2bin_result.prepend(QString::number(i%2));
}
result->setText(dec2bin_result);
}


nsc.cpp:


#include "nsc.h"
#include "ui_nsc.h"

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

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


thanks in advance

d_stranz
28th June 2017, 23:20
why is this happening?

Learn how to read and understand what the compiler or runtime is telling you: QLabel doesn't have a slot named _dec2bin_(). So if QLabel doesn't have this slot, what does? Your NSC class maybe?. So you don't use "result" (a QLabel instance) in the connect() call, you use "this".

And by the way, it is usually very dangerous to use method and variable names that begin with one or two underscores. Often the runtime libraries use names for macros that begin with single or double underscores, and if your name happens to match a macro, then all kinds of weird things happen when the preprocessor substitutes the macro for your variable or method name. Don't do it.

Syrian Lucianos
29th June 2017, 15:19
Learn how to read and understand what the compiler or runtime is telling you: QLabel doesn't have a slot named _dec2bin_(). So if QLabel doesn't have this slot, what does? Your NSC class maybe?. So you don't use "result" (a QLabel instance) in the connect() call, you use "this".
whenever i use "this" i get the following error:
'this' : can only be referenced inside non-static member functions or non-static data member initializers

d_stranz
29th June 2017, 18:07
In order for your NSC class to be able to handle this signal, you have to have an instance of it. Simply including the header file doesn't create an instance, it only tells the compiler that, "Hey, there's a class called NSC."

So I don't see anywhere in the code that you've posted that you are creating an NSC instance. That's the pointer you need to be passing into the connect() call. I misread your code, and assumed all of that was taking place inside the NSC constructor.

If the NSC class is meant to be the main window of your app, then that is what you should be using instead of the generic QWidget you create in line 57 of main.cpp. And all the code in main.cpp that creates the labels and other widgets should be in the NSC constructor.