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:
Code:
#include <QtGui/QApplication>
#include <QtGui>
#include "mainwindow.h"
#include "fraction.h"
#include <windows.h>
#include <QObject>
int main(int argc, char *argv[])
{
buttonlayout->addWidget(first);
buttonlayout-> addWidget(num);
buttonlayout-> addWidget(setbutton);
buttonlayout->addWidget(box);
window->setLayout(buttonlayout);
window->show();
Fraction f1, f2;
QObject::connect(setbutton,
SIGNAL(clicked
()),num,
SLOT(set
(int nn,
int nd
)));
//f1.set(1,7);
//f2.set(11,12);
//cout << "The first fraction is: " << f1.toString() << endl;
//cout << "\nThe second fraction, as a double is: " << f2.toDouble() << endl;
//cout.flush();
//Sleep(10000);
//return 0;
return a.exec();
}
here is fraction.h:
Code:
#ifndef FRACTION_H
#define FRACTION_H
#include <QString>
#include <QObject>
Q_OBJECT
public slots:
void set(int nn, int nd);
public:
Fraction();
Fraction(int nn, int nd);
double toDouble() const;
Fraction add(const Fraction& other);
Fraction subtract(const Fraction& other);
Fraction multiply(const Fraction& other);
Fraction divide(const Fraction& other);
private:
int m_Numerator;
int m_Denominator;
};
#endif // FRACTION_H
this is the error I get when I try to run:
'QObject::QOject(const QObject&)' is private
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.
Re: trouble with signals and slots
Code:
QObject::connect(setbutton,
SIGNAL(clicked
()),num,
SLOT(set
(int nn,
int nd
)));
This is not legal in Qt. The slot should have less than or equal number of arguments as signal.
Code:
Fraction add(const Fraction& other);
Fraction subtract(const Fraction& other);
Fraction multiply(const Fraction& other);
Fraction divide(const Fraction& other);
All these function declaration are not possible int Qt, because Fraction is derived QObject, instead use somthing like
Code:
void add(const Fraction& other);
void subtract(const Fraction& other);
void multiply(const Fraction& other);
void divide(const Fraction& other);
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)
Code:
Q_OBJECT
public:
Fraction
(int nn,
int nd,
QObject * parent
= 0);
}
Re: trouble with signals and slots
In my opinion it makes completely no sense to derive Fraction from QObject.
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?
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)