rdelgado
6th August 2010, 19:34
Hi,
I'm trying to make a simple drawing application and I am having some trouble.
My app have 1 drawingarea, 2 spinboxes, 2 buttons (one of the the quit button).
All I want to do is to start the program and show nothing in the drawing area. Then, using the spinboxes, set the X and Y coordinates of a circle. Then when I press the Action button, I want the circle to be displayed in the location given before.
I made the program buy what happens is that the circle is always drawing in the same position I give it initially. I don't know why it won't get the right position from the spinboxes before drawing.
I'll appreciate any help or suggestion.
I'll put the code of my program. Excuse me the words in spanish within the code.
This is the main.cpp file:
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include "spinvariable.h"
#include "areadibujo.h"
class MiWidget : public QWidget
{
public:
MiWidget(QWidget *parent = 0);
signals:
public slots:
};
MiWidget::MiWidget(QWidget *parent) :
QWidget(parent)
{
QPushButton *quit = new QPushButton("Quit");
QPushButton *accion = new QPushButton("Accion");
QObject::connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
spinvariable *spin1 = new spinvariable;
spinvariable *spin2 = new spinvariable;
spin1->setLabel("Valor X:");
spin1->setValue(10);
spin1->setRange(0, 100);
spin2->setLabel("Valor Y:");
spin2->setValue(20);
spin2->setRange(0, 100);
areadibujo *area = new areadibujo;
QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
QObject::connect(spin2, SIGNAL(valueChanged(int)), area, SLOT(setposicionY(int)));
QObject::connect(area, SIGNAL(cambiox(int)), spin1, SLOT(setValue(int)));
QObject::connect(area, SIGNAL(cambioy(int)), spin2, SLOT(setValue(int)));
QObject::connect(accion, SIGNAL(clicked()), area, SLOT(accion()));
QVBoxLayout *marcovertical = new QVBoxLayout;
QHBoxLayout *marcohorizontal = new QHBoxLayout;
marcovertical->addWidget(area);
marcovertical->addStretch();
marcohorizontal->addWidget(spin1);
marcohorizontal->addWidget(spin2);
marcohorizontal->addStretch();
marcovertical->addLayout(marcohorizontal);
marcovertical->addWidget(accion);
marcovertical->addWidget(quit);
setLayout(marcovertical);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MiWidget widget;
widget.setGeometry(0, 0, 500, 500);
widget.show();
return app.exec();
}
This is the spinvariable class:
spinvariable.h
#ifndef SPINVARIABLE_H
#define SPINVARIABLE_H
#include <QWidget>
class QSpinBox;
class QLabel;
class spinvariable : public QWidget
{
Q_OBJECT
public:
spinvariable(QWidget *parent = 0);
int value() const;
signals:
void valueChanged(int newValue);
public slots:
void setLabel(char *nombre);
void setValue(int value);
void setRange(int Rmin, int Rmax);
private:
QSpinBox *spinbox;
QLabel *label;
};
#endif // SPINVARIABLE_H
spinvariable.cpp
#include <QSpinBox>
#include <QLabel>
#include <QHBoxLayout>
#include "spinvariable.h"
spinvariable::spinvariable(QWidget *parent) :
QWidget(parent)
{
spinbox = new QSpinBox;
label = new QLabel;
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(spinbox);
setLayout(layout);
}
void spinvariable::setValue(int value)
{
spinbox->setValue(value);
}
void spinvariable::setRange(int Rmin, int Rmax)
{
spinbox->setRange(Rmin, Rmax);
}
int spinvariable::value() const
{
return spinbox->value();
}
void spinvariable::setLabel(char *nombre)
{
label->setText(nombre);
}
This is the areadibujo class (drawingarea):
areadibujo.h
#ifndef AREADIBUJO_H
#define AREADIBUJO_H
#include <QWidget>
class areadibujo : public QWidget
{
Q_OBJECT
public:
areadibujo(QWidget *parent = 0);
signals:
void cambiox(int nuevox);
void cambioy(int nuevoy);
public slots:
void accion();
void setposicionX(int x);
void setposicionY(int y);
protected:
void paintEvent(QPaintEvent *event);
private:
void pintaCirculo(QPainter &painter);
int apretar;
int posicionX, posicionY;
};
#endif // AREADIBUJO_H
areadibujo.cpp
#include <QPaintEvent>
#include <QPainter>
#include "areadibujo.h"
areadibujo::areadibujo(QWidget *parent) :
QWidget(parent)
{
setPalette(QPalette(QColor(255, 255, 255)));
setAutoFillBackground(true);
setFixedSize(500, 450);
posicionX=10;
posicionY=20;
apretar=0;
}
void areadibujo::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(apretar==1)
{
pintaCirculo(painter);
}
}
void areadibujo::pintaCirculo(QPainter &painter)
{
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawEllipse(posicionX,posicionY,10,10);
}
void areadibujo::accion()
{
update();
apretar=1;
}
void areadibujo::setposicionX(int x)
{
posicionX=x;
emit cambiox(posicionX);
}
void areadibujo::setposicionY(int y)
{
posicionY=y;
emit cambioy(posicionY);
}
I can't see how to make it work.
I am using the cannonball Qt tutorial as an example.
Thanks you.
R. D.
I'm trying to make a simple drawing application and I am having some trouble.
My app have 1 drawingarea, 2 spinboxes, 2 buttons (one of the the quit button).
All I want to do is to start the program and show nothing in the drawing area. Then, using the spinboxes, set the X and Y coordinates of a circle. Then when I press the Action button, I want the circle to be displayed in the location given before.
I made the program buy what happens is that the circle is always drawing in the same position I give it initially. I don't know why it won't get the right position from the spinboxes before drawing.
I'll appreciate any help or suggestion.
I'll put the code of my program. Excuse me the words in spanish within the code.
This is the main.cpp file:
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include "spinvariable.h"
#include "areadibujo.h"
class MiWidget : public QWidget
{
public:
MiWidget(QWidget *parent = 0);
signals:
public slots:
};
MiWidget::MiWidget(QWidget *parent) :
QWidget(parent)
{
QPushButton *quit = new QPushButton("Quit");
QPushButton *accion = new QPushButton("Accion");
QObject::connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
spinvariable *spin1 = new spinvariable;
spinvariable *spin2 = new spinvariable;
spin1->setLabel("Valor X:");
spin1->setValue(10);
spin1->setRange(0, 100);
spin2->setLabel("Valor Y:");
spin2->setValue(20);
spin2->setRange(0, 100);
areadibujo *area = new areadibujo;
QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
QObject::connect(spin2, SIGNAL(valueChanged(int)), area, SLOT(setposicionY(int)));
QObject::connect(area, SIGNAL(cambiox(int)), spin1, SLOT(setValue(int)));
QObject::connect(area, SIGNAL(cambioy(int)), spin2, SLOT(setValue(int)));
QObject::connect(accion, SIGNAL(clicked()), area, SLOT(accion()));
QVBoxLayout *marcovertical = new QVBoxLayout;
QHBoxLayout *marcohorizontal = new QHBoxLayout;
marcovertical->addWidget(area);
marcovertical->addStretch();
marcohorizontal->addWidget(spin1);
marcohorizontal->addWidget(spin2);
marcohorizontal->addStretch();
marcovertical->addLayout(marcohorizontal);
marcovertical->addWidget(accion);
marcovertical->addWidget(quit);
setLayout(marcovertical);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MiWidget widget;
widget.setGeometry(0, 0, 500, 500);
widget.show();
return app.exec();
}
This is the spinvariable class:
spinvariable.h
#ifndef SPINVARIABLE_H
#define SPINVARIABLE_H
#include <QWidget>
class QSpinBox;
class QLabel;
class spinvariable : public QWidget
{
Q_OBJECT
public:
spinvariable(QWidget *parent = 0);
int value() const;
signals:
void valueChanged(int newValue);
public slots:
void setLabel(char *nombre);
void setValue(int value);
void setRange(int Rmin, int Rmax);
private:
QSpinBox *spinbox;
QLabel *label;
};
#endif // SPINVARIABLE_H
spinvariable.cpp
#include <QSpinBox>
#include <QLabel>
#include <QHBoxLayout>
#include "spinvariable.h"
spinvariable::spinvariable(QWidget *parent) :
QWidget(parent)
{
spinbox = new QSpinBox;
label = new QLabel;
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(spinbox);
setLayout(layout);
}
void spinvariable::setValue(int value)
{
spinbox->setValue(value);
}
void spinvariable::setRange(int Rmin, int Rmax)
{
spinbox->setRange(Rmin, Rmax);
}
int spinvariable::value() const
{
return spinbox->value();
}
void spinvariable::setLabel(char *nombre)
{
label->setText(nombre);
}
This is the areadibujo class (drawingarea):
areadibujo.h
#ifndef AREADIBUJO_H
#define AREADIBUJO_H
#include <QWidget>
class areadibujo : public QWidget
{
Q_OBJECT
public:
areadibujo(QWidget *parent = 0);
signals:
void cambiox(int nuevox);
void cambioy(int nuevoy);
public slots:
void accion();
void setposicionX(int x);
void setposicionY(int y);
protected:
void paintEvent(QPaintEvent *event);
private:
void pintaCirculo(QPainter &painter);
int apretar;
int posicionX, posicionY;
};
#endif // AREADIBUJO_H
areadibujo.cpp
#include <QPaintEvent>
#include <QPainter>
#include "areadibujo.h"
areadibujo::areadibujo(QWidget *parent) :
QWidget(parent)
{
setPalette(QPalette(QColor(255, 255, 255)));
setAutoFillBackground(true);
setFixedSize(500, 450);
posicionX=10;
posicionY=20;
apretar=0;
}
void areadibujo::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(apretar==1)
{
pintaCirculo(painter);
}
}
void areadibujo::pintaCirculo(QPainter &painter)
{
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawEllipse(posicionX,posicionY,10,10);
}
void areadibujo::accion()
{
update();
apretar=1;
}
void areadibujo::setposicionX(int x)
{
posicionX=x;
emit cambiox(posicionX);
}
void areadibujo::setposicionY(int y)
{
posicionY=y;
emit cambioy(posicionY);
}
I can't see how to make it work.
I am using the cannonball Qt tutorial as an example.
Thanks you.
R. D.