Hi,
Maybe this can help you. This is a test program I made. It draws a circle on screen following spinbuttons coordinates:
main.cpp
#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpinBox>
#include "areadibujo.h"
{
public:
};
{
AreaDibujo *area = new AreaDibujo;
valorX->setRange(0, 1000);
valorY->setRange(0, 1000);
valorX->setValue(100);
valorY->setValue(100);
connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(dibujar, SIGNAL(clicked()), area, SLOT(Dibuja()));
connect(valorX, SIGNAL(valueChanged(int)), area, SLOT(ColocaX(int)));
connect(valorY, SIGNAL(valueChanged(int)), area, SLOT(ColocaY(int)));
valores->addWidget(valorX);
valores->addWidget(valorY);
dibujo->addWidget(area);
dibujo->addLayout(valores);
dibujo->addWidget(dibujar);
dibujo->addWidget(salir);
setLayout(dibujo);
}
int main(int argc, char *argv[])
{
MyWidget widget;
widget.setGeometry(100, 100, 500, 500);
widget.show();
return app.exec();
}
#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpinBox>
#include "areadibujo.h"
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent):QWidget(parent)
{
AreaDibujo *area = new AreaDibujo;
QPushButton *salir = new QPushButton("Salir");
QPushButton *dibujar = new QPushButton("Dibujar");
QSpinBox *valorX = new QSpinBox;
QSpinBox *valorY = new QSpinBox;
valorX->setRange(0, 1000);
valorY->setRange(0, 1000);
valorX->setValue(100);
valorY->setValue(100);
connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(dibujar, SIGNAL(clicked()), area, SLOT(Dibuja()));
connect(valorX, SIGNAL(valueChanged(int)), area, SLOT(ColocaX(int)));
connect(valorY, SIGNAL(valueChanged(int)), area, SLOT(ColocaY(int)));
QVBoxLayout *dibujo = new QVBoxLayout;
QHBoxLayout *valores = new QHBoxLayout;
valores->addWidget(valorX);
valores->addWidget(valorY);
dibujo->addWidget(area);
dibujo->addLayout(valores);
dibujo->addWidget(dibujar);
dibujo->addWidget(salir);
setLayout(dibujo);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.setGeometry(100, 100, 500, 500);
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
areadibujo.h
#ifndef __AREADIBUJO_H__
#define __AREADIBUJO_H__
#include <QWidget>
{
Q_OBJECT
public:
protected:
public slots:
void ColocaX(int colocaX);
void ColocaY(int colocaY);
void Dibuja();
signals:
void cambioX(int nuevoX);
void cambioY(int nuevoY);
private:
int Xactual;
int Yactual;
bool pulsador;
};
#endif
#ifndef __AREADIBUJO_H__
#define __AREADIBUJO_H__
#include <QWidget>
class AreaDibujo : public QWidget
{
Q_OBJECT
public:
AreaDibujo(QWidget *parent=0);
protected:
void paintEvent(QPaintEvent *event);
public slots:
void ColocaX(int colocaX);
void ColocaY(int colocaY);
void Dibuja();
signals:
void cambioX(int nuevoX);
void cambioY(int nuevoY);
private:
void pintaCirculo(QPainter &painter);
int Xactual;
int Yactual;
bool pulsador;
};
#endif
To copy to clipboard, switch view to plain text mode
areadibujo.cpp
#include "areadibujo.h"
#include <QPainter>
{
setAutoFillBackground(true);
Xactual=100;
Yactual=100;
pulsador=false;
}
{
if(pulsador)
{
pintaCirculo(painter);
}
}
void AreaDibujo::Dibuja()
{
pulsador=true;
update();
}
void AreaDibujo
::pintaCirculo(QPainter &painter
) {
painter.setPen(Qt::blue);
painter.setBrush(Qt::blue);
painter.
setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(Xactual-25,Yactual-25,50,50);
}
void AreaDibujo::ColocaX(int colocaX)
{
Xactual=colocaX;
emit cambioX(Xactual);
}
void AreaDibujo::ColocaY(int colocaY)
{
Yactual=colocaY;
emit cambioY(Yactual);
}
#include "areadibujo.h"
#include <QPainter>
AreaDibujo::AreaDibujo(QWidget *parent):QWidget(parent)
{
setPalette(QPalette(QColor(255, 255, 255)));
setAutoFillBackground(true);
Xactual=100;
Yactual=100;
pulsador=false;
}
void AreaDibujo::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(pulsador)
{
pintaCirculo(painter);
}
}
void AreaDibujo::Dibuja()
{
pulsador=true;
update();
}
void AreaDibujo::pintaCirculo(QPainter &painter)
{
painter.setPen(Qt::blue);
painter.setBrush(Qt::blue);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(Xactual-25,Yactual-25,50,50);
}
void AreaDibujo::ColocaX(int colocaX)
{
Xactual=colocaX;
emit cambioX(Xactual);
}
void AreaDibujo::ColocaY(int colocaY)
{
Yactual=colocaY;
emit cambioY(Yactual);
}
To copy to clipboard, switch view to plain text mode
Sorry for the spanish words in the code, but I think you can follow.
Bookmarks