Results 1 to 20 of 20

Thread: Problem with slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with slot

    Hi!
    I'm trying to develop a simple calculator. I'm using Qt Creator. I've already designed a form, but now I have a problem with slot. It calls, when i push the button with digit and adds the digit to lcd. I think everything is ok, but i get compiler's errors. Here is the source code:
    Qt Code:
    1. //calc.h
    2.  
    3. #ifndef CALC_H
    4. #define CALC_H
    5.  
    6. #include <QtGui/QDialog>
    7.  
    8. namespace Ui
    9. {
    10. class Calc;
    11. }
    12.  
    13. class Calc : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Calc(QWidget *parent = 0);
    19. ~Calc();
    20.  
    21. slots:
    22. void setVal(const int &val);
    23.  
    24. private:
    25. Ui::Calc *ui;
    26. };
    27.  
    28. #endif // CALC_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //calc.cpp
    2. #include "calc.h"
    3. #include "ui_calc.h"
    4.  
    5. Calc::Calc(QWidget *parent)
    6. : QDialog(parent), ui(new Ui::Calc)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Calc::~Calc()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Calc::setVal(const int &val){
    17.  
    18. lcdNumber-> intValue*=10;
    19. lcdNumber -> intValue+=val;
    20. }
    To copy to clipboard, switch view to plain text mode 


    And these are the errors:
    calc.h:16: error: expected primary-expression before ‘void’
    calc.h:16: error: ISO C++ forbids declaration of ‘type name’ with no type
    calc.h:16: error: expected ‘;’ before ‘void’
    calc.cpp:11: error: no ‘void Calc::setVal(const int&)’ member function declared in class ‘Calc’
    Please help me
    Regards
    Stefek

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with slot

    It should say "public slots:" and not "slots:".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Thanks, this problem is solved, but now come other problems.
    calc.h:18: error: ISO C++ forbids declaration of ‘QLCDNumber’ with no type
    calc.h:18: error: expected ‘;’ before ‘*’ token
    So I added the declaration of QLCDNumber class: class QLCDNumber;. and now are problems with the inside of the slot:
    calc.cpp:17: error: invalid use of member (did you forget the ‘&’ ?)
    How can I solve it?

    I think i solve this problem. I add to class Calc new variable int value, which stores the actual result of calculations and use display function.

    But... I have new problem...
    Here is my source code:
    Qt Code:
    1. //calc.h
    2.  
    3. #ifndef CALC_H
    4. #define CALC_H
    5.  
    6. #include <QtGui/QDialog>
    7.  
    8. class QLCDNumber;
    9. namespace Ui
    10. {
    11. class Calc;
    12. }
    13.  
    14. class Calc : public QDialog
    15. {
    16. Q_OBJECT
    17.  
    18. private:
    19. QLCDNumber *lcdNumber;
    20. QPushButton *seventhButton;
    21. QPushButton *eighthButton;
    22. QPushButton *ninethButton;
    23. QPushButton *divisionButton;
    24. QPushButton *fourthButton;
    25. QPushButton *fifthButton;
    26. QPushButton *sixthButton;
    27. QPushButton *additionButton;
    28. QPushButton *firstButton;
    29. QPushButton *secondButton;
    30. QPushButton *thirdButton;
    31. QPushButton *subtractionButton;
    32. QPushButton *zeroButton;
    33. QPushButton *sqrtButton;
    34. QPushButton *multiplicationButton;
    35. QPushButton *moduloButton;
    36. int value;
    37.  
    38. public:
    39. Calc(QWidget *parent = 0);
    40. ~Calc();
    41.  
    42. public slots:
    43. void setVal(const int &val);
    44.  
    45.  
    46.  
    47. private:
    48. Ui::Calc *ui;
    49. };
    50.  
    51. #endif // CALC_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "calc.h"
    2. #include "ui_calc.h"
    3.  
    4. Calc::Calc(QWidget *parent)
    5. : QDialog(parent), ui(new Ui::Calc)
    6. {
    7. ui->setupUi(this);
    8. connect(zeroButton, SIGNAL(clicked()), this, SLOT(setVal(0)));
    9. }
    10.  
    11. Calc::~Calc()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Calc::setVal(const int &val){
    17.  
    18. value*=10;
    19. value+=val;
    20. lcdNumber ->display(value);
    21. }
    To copy to clipboard, switch view to plain text mode 

    In the constructor I add the conection between zero button and LCDNumber. It should works, but in the console are errors:
    Segmentation fault
    I have no idea how to do this. Could anyone help me?
    It's harder than I thought at the beginning of lessons.

    I've read in documentation that the signal and slot parameters mustn't contain variable name, but only type, so how can I send the value, which is needed?
    Last edited by stefek; 28th February 2009 at 16:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with slot

    Your original problem simply required including <QLCDNumber>. Your current problem is that you didn't create an object behind the zeroButton variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    u dont have to send any value from the slot..remove 0 from setVal(0) and it should work..the signal will IMPLICITLY send the data as required by the slot...in the case of clicked(bool), if the button is clicked, 'true' is the value sent, otherwise 'false' is sent

  6. #6
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    I've added in main function,after the Calc constructor, new procedure "connection" that connects signals with slots. The effect is the same = segfault.

    Yes, there was error,but now is:
    Qt Code:
    1. connect(this->zeroButton, SIGNAL(clicked(bool)), this, SLOT(setVal(const int &)));
    To copy to clipboard, switch view to plain text mode 
    And still is segfault.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with slot

    Did you initialize the zeroButton variable to anything?

    By the way, please read about signal-slot connections in the reference. You can't connect a bool signal to an int slot, it wouldn't make sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    I've read this, but I can't write anything that could work. Is it really hard or do I make it more difficult than it is? :/
    I think that actually the whole calculator is too difficult for me. I'll try to make simple dialog with one QPushButton and QLCDNumber.
    I've written a function, which sets value to qlcdnumber:
    Qt Code:
    1. void changeValue(const int &val){
    2.  
    3. lcdNumber->display(val);
    4. }
    To copy to clipboard, switch view to plain text mode 
    But no matter how I call this function I get segfaults. I don't know why.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with slot

    I will repeat this question the last time - did you initialize your variables? Do you have any knowledge of C++ at all? Do you understand the difference between a pointer and an object? You are getting a segmentation fault because you try to dereference an uninitialized pointer. The control flow doesn't even reach the connect statement because it segfaults a while earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Here is actual source code:
    Qt Code:
    1. //dialog.cpp
    2. #include "dialog.h"
    3. #include "ui_dialog.h"
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. : QDialog(parent), ui(new Ui::DialogClass)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11.  
    12. Dialog::~Dialog()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Dialog::changeValue(const int &val){
    18.  
    19. lcdNumber->display(val);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //dialog.h
    2. #ifndef DIALOG_H
    3. #define DIALOG_H
    4.  
    5. #include <QtGui/QDialog>
    6. class QLCDNumber;
    7. namespace Ui
    8. {
    9. class DialogClass;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19.  
    20. public slots:
    21. void changeValue(const int &val);
    22.  
    23. signals:
    24. void onClick(const int &val);
    25.  
    26.  
    27. private:
    28. QLCDNumber *lcdNumber;
    29. Ui::DialogClass *ui;
    30. };
    31.  
    32. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //ui_dialog.h
    2. #ifndef UI_DIALOG_H
    3. #define UI_DIALOG_H
    4.  
    5. #include <QtCore/QVariant>
    6. #include <QtGui/QAction>
    7. #include <QtGui/QApplication>
    8. #include <QtGui/QButtonGroup>
    9. #include <QtGui/QDialog>
    10. #include <QtGui/QLCDNumber>
    11. #include <QtGui/QPushButton>
    12.  
    13. QT_BEGIN_NAMESPACE
    14.  
    15. class Ui_DialogClass
    16. {
    17. public:
    18. QLCDNumber *lcdNumber;
    19. QPushButton *pushButton;
    20.  
    21. void setupUi(QDialog *DialogClass)
    22. {
    23. if (DialogClass->objectName().isEmpty())
    24. DialogClass->setObjectName(QString::fromUtf8("DialogClass"));
    25. DialogClass->resize(209, 62);
    26. lcdNumber = new QLCDNumber(DialogClass);
    27. lcdNumber->setObjectName(QString::fromUtf8("lcdNumber"));
    28. lcdNumber->setGeometry(QRect(130, 20, 64, 23));
    29. pushButton = new QPushButton(DialogClass);
    30. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    31. pushButton->setGeometry(QRect(10, 20, 105, 25));
    32. pushButton->setProperty("value", QVariant(0));
    33.  
    34. retranslateUi(DialogClass);
    35.  
    36. QMetaObject::connectSlotsByName(DialogClass);
    37. } // setupUi
    38.  
    39. void retranslateUi(QDialog *DialogClass)
    40. {
    41. DialogClass->setWindowTitle(QApplication::translate("DialogClass", "Dialog", 0, QApplication::UnicodeUTF8));
    42. pushButton->setText(QApplication::translate("DialogClass", "0", 0, QApplication::UnicodeUTF8));
    43. Q_UNUSED(DialogClass);
    44. } // retranslateUi
    45.  
    46. };
    47.  
    48. namespace Ui {
    49. class DialogClass: public Ui_DialogClass {};
    50. } // namespace Ui
    51.  
    52. QT_END_NAMESPACE
    53.  
    54. #endif // UI_DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.cpp
    2. #include <QtGui/QApplication>
    3. #include "dialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. Dialog w;
    9. w.show();
    10.  
    11.  
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I'm newbie in Qt and probably not C++ pro, but I want to learn and improve my skills.No one are the best at the beginning. Could you explain me how can I initialize variables? I'm in the brain dead.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with slot

    Your problem is strictly related to C++. You have two lcdNumber member variables from different levels of inheritance and you use the one that is uninitialized. Get rid of lcdNumber from your Dialog class and use the one in ui.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    stefek (1st March 2009)

  13. #12
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: Problem with slot

    I agree with wysota!

    stefek, rewrite your code as follows:

    Qt Code:
    1. void Dialog::changeValue(const int &val)
    2. {
    3. ui->lcdNumber->display(val);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Also you should remove QLCDNumber *lcdNumber member from Dialog class declaration.

  14. The following user says thank you to pastor for this useful post:

    stefek (1st March 2009)

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Replies: 12
    Last Post: 18th September 2008, 15:04
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  5. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45

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
  •  
Qt is a trademark of The Qt Company.