Results 1 to 6 of 6

Thread: trouble with signals and slots

Threaded 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:


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.