PDA

View Full Version : trouble with signals and slots



Dmon
25th February 2013, 21:15
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:


#include <QtGui/QApplication>
#include <QtGui>
#include "mainwindow.h"
#include "fraction.h"
#include <windows.h>
#include <QObject>

int main(int argc, char *argv[])
{
QTextStream cout(stdout);
QApplication a(argc, argv);
QWidget *window = new QWidget;
QLabel* first = new QLabel("Enter a fraction");
QDoubleSpinBox* num = new QDoubleSpinBox;
QPushButton* setbutton = new QPushButton ("Set Fraction");
QDialogButtonBox* box = new QDialogButtonBox;
box->addButton(QDialogButtonBox::Ok);
box->addButton(QDialogButtonBox::Cancel);
QVBoxLayout* buttonlayout = new QVBoxLayout;
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,S LOT(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:


#ifndef FRACTION_H
#define FRACTION_H
#include <QString>
#include <QObject>

class Fraction: public QObject {
Q_OBJECT
public slots:
void set(int nn, int nd);
public:
Fraction();
Fraction(int nn, int nd);
QString toString() const;
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

wysota
25th February 2013, 21:40
You are trying to copy an instance of your Fraction object in lines 15-18 of your last snippet.

Santosh Reddy
25th February 2013, 21:50
QObject::connect(setbutton,SIGNAL(clicked()),num,S LOT(set(int nn, int nd)));
This is not legal in Qt. The slot should have less than or equal number of arguments as signal.


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

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)


class Fraction: public QObject {
Q_OBJECT
public:
Fraction(int nn, int nd, QObject * parent = 0);
}

wysota
25th February 2013, 21:52
In my opinion it makes completely no sense to derive Fraction from QObject.

Dmon
26th February 2013, 18:40
Thanks for the help. So what you are saying is that the SLOT needs to have no variables because the SIGNAL has none?

alizadeh91
26th February 2013, 19:34
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)