Results 1 to 4 of 4

Thread: draw a line between two circles

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Posts
    24
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default draw a line between two circles

    Hello ,

    I am a beginner in C++ and I am trying , but without success to draw a line between two circles .The condition that must be repsected to draw this line is that the element of the array M[i][j]==1 where i is the departure circle and j the second one.For reasons that I really do not understand other lines are drawn .
    Here is the code:
    Qt Code:
    1. // The header arbre.h
    2. #ifndef ARBRE_H
    3. #define ARBRE_H
    4. #include <QtGui>
    5.  
    6. class Arbre : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Arbre();
    12.  
    13. private slots:
    14.  
    15. //void dessin ();
    16. private:
    17. //QTableWidget* tableWidget;
    18. //QPushButton * Valider;
    19. QPainter *painter;
    20. int M [6][6];
    21. void paintEvent(QPaintEvent *);
    22.  
    23. };
    24.  
    25.  
    26.  
    27. #endif // ARBRE_H
    28.  
    29.  
    30.  
    31.  
    32.  
    33. //arbre.cpp
    34.  
    35. #include "arbre.h"
    36. #include "math.h"
    37. #include <QtGui>
    38.  
    39. Arbre::Arbre()
    40. {
    41. paintEvent(e);
    42.  
    43. }
    44.  
    45.  
    46. void Arbre::paintEvent(QPaintEvent *e)
    47. {
    48. QPainter painter(this);
    49.  
    50. M[0][1]=1;
    51. M[1][0]=1;
    52. //M[1][2]=1;
    53. M[0][3]=1;
    54. // M[0][4]=1;
    55. //M[0][5]=1;
    56.  
    57.  
    58. bool tour=false;
    59. int x=35;
    60. int y=45;
    61. int Num_Sommet=0;
    62.  
    63.  
    64. int T_Coordonnees [6][6];
    65. for (Num_Sommet=0; Num_Sommet<6; Num_Sommet++)
    66. {
    67.  
    68. painter.drawEllipse(x, y, 30.0, 30.0);
    69. (T_Coordonnees[Num_Sommet][0])=x;
    70. (T_Coordonnees[Num_Sommet][1])=y;
    71. if (tour==false)
    72. {
    73. x+=85;
    74. tour=true;
    75. }
    76. else
    77. {
    78. y+=95;
    79. tour=false;
    80. }
    81.  
    82. }
    83.  
    84.  
    85. int i,j;
    86. for (i=0; i<6; i++)
    87. {
    88. for (j=0; j<6; j++)
    89. {
    90. if (M[i][j]==1)
    91. {
    92. painter.drawLine((T_Coordonnees[i][0]),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]),(T_Coordonnees[j][1]));
    93. }
    94. }
    95. }
    96. Num_Sommet=0;
    97. for (Num_Sommet=0; Num_Sommet<6; Num_Sommet++)
    98. {
    99. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    100. QString str;
    101. str.setNum(Num_Sommet);
    102. painter.drawText( point,str);
    103.  
    104. }
    105. }
    106.  
    107.  
    108.  
    109.  
    110. //main.cpp
    111. #include <QApplication>
    112. #include "arbre.h"
    113.  
    114. int main(int argc, char* argv[])
    115. {
    116. QApplication app(argc, argv);
    117.  
    118. Arbre fenetre;
    119. fenetre.show();
    120.  
    121. return app.exec();
    122. }
    To copy to clipboard, switch view to plain text mode 


    I thank you in advance for your time and I wish that someone could help to find out where the problem is.

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: draw a line between two circles

    First, initialize all the elements of your array before you start. In debug mode, some compilers initialize everything to 0, but in release mode all uninitialized data will have an unpredictable value.

    When you post some code, it is also helpfull to put some comments in it. You should add comments to your code anyway.

    To find the problem, you could use qDebug. Include QDebug.h in your file, and then you can write something like this :
    Qt Code:
    1. qDebug() << "now element " << i << j;
    To copy to clipboard, switch view to plain text mode 
    Print some debug info in key parts of your routine, and it will help you understand what is going on.

    Best regards,
    Marc

  3. The following user says thank you to marcvanriet for this useful post:

    hichemnho (6th February 2012)

  4. #3
    Join Date
    Feb 2012
    Posts
    24
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: draw a line between two circles

    Thank you very much Marc ,
    Like you said the problem was due to the uninitialized elements.
    I really apreciate your help.


    Added after 7 minutes:


    Hello,

    I've tried the Debug line instruction that you have put , after compilation , the result appears twice , because it is the first time that I use this , I do not understand why .
    Here is where I've put it:

    Qt Code:
    1. M[0][1]=1;
    2. qDebug() << "now element " << M[o][p];
    3. //M[1][0]=1;
    To copy to clipboard, switch view to plain text mode 
    The display is
    now element 1
    now element 1
    Could you please tell me why .
    Thank you.
    Last edited by hichemnho; 6th February 2012 at 08:47.

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

    Default Re: draw a line between two circles

    Calling paintEvent during the constructor makes no sense. The object does not have a visible aspect at this point. Qt will call paintEvent (which is usually a protected member BTW) when the widget needs to be drawn. This is also why debug message is appearing multiple times.

Similar Threads

  1. Replies: 2
    Last Post: 27th June 2011, 13:44
  2. To draw a line
    By vinayaka in forum Qt Quick
    Replies: 1
    Last Post: 6th June 2011, 10:53
  3. Draw Line
    By sagirahmed in forum Newbie
    Replies: 5
    Last Post: 18th October 2010, 07:49
  4. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  5. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02

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.