Results 1 to 4 of 4

Thread: another problem with QObject::connect()

  1. #1
    Join Date
    Jun 2017
    Location
    Damascus-Syria
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default another problem with QObject::connect()

    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:
    Qt Code:
    1. #ifndef NSC_H
    2. #define NSC_H
    3.  
    4. #include <QWidget>
    5. #include <QMainWindow>
    6. #include <Qt>
    7. #include <QLabel>
    8. #include <QLineEdit>
    9. #include <QCommandLinkButton>
    10. #include <QScrollArea>
    11. #include <QVBoxLayout>
    12.  
    13. namespace Ui {
    14. class NSC;
    15. }
    16.  
    17. class NSC : public QWidget
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit NSC(QWidget *parent = 0);
    23. ~NSC();
    24.  
    25. QLabel *title;
    26. QLineEdit *decimal_entry;
    27. QCommandLinkButton *dec2bin;
    28. QCommandLinkButton *dec2oct;
    29. QCommandLinkButton *dec2hex;
    30. QLabel *result;
    31. QLabel *footer;
    32. QVBoxLayout *layout;
    33. QWidget *mainWindow;
    34.  
    35. public slots:
    36. void __dec2bin_();
    37.  
    38. private:
    39. Ui::NSC *ui;
    40. };
    41.  
    42. #endif // NSC_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include "nsc.h"
    2. #include <QApplication>
    3. #include <QWidget>
    4. #include <QMainWindow>
    5. #include <Qt>
    6. #include <QLabel>
    7. #include <QLineEdit>
    8. #include <QCommandLinkButton>
    9. #include <QScrollArea>
    10. #include <QVBoxLayout>
    11.  
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication a(argc, argv);
    16.  
    17. QLabel *title=new QLabel;
    18. title->setGeometry(0,0,400,30);
    19. title->setStyleSheet("font-size:20px;");
    20. title->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    21. title->setText("Numerical Systems Converter");
    22.  
    23. QLineEdit *decimal_entry=new QLineEdit;
    24. decimal_entry->setGeometry(0,50,400,30);
    25. decimal_entry->setClearButtonEnabled(true);
    26. decimal_entry->setStyleSheet("font-size:20px;");
    27. decimal_entry->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    28. decimal_entry->setPlaceholderText("Enter Your Decimal Number Here...");
    29.  
    30. QCommandLinkButton *dec2bin=new QCommandLinkButton;
    31. dec2bin->setGeometry(0,90,400,50);
    32. dec2bin->setStyleSheet("font-size:20px;");
    33. dec2bin->setText("Convert To Binary");
    34.  
    35. QLabel *result=new QLabel;
    36. result->setGeometry(0,250,400,30);
    37. result->setStyleSheet("font-size:20px;");
    38. result->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    39. result->setText("The Result Should Appear Here...");
    40.  
    41. QLabel *footer=new QLabel;
    42. footer->setGeometry(0,300,400,80);
    43. footer->setStyleSheet("font-size:20px;");
    44. footer->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    45. footer->setText("Designed And Programmed By:<br> <a href='http://www.facebook.com/eyad.syrialover'>Syrian Lucianos</a>");
    46. footer->setTextFormat(Qt::RichText);
    47. footer->setTextInteractionFlags(Qt::TextBrowserInteraction);
    48. footer->setOpenExternalLinks(true);
    49.  
    50. QVBoxLayout *layout=new QVBoxLayout;
    51. layout->addWidget(title);
    52. layout->addWidget(decimal_entry);
    53. layout->addWidget(dec2bin);
    54. layout->addWidget(result);
    55. layout->addWidget(footer);
    56.  
    57. QWidget *mainWindow=new QWidget;
    58. mainWindow->setLayout(layout);
    59. mainWindow->setFixedSize(400,400);
    60.  
    61. QObject::connect(dec2bin,SIGNAL(clicked()),result,SLOT(__dec2bin_()));
    62.  
    63. mainWindow->show();
    64.  
    65. return a.exec();
    66. }
    67.  
    68. void NSC::__dec2bin_() {
    69. QString dec2bin_result="";
    70. for (int i=(decimal_entry->text()).toInt();i>0;i=i/2) {
    71. dec2bin_result.prepend(QString::number(i%2));
    72. }
    73. result->setText(dec2bin_result);
    74. }
    To copy to clipboard, switch view to plain text mode 

    nsc.cpp:
    Qt Code:
    1. #include "nsc.h"
    2. #include "ui_nsc.h"
    3.  
    4. NSC::NSC(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::NSC)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. NSC::~NSC()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: another problem with QObject::connect()

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2017
    Location
    Damascus-Syria
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: another problem with QObject::connect()

    Quote Originally Posted by d_stranz View Post
    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: another problem with QObject::connect()

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 27th June 2014, 16:29
  2. Problem with the Slot in QObject::connect(...)
    By d'Matthias in forum Qt Programming
    Replies: 14
    Last Post: 4th June 2011, 07:46
  3. [Help] QObject::connect
    By vinny gracindo in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 13:26
  4. Problem in QObject::connect( ... ) with smart_ptr ( Boost )
    By kunalnandi in forum General Programming
    Replies: 1
    Last Post: 15th October 2008, 07:44
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.