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
#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