Results 1 to 6 of 6

Thread: trouble with signals and slots

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Posts
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default trouble with signals and slots

    Hi I am new to Qt and I can't figure out how to get a signal and slot to work. I am trying to set a object called f1 of type Fraction to values in a QDoubleSpinBox called num when the button setbutton is clicked. This is main:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui>
    3. #include "mainwindow.h"
    4. #include "fraction.h"
    5. #include <windows.h>
    6. #include <QObject>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QTextStream cout(stdout);
    11. QApplication a(argc, argv);
    12. QWidget *window = new QWidget;
    13. QLabel* first = new QLabel("Enter a fraction");
    14. QPushButton* setbutton = new QPushButton ("Set Fraction");
    15. box->addButton(QDialogButtonBox::Ok);
    16. box->addButton(QDialogButtonBox::Cancel);
    17. QVBoxLayout* buttonlayout = new QVBoxLayout;
    18. buttonlayout->addWidget(first);
    19. buttonlayout-> addWidget(num);
    20. buttonlayout-> addWidget(setbutton);
    21. buttonlayout->addWidget(box);
    22. window->setLayout(buttonlayout);
    23. window->show();
    24. Fraction f1, f2;
    25. QObject::connect(setbutton,SIGNAL(clicked()),num,SLOT(set(int nn, int nd)));
    26.  
    27. //f1.set(1,7);
    28. //f2.set(11,12);
    29. //cout << "The first fraction is: " << f1.toString() << endl;
    30. //cout << "\nThe second fraction, as a double is: " << f2.toDouble() << endl;
    31. //cout.flush();
    32. //Sleep(10000);
    33. //return 0;
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

    here is fraction.h:
    Qt Code:
    1. #ifndef FRACTION_H
    2. #define FRACTION_H
    3. #include <QString>
    4. #include <QObject>
    5.  
    6. class Fraction: public QObject {
    7. Q_OBJECT
    8. public slots:
    9. void set(int nn, int nd);
    10. public:
    11. Fraction();
    12. Fraction(int nn, int nd);
    13. QString toString() const;
    14. double toDouble() const;
    15. Fraction add(const Fraction& other);
    16. Fraction subtract(const Fraction& other);
    17. Fraction multiply(const Fraction& other);
    18. Fraction divide(const Fraction& other);
    19.  
    20. private:
    21. int m_Numerator;
    22. int m_Denominator;
    23. };
    24.  
    25.  
    26. #endif // FRACTION_H
    To copy to clipboard, switch view to plain text mode 

    this is the error I get when I try to run:
    'QObject::QOject(const QObject&)' is private

  2. The following user says thank you to Dmon for this useful post:


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

    Default Re: trouble with signals and slots

    You are trying to copy an instance of your Fraction object in lines 15-18 of your last snippet.
    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.


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


  5. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: trouble with signals and slots

    Qt Code:
    1. QObject::connect(setbutton,SIGNAL(clicked()),num,SLOT(set(int nn, int nd)));
    To copy to clipboard, switch view to plain text mode 
    This is not legal in Qt. The slot should have less than or equal number of arguments as signal.

    Qt Code:
    1. Fraction add(const Fraction& other);
    2. Fraction subtract(const Fraction& other);
    3. Fraction multiply(const Fraction& other);
    4. Fraction divide(const Fraction& other);
    To copy to clipboard, switch view to plain text mode 
    All these function declaration are not possible int Qt, because Fraction is derived QObject, instead use somthing like
    Qt Code:
    1. void add(const Fraction& other);
    2. void subtract(const Fraction& other);
    3. void multiply(const Fraction& other);
    4. void divide(const Fraction& other);
    To copy to clipboard, switch view to plain text mode 

    Also typically all QObject derived class objects follow parent-child relationships, i.e the parent is passed during QObject construction. The best way to define Fraction class would be to (If you have decided to make it QObject)
    Qt Code:
    1. class Fraction: public QObject {
    2. Q_OBJECT
    3. public:
    4. Fraction(int nn, int nd, QObject * parent = 0);
    5. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:


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

    Default Re: trouble with signals and slots

    In my opinion it makes completely no sense to derive Fraction from QObject.
    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. The following user says thank you to wysota for this useful post:


  9. #5
    Join Date
    Feb 2013
    Posts
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: trouble with signals and slots

    Thanks for the help. So what you are saying is that the SLOT needs to have no variables because the SIGNAL has none?

  10. The following user says thank you to Dmon for this useful post:


  11. #6
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: trouble with signals and slots

    The arguments in Slot and signal has to be same or at least the slot can have less arguments than signal(but the type of argument and ordering is mandatory)

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


Similar Threads

  1. QT SIGNALS and SLOTS
    By beginQT in forum Newbie
    Replies: 7
    Last Post: 23rd September 2011, 14:40
  2. Signals & Slots!
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2010, 01:50
  3. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  4. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49
  5. trouble with signals and slots
    By therealjag in forum Newbie
    Replies: 4
    Last Post: 23rd February 2006, 21:52

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.