Results 1 to 10 of 10

Thread: How to draw a circle with spinbox coordinates

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to draw a circle with spinbox coordinates

    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:

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include <QVBoxLayout>
    5. #include <QHBoxLayout>
    6. #include <QWidget>
    7.  
    8. #include "spinvariable.h"
    9. #include "areadibujo.h"
    10.  
    11. class MiWidget : public QWidget
    12. {
    13. public:
    14. MiWidget(QWidget *parent = 0);
    15.  
    16. signals:
    17.  
    18. public slots:
    19.  
    20. };
    21.  
    22. MiWidget::MiWidget(QWidget *parent) :
    23. QWidget(parent)
    24. {
    25. QPushButton *quit = new QPushButton("Quit");
    26. QPushButton *accion = new QPushButton("Accion");
    27.  
    28. QObject::connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    29.  
    30. spinvariable *spin1 = new spinvariable;
    31. spinvariable *spin2 = new spinvariable;
    32.  
    33. spin1->setLabel("Valor X:");
    34. spin1->setValue(10);
    35. spin1->setRange(0, 100);
    36.  
    37.  
    38. spin2->setLabel("Valor Y:");
    39. spin2->setValue(20);
    40. spin2->setRange(0, 100);
    41.  
    42. areadibujo *area = new areadibujo;
    43.  
    44. QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
    45. QObject::connect(spin2, SIGNAL(valueChanged(int)), area, SLOT(setposicionY(int)));
    46. QObject::connect(area, SIGNAL(cambiox(int)), spin1, SLOT(setValue(int)));
    47. QObject::connect(area, SIGNAL(cambioy(int)), spin2, SLOT(setValue(int)));
    48.  
    49. QObject::connect(accion, SIGNAL(clicked()), area, SLOT(accion()));
    50.  
    51. QVBoxLayout *marcovertical = new QVBoxLayout;
    52. QHBoxLayout *marcohorizontal = new QHBoxLayout;
    53.  
    54. marcovertical->addWidget(area);
    55. marcovertical->addStretch();
    56. marcohorizontal->addWidget(spin1);
    57. marcohorizontal->addWidget(spin2);
    58. marcohorizontal->addStretch();
    59. marcovertical->addLayout(marcohorizontal);
    60. marcovertical->addWidget(accion);
    61. marcovertical->addWidget(quit);
    62. setLayout(marcovertical);
    63. }
    64.  
    65.  
    66. int main(int argc, char *argv[])
    67. {
    68. QApplication app(argc, argv);
    69. MiWidget widget;
    70. widget.setGeometry(0, 0, 500, 500);
    71. widget.show();
    72. return app.exec();
    73. }
    To copy to clipboard, switch view to plain text mode 

    This is the spinvariable class:

    spinvariable.h

    Qt Code:
    1. #ifndef SPINVARIABLE_H
    2. #define SPINVARIABLE_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QSpinBox;
    7. class QLabel;
    8.  
    9. class spinvariable : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. spinvariable(QWidget *parent = 0);
    15. int value() const;
    16.  
    17. signals:
    18. void valueChanged(int newValue);
    19.  
    20. public slots:
    21. void setLabel(char *nombre);
    22. void setValue(int value);
    23. void setRange(int Rmin, int Rmax);
    24.  
    25. private:
    26. QSpinBox *spinbox;
    27. QLabel *label;
    28. };
    29.  
    30. #endif // SPINVARIABLE_H
    To copy to clipboard, switch view to plain text mode 

    spinvariable.cpp
    Qt Code:
    1. #include <QSpinBox>
    2. #include <QLabel>
    3. #include <QHBoxLayout>
    4. #include "spinvariable.h"
    5.  
    6. spinvariable::spinvariable(QWidget *parent) :
    7. QWidget(parent)
    8. {
    9. spinbox = new QSpinBox;
    10. label = new QLabel;
    11.  
    12. QHBoxLayout *layout = new QHBoxLayout;
    13.  
    14. layout->addWidget(label);
    15. layout->addWidget(spinbox);
    16.  
    17. setLayout(layout);
    18. }
    19.  
    20. void spinvariable::setValue(int value)
    21. {
    22. spinbox->setValue(value);
    23. }
    24.  
    25. void spinvariable::setRange(int Rmin, int Rmax)
    26. {
    27. spinbox->setRange(Rmin, Rmax);
    28. }
    29.  
    30. int spinvariable::value() const
    31. {
    32. return spinbox->value();
    33. }
    34.  
    35. void spinvariable::setLabel(char *nombre)
    36. {
    37. label->setText(nombre);
    38. }
    To copy to clipboard, switch view to plain text mode 

    This is the areadibujo class (drawingarea):

    areadibujo.h

    Qt Code:
    1. #ifndef AREADIBUJO_H
    2. #define AREADIBUJO_H
    3.  
    4. #include <QWidget>
    5.  
    6. class areadibujo : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. areadibujo(QWidget *parent = 0);
    11.  
    12. signals:
    13. void cambiox(int nuevox);
    14. void cambioy(int nuevoy);
    15.  
    16. public slots:
    17. void accion();
    18. void setposicionX(int x);
    19. void setposicionY(int y);
    20.  
    21. protected:
    22. void paintEvent(QPaintEvent *event);
    23.  
    24. private:
    25. void pintaCirculo(QPainter &painter);
    26. int apretar;
    27.  
    28. int posicionX, posicionY;
    29. };
    30.  
    31. #endif // AREADIBUJO_H
    To copy to clipboard, switch view to plain text mode 

    areadibujo.cpp
    Qt Code:
    1. #include <QPaintEvent>
    2. #include <QPainter>
    3. #include "areadibujo.h"
    4.  
    5. areadibujo::areadibujo(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. setPalette(QPalette(QColor(255, 255, 255)));
    9. setAutoFillBackground(true);
    10. setFixedSize(500, 450);
    11. posicionX=10;
    12. posicionY=20;
    13. apretar=0;
    14. }
    15.  
    16. void areadibujo::paintEvent(QPaintEvent *)
    17. {
    18. QPainter painter(this);
    19.  
    20. if(apretar==1)
    21. {
    22. pintaCirculo(painter);
    23. }
    24. }
    25.  
    26. void areadibujo::pintaCirculo(QPainter &painter)
    27. {
    28. painter.setPen(Qt::NoPen);
    29. painter.setBrush(Qt::blue);
    30. painter.drawEllipse(posicionX,posicionY,10,10);
    31. }
    32.  
    33. void areadibujo::accion()
    34. {
    35. update();
    36. apretar=1;
    37. }
    38.  
    39. void areadibujo::setposicionX(int x)
    40. {
    41. posicionX=x;
    42. emit cambiox(posicionX);
    43. }
    44.  
    45. void areadibujo::setposicionY(int y)
    46. {
    47. posicionY=y;
    48. emit cambioy(posicionY);
    49. }
    To copy to clipboard, switch view to plain text mode 

    I can't see how to make it work.
    I am using the cannonball Qt tutorial as an example.

    Thanks you.

    R. D.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to draw a circle with spinbox coordinates

    Hi,

    it because you connect a signal valueChanged of your class spinvariable with the setter, but you never emit such a signal! You obviously think this should be from your spinbox, but it is in your container class and the signal is not forwarded, so you have to write in your constructor:
    Qt Code:
    1. QObject::connect(spinbox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
    To copy to clipboard, switch view to plain text mode 
    then it will work.

    Note: your last two connections of
    Qt Code:
    1. QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
    2. QObject::connect(spin2, SIGNAL(valueChanged(int)), area, SLOT(setposicionY(int)));
    3. QObject::connect(area, SIGNAL(cambiox(int)), spin1, SLOT(setValue(int)));
    4. QObject::connect(area, SIGNAL(cambioy(int)), spin2, SLOT(setValue(int)));
    To copy to clipboard, switch view to plain text mode 
    don't make sense with your actual code. You can skip them.

    Lykurg

  3. #3
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw a circle with spinbox coordinates

    Hi,

    it because you connect a signal valueChanged of your class spinvariable with the setter, but you never emit such a signal! You obviously think this should be from your spinbox, but it is in your container class and the signal is not forwarded, so you have to write in your constructor:
    Qt Code:
    Qt Code:
    1. QObject::connect(spinbox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
    To copy to clipboard, switch view to plain text mode 
    That won't work, since there is no valueChanged() signal for the MiWidget widget.
    The thing is that when I press the Accion button, it should update the posicionX and posicionY variables and put it in the pintaCirculo() event... I can't make it work yet.

    Thanks for the help!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to draw a circle with spinbox coordinates

    Not in MiWidget, but in SpinVariable.

    You do not emit a signal at the moment from SpinVariable.

  5. #5
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw a circle with spinbox coordinates

    Not in MiWidget, but in SpinVariable.

    You do not emit a signal at the moment from SpinVariable.
    Sorry but I don't understand.
    I emit it right here, I think:

    Qt Code:
    1. QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
    2. QObject::connect(spin2, SIGNAL(valueChanged(int)), area, SLOT(setposicionY(int)));
    To copy to clipboard, switch view to plain text mode 

    Right there, when the spinbox value is changed it should trigger the setposicion functions.

    Thanks for the help!

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to draw a circle with spinbox coordinates

    There you only connect! In your class SpinVariable you have to add a signal forwarding as said:
    Qt Code:
    1. spinvariable::spinvariable(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. spinbox = new QSpinBox;
    5. label = new QLabel;
    6.  
    7. QHBoxLayout *layout = new QHBoxLayout;
    8.  
    9. layout->addWidget(label);
    10. layout->addWidget(spinbox);
    11.  
    12. setLayout(layout);
    13. QObject::connect(spinbox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
    14. }
    To copy to clipboard, switch view to plain text mode 
    The
    Qt Code:
    1. QObject::connect(spin1, SIGNAL(valueChanged(int)), area, SLOT(setposicionX(int)));
    To copy to clipboard, switch view to plain text mode 
    never came to action, since such a signal is never emitted!

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to draw a circle with spinbox coordinates

    Qt Code:
    1. spinvariable::spinvariable(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. spinbox = new QSpinBox;
    5. label = new QLabel;
    6.  
    7. QHBoxLayout *layout = new QHBoxLayout;
    8.  
    9. layout->addWidget(label);
    10. layout->addWidget(spinbox);
    11.  
    12. setLayout(layout);
    13.  
    14. QObject::connect(spinbox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
    15. }
    To copy to clipboard, switch view to plain text mode 

    Edit: too late again ;-)

  8. #8
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw a circle with spinbox coordinates

    Hi!

    It worked! Thank you very much.

    Now, I am a little confused. Why do I have to connect the valueChanged() signal of the spinbox to itself?
    Is it because I am using a PushButton to trigger and event?
    I mean, I've been reading and doing a lot of Qt tutorials and this is the first time I see that. I followed the cannonball tutorial to write my program and if you check it you'll see they don't do that. Whenever the slider is moved in their program, it updates the variable values in the cannonball drawing area.

    I thought that whenever a spinbox or a slider is changed, it emits its valueChanged() signal, and the only thing you have to do is to connect it to whatever you want to do.

    So if you please can clarify me that I'll appreciate.

    Thanks again!

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to draw a circle with spinbox coordinates

    You don't connect the signal to itself. To use a signal you need direct access to the object. In MiWidget you cant access your spinbox, you only can access your class spinvariable. It has a spinbox inside, but that don't matter, for the connect it is only a widget and a widget has no value changed signal. So you have to forward the signal. From the spinbox to the class spinvariable and from there to your MiWidget.

    Another option would be to declare a public pointer to your spinbox but that would be a bad design.

  10. The following user says thank you to Lykurg for this useful post:

    rdelgado (18th August 2010)

  11. #10
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw a circle with spinbox coordinates

    Hi,

    Now I see it, as you said, from the spinbox to the class, and from there to MiWidget!
    I get it now. In the others examples I looked, all the spinboxes and sliders where part of the same class.

    Thank you very much for the help. I'll keep learning!

Similar Threads

  1. To draw a circle on a frame when the key is pressed
    By soumya in forum Qt Programming
    Replies: 18
    Last Post: 9th February 2010, 11:21
  2. how to draw a circle on a frame in Qt-4
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 09:05
  3. How to draw a special circle pie
    By parnedo in forum Qt Programming
    Replies: 7
    Last Post: 3rd July 2009, 16:25
  4. How to draw a semi circle or arc of ellipse
    By parnedo in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2009, 02:39
  5. What is the fastest way to draw a circle ?
    By Vladimir in forum Qt Programming
    Replies: 18
    Last Post: 6th September 2007, 18:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.