Results 1 to 13 of 13

Thread: Re: Draw circle in a qframe

  1. #1
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Hi,

    I am trying to draw a Main circle in a QFrame, Inside that main circle, I have two small eclipses those two will be touching each other at the center of the main circle. Those two small eclipse position should change according to the angle provided. I can able to generate paint even and draw a circle.But that is out of frame

    Could you please help me out to draw that inside the frame and other two eclipse as well.. I am attaching my project file here.

    Thanks in Advence


    Added after 5 minutes:


    mydrawtest.ui


    xml Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>DrawFrontPage</class>
    4. <widget class="QMainWindow" name="DrawFrontPage">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>573</width>
    10. <height>448</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>DrawFrontPage</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QFrame" name="frame">
    18. <property name="geometry">
    19. <rect>
    20. <x>100</x>
    21. <y>40</y>
    22. <width>281</width>
    23. <height>261</height>
    24. </rect>
    25. </property>
    26. <property name="frameShape">
    27. <enum>QFrame::StyledPanel</enum>
    28. </property>
    29. <property name="frameShadow">
    30. <enum>QFrame::Raised</enum>
    31. </property>
    32. </widget>
    33. </widget>
    34. <widget class="QMenuBar" name="menuBar">
    35. <property name="geometry">
    36. <rect>
    37. <x>0</x>
    38. <y>0</y>
    39. <width>573</width>
    40. <height>25</height>
    41. </rect>
    42. </property>
    43. </widget>
    44. <widget class="QToolBar" name="mainToolBar">
    45. <attribute name="toolBarArea">
    46. <enum>TopToolBarArea</enum>
    47. </attribute>
    48. <attribute name="toolBarBreak">
    49. <bool>false</bool>
    50. </attribute>
    51. </widget>
    52. <widget class="QStatusBar" name="statusBar"/>
    53. </widget>
    54. <layoutdefault spacing="6" margin="11"/>
    55. <resources/>
    56. <connections/>
    57. </ui>
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "drawfrontpage.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. DrawFrontPage w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    12.  
    13.  
    14. mydrawtest.cpp
    15.  
    16.  
    17. #include "drawfrontpage.h"
    18. #include "ui_drawfrontpage.h"
    19.  
    20.  
    21. #include"QFrame"
    22.  
    23.  
    24. DrawFrontPage::DrawFrontPage(QWidget *parent) :
    25. QMainWindow(parent),
    26. ui(new Ui::DrawFrontPage)
    27. {
    28. ui->setupUi(this);
    29.  
    30. }
    31.  
    32. DrawFrontPage::~DrawFrontPage()
    33. {
    34. delete ui;
    35. }
    36.  
    37.  
    38. void DrawFrontPage::paintEvent ( QPaintEvent * event ){
    39. //QFrame::paintEvent( event );
    40. //int m_radius = 4;
    41. //if ( m_radius > 0 )
    42. //{
    43.  
    44. QRectF rectangle(10.0, 20.0, 150.0, 150.0);
    45.  
    46. QPainter painter(this);
    47. painter.drawEllipse(rectangle);
    48.  
    49.  
    50. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th June 2012 at 20:33. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Draw circle in a qframe

    Instead of passing to drawEllipse() rect pass center and width/height.
    Or calculate position based on the rect.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw circle in a qframe

    Your code doesn't draw on a QFrame at all: you override the QMainWindow paint event, so you are drawing in the client area of the main window. Here's an example that draws a circle inside a frame that is a part of a main window:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyFrame: public QFrame
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit MyFrame(QWidget *p = 0): QFrame(p) {
    8. setFrameStyle(QFrame::Box);
    9. }
    10. protected:
    11. void paintEvent(QPaintEvent *event) {
    12. QFrame::paintEvent(event);
    13.  
    14. int radius = qMin(event->rect().width(), event->rect().height()) / 2;
    15.  
    16. QPainter p(this);
    17. p.drawEllipse(event->rect().center(), radius, radius);
    18. }
    19.  
    20. };
    21.  
    22. int main(int argc, char **argv)
    23. {
    24. QApplication app(argc, argv);
    25. MyFrame *f = new MyFrame(&m);
    26. m.setCentralWidget(f);
    27. m.resize(150, 150);
    28. m.show();
    29. return app.exec();
    30. }
    31. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    You can use Designer to put MyFrame into your UI using the Promotion feature.

  4. #4
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Thanks for your reply and your valuable time.

    i have one more requirement in this regard.

    I need to draw a plate kind of thing that will change according to the angle that we have provided say in lineEdit. Screenshot.jpg


    The requirement is attached as screen shot image

    Could you please help me out to draw this according to angle provided..


    Thanks
    Vinithr

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw circle in a qframe

    Why don't you take my example code and add some code:
    • To fill the circle, see QPainter::setBrush()
    • To draw two ellipses along the x or y axis (you already know how to draw an ellipse)
    • To allow setting a member variable containing the angle that the ellipses should be drawn at


    Then read the Analog Clock Example to see how you might rotate the ellipses using QPainter::translate() and QPainter::rotate().
    Last edited by ChrisW67; 11th June 2012 at 09:18.

  6. #6
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Thanks for your reply.

    I will check out and let you know the result.

    Again thanks for your help and time.

    Regards
    vinithr

  7. #7
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Thanks for your help. But I did not reached that what i need. i tried to integrate the code that you are given. I attached the output that i gotScreenshot-1.jpg

    Since i am new to Qt . i couldnt able to create a Event for the QFrame

    In the image that i have attached, i have a frame In that frame only i need to draw the circle.
    i have added the source code for your reference .



    //Drawfrontpage.h


    Qt Code:
    1. #ifndef DRAWFRONTPAGE_H
    2. #define DRAWFRONTPAGE_H
    3.  
    4. #include <QMainWindow>
    5. #include"QtGui"
    6. #include"qpainter.h"
    7. #include"QFrame"
    8.  
    9. namespace Ui {
    10. class DrawFrontPage ;
    11. }
    12.  
    13. class DrawFrontPage : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit DrawFrontPage(QWidget *parent = 0);
    19. ~DrawFrontPage();
    20.  
    21.  
    22. public:
    23. void paintEvent ( QPaintEvent * event );
    24. // void QFrame::paintEvent( QPaintEvent * event );
    25.  
    26.  
    27.  
    28. private:
    29. Ui::DrawFrontPage *ui;
    30. };
    31.  
    32.  
    33. #endif // DRAWFRONTPAGE_H
    34.  
    35.  
    36.  
    37. //drawfrontpage.cpp
    38.  
    39.  
    40. #include "drawfrontpage.h"
    41. #include "ui_drawfrontpage.h"
    42.  
    43.  
    44. #include"qframe.h"
    45.  
    46.  
    47. DrawFrontPage:: DrawFrontPage(QWidget *parent) :
    48. QMainWindow(parent),
    49. ui(new Ui:: DrawFrontPage)
    50. {
    51. ui->setupUi(this);
    52.  
    53.  
    54. }
    55.  
    56. DrawFrontPage::~DrawFrontPage()
    57. {
    58. delete ui;
    59. }
    60.  
    61.  
    62. void DrawFrontPage::paintEvent ( QPaintEvent * event ){
    63.  
    64. QFrame *myframe =new QFrame;
    65. // QFrame::paintEvent(event);
    66.  
    67. int radius = qMin(event->rect().width(), event->rect().height()) / 2;
    68. QPainter painter(this);
    69. painter.drawEllipse(event->rect().center(), radius, radius);
    70. /*
    71.   QRectF rectangle(10.0, 20.0, 150.0, 150.0);
    72.  
    73.   QPainter painter(this);
    74.   painter.drawEllipse(rectangle);
    75. */
    76.  
    77. }
    78. //}
    79.  
    80. //main.cpp
    81.  
    82.  
    83. #include <QtGui/QApplication>
    84. #include "drawfrontpage.h"
    85.  
    86. int main(int argc, char *argv[])
    87. {
    88. QApplication a(argc, argv);
    89. DrawFrontPage w;
    90. /* DrawFrontPage m;
    91.  
    92.   QFrame *f = new QFrame(&m);
    93.  
    94.   m.setCentralWidget(f);
    95.  
    96. // m.resize(150, 150);
    97.  
    98.   m.show();
    99. */
    100. w.show();
    101.  
    102. return a.exec();
    103. }
    To copy to clipboard, switch view to plain text mode 
    Drafrontpage.ui


    xml Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>DrawFrontPage</class>
    4. <widget class="QMainWindow" name="DrawFrontPage">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>573</width>
    10. <height>448</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>DrawFrontPage</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QFrame" name="myframe">
    18. <property name="geometry">
    19. <rect>
    20. <x>270</x>
    21. <y>50</y>
    22. <width>281</width>
    23. <height>261</height>
    24. </rect>
    25. </property>
    26. <property name="frameShape">
    27. <enum>QFrame::StyledPanel</enum>
    28. </property>
    29. <property name="frameShadow">
    30. <enum>QFrame::Raised</enum>
    31. </property>
    32. </widget>
    33. </widget>
    34. </widget>
    35. <layoutdefault spacing="6" margin="11"/>
    36. <resources/>
    37. <connections/>
    38. </ui>
    To copy to clipboard, switch view to plain text mode 



    Could you please tell me how to create a QFrame Paint event in this code .

    Thanks in advance
    Regards
    vinithr
    Last edited by wysota; 11th June 2012 at 20:29. Reason: missing [code] tags

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draw circle in a qframe

    Did you remember to take a look at the analog clock example? And as we're at it... Did you remember to actually read the post of your pre-poster with understanding? You have practically posted the same code again, totally ignoring what you were told.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw circle in a qframe

    Quote Originally Posted by vinithr View Post
    Could you please tell me how to create a QFrame Paint event in this code .
    You don't. Qt generates paint events on a widget when the widget needs to be repainted. This happens, for example, if the widget is resized, exposed after being hidden, or program code calls QWidget::update() on a widget or one of its parents. Put a debug message inside my example's paintEvent() to see when it is called... then minimise, maximise, resize, partially hide and generally play with the window.

    The idea of creating a custom widget with a custom paintEvent() to draw your circles is that the widget looks after drawing itself. In order to use the custom MyWidget you need to put it in your UI. You have not done that. There is a section in the Designer manual as to how you do that.

  10. #10
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Sorry for the late reply.

    i draw whatever the things ineed to display. But Thats coming in the widget not in the frame.. If i am giving setcentralwidget the frame size get changed and its occupying the whole mainwindow.

    Since i am new to Qt,i could nt able to proceed further. Could you please look in to the code that i have attached and could you please help me out to sort this issue.

    i have added my output image also. In that image one frame is visible. In that frame only i need to draw that whole thing.
    image.jpg

    Main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "drawfrontpage.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. DrawFrontPage w;
    8. // DrawFrontPage m;
    9.  
    10. // QFrame *f = new QFrame(&m);
    11.  
    12. // m.setCentralWidget(f);
    13.  
    14. // m.resize(150, 150);
    15.  
    16. // m.show();
    17.  
    18. w.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 


    MainWindow.h


    Qt Code:
    1. #ifndef DRAWFRONTPAGE_H
    2. #define DRAWFRONTPAGE_H
    3.  
    4. #include <QMainWindow>
    5. #include"QtGui"
    6. #include"qpainter.h"
    7. #include"QFrame"
    8.  
    9. namespace Ui {
    10. class DrawFrontPage ;
    11. }
    12.  
    13. class DrawFrontPage : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit DrawFrontPage(QWidget *parent = 0);
    19. ~DrawFrontPage();
    20.  
    21.  
    22. public:
    23. void paintEvent ( QPaintEvent * event );
    24. // void QFrame::paintEvent( QPaintEvent * event );
    25.  
    26.  
    27.  
    28. private:
    29. Ui::DrawFrontPage *ui;
    30. };
    31.  
    32.  
    33. #endif // DRAWFRONTPAGE_H
    To copy to clipboard, switch view to plain text mode 


    Mainwindow.cpp


    Qt Code:
    1. #include "drawfrontpage.h"
    2. #include "ui_drawfrontpage.h"
    3.  
    4.  
    5. #include"qframe.h"
    6.  
    7.  
    8. int Direction=1; //0-> clockwise 1->anti Colockwise
    9. int Angle=10;
    10. int Temp1;
    11. int Temp2;
    12.  
    13. DrawFrontPage:: DrawFrontPage(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui:: DrawFrontPage)
    16. {
    17. ui->setupUi(this);
    18.  
    19.  
    20. }
    21.  
    22. DrawFrontPage::~DrawFrontPage()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28. void DrawFrontPage::paintEvent ( QPaintEvent * event ){
    29.  
    30. QFrame *myframe =new QFrame(this);
    31. // QFrame::paintEvent(event);
    32. // ui->myframe->setSizeIncrement(10,10);
    33. // setCentralWidget(myframe)
    34. // setCentralWidget(myframe);
    35. int radius = qMin(event->rect().width(), event->rect().height()) / 2;
    36. // QPainter painter(this);
    37.  
    38. /*
    39.   QRectF rectangle(10.0, 20.0, 150.0, 150.0);
    40.  
    41.   QPainter painter(this);
    42.   painter.drawEllipse(rectangle);
    43.  
    44. */
    45. //fix the direction for the blade
    46.  
    47. if(Direction == 0)
    48. {
    49. if(Angle <=180)
    50. {
    51. Temp1 = 0 + Angle;
    52. Temp2= 180+ Angle;
    53. }
    54. else
    55. {
    56. Temp1 = 0 + Angle;
    57. Temp2= 0 + (Angle-180);
    58.  
    59. }
    60. }
    61. else
    62. {
    63. if(Angle <=180)
    64. {
    65. Temp1 = 360 - Angle;
    66. Temp2= 180 - Angle;
    67. }
    68. else
    69. {
    70. Temp1 = 360 -Angle;
    71. Temp2= 360 -(Angle-180);
    72.  
    73. }
    74. }
    75.  
    76. static const QPoint hourHand[3] = {
    77. QPoint(7, 8),
    78. QPoint(-7, 8),
    79. QPoint(0, -100)
    80. };
    81. static const QPoint minuteHand[3] = {
    82. QPoint(7, 8),
    83. QPoint(-7, 8),
    84. QPoint(0, -100)
    85. };
    86.  
    87. QColor hourColor(127, 0, 127);
    88. QColor minuteColor(127, 0, 127);
    89.  
    90. int side = qMin(width(), height());
    91.  
    92. QPainter painter(this);
    93. painter.drawEllipse(event->rect().center(), radius, radius);
    94.  
    95. painter.setRenderHint(QPainter::Antialiasing);
    96.  
    97. painter.translate(width() / 2, height() / 2);
    98.  
    99. painter.scale(side / 200.0, side / 200.0);
    100.  
    101. painter.setPen(Qt::NoPen);
    102.  
    103. painter.setBrush(hourColor);
    104.  
    105. painter.save();
    106.  
    107. painter.rotate(Temp1);
    108. painter. drawConvexPolygon(hourHand, 3);
    109. painter.restore();
    110.  
    111. painter.setPen(hourColor);
    112.  
    113.  
    114. painter.setPen(Qt::NoPen);
    115. painter.setBrush(minuteColor);
    116.  
    117. painter.save();
    118. painter.rotate(Temp2);
    119. painter.drawConvexPolygon(minuteHand, 3);
    120. painter.restore();
    121.  
    122. painter.setPen(minuteColor);
    123. }
    To copy to clipboard, switch view to plain text mode 

    Mainwindow.ui

    xml Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>DrawFrontPage</class>
    4. <widget class="QMainWindow" name="DrawFrontPage">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>573</width>
    10. <height>448</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>DrawFrontPage</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QFrame" name="myframe">
    18. <property name="geometry">
    19. <rect>
    20. <x>270</x>
    21. <y>50</y>
    22. <width>281</width>
    23. <height>261</height>
    24. </rect>
    25. </property>
    26. <property name="frameShape">
    27. <enum>QFrame::StyledPanel</enum>
    28. </property>
    29. <property name="frameShadow">
    30. <enum>QFrame::Raised</enum>
    31. </property>
    32. </widget>
    33. </widget>
    34. </widget>
    35. <layoutdefault spacing="6" margin="11"/>
    36. <resources/>
    37. <connections/>
    38. </ui>
    To copy to clipboard, switch view to plain text mode 


    Thanks for your response and looking forward to close this issue ASAP

    Regards
    vinithr
    Last edited by wysota; 3rd July 2012 at 15:06. Reason: missing [code] tags

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw circle in a qframe

    Get rid of the references to a QFrame in your custom widget code. You can probably also dispense with whatever Designer UI stuff you have associated with your custom widget and inherit from QWidget rather than QMainWindow. Put an instance of your custom widget inside a QFrame instance, put that QFrame instance in the central area of a QMainWindow. Problem solved.

  12. #12
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw circle in a qframe

    Thanks for your reply.


    Since i am new to Qt and C++ , Could you please tell me in the beginer understanding format.


    And could you please tell me what are the things i need to put in that QFrame class and How do i need to call that QFrame class from MainWindow.

    Thanks in Advance.

    Regards
    Vinithr

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw circle in a qframe

    Quote Originally Posted by vinithr View Post
    And could you please tell me what are the things i need to put in that QFrame class
    You do not need to subclass or extend QFrame at all. You need to use a QFrame instance to contain your circle drawing widget. You need to create the container (a QFrame), create the contained widget (a MyCircleDrawingWidget), add the contained widget to a layout and set the layout on the container.
    How do i need to call that QFrame class from MainWindow.
    Just like any other widget.

    I am not going to write the entire solution for you.

Similar Threads

  1. how to draw a circle using qml
    By hema in forum Qt Quick
    Replies: 2
    Last Post: 8th August 2016, 12:48
  2. draw a circle
    By NewLegend in forum Qt Programming
    Replies: 3
    Last Post: 13th October 2010, 16:01
  3. how to draw a circle on a frame in Qt-4
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 08:05
  4. How to draw a special circle pie
    By parnedo in forum Qt Programming
    Replies: 7
    Last Post: 3rd July 2009, 15:25
  5. What is the fastest way to draw a circle ?
    By Vladimir in forum Qt Programming
    Replies: 18
    Last Post: 6th September 2007, 17: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
  •  
Qt is a trademark of The Qt Company.