Hello,

I would like to fill a QTableWidget , and after clicking on the button to validate , paint lines between circles according to the condition that if the element[i][j] of the qtablewidget equals to 1 then there will be a line between the circle i and the circle j.
Here is the code:

Qt Code:
  1. //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 * Tracer;
  19. QPainter *painter;
  20. int M [2][2];
  21. //void paintEvent(QPaintEvent *);
  22. };
  23.  
  24.  
  25.  
  26. #endif // ARBRE_H
  27.  
  28. //arbre.cpp
  29. #include "arbre.h"
  30. #include "math.h"
  31. #include <QtGui>
  32. Arbre::Arbre()
  33. {
  34. tableWidget = new QTableWidget(2,2,this);
  35. Tracer = new QPushButton("Tracer",this);
  36. Tracer->move(10,10);
  37.  
  38. connect(Tracer, SIGNAL(clicked()), this, SLOT(dessin()));
  39. }
  40.  
  41.  
  42. /*void Arbre::paintEvent(QPaintEvent *e)
  43. {
  44.   tableWidget = new QTableWidget(2,2,this);
  45.   Tracer = new QPushButton("Tracer",this);
  46.   Tracer->move(10,10);
  47.  
  48.   connect(Tracer, SIGNAL(clicked()), this, SLOT(dessin()));
  49. }*/
  50. /////////////////////////////////////////////////////////////////
  51.  
  52.  
  53.  
  54. void Arbre::dessin()
  55. {
  56. int Nb_Sommet=2;
  57.  
  58. int s;
  59. int k;
  60. QString str;
  61. for (s=0; s<Nb_Sommet; s++)
  62. {
  63. for (k=0; k<Nb_Sommet; k++)
  64. {
  65. M[s][k] = 0;
  66.  
  67. }
  68. }
  69. str = ((tableWidget->itemAt(1,0))->text());
  70. int dec = str.toInt();
  71. (M[0][1])=dec;
  72. QPainter painter(this);
  73.  
  74. bool tour=false;
  75. int x=35;
  76. int y=45;
  77. int Num_Sommet=0;
  78.  
  79. int T_Coordonnees [Nb_Sommet][Nb_Sommet];
  80. while (Num_Sommet<Nb_Sommet)
  81. {
  82.  
  83. painter.drawEllipse(x,y,30,30);
  84. (T_Coordonnees[Num_Sommet][0])=x;
  85. (T_Coordonnees[Num_Sommet][1])=y;
  86.  
  87.  
  88. if (tour==false)
  89. {
  90. x+=85;
  91. tour=true;
  92. }
  93. else
  94. {
  95. y+=95;
  96. tour=false;
  97. }
  98. Num_Sommet++;
  99. }
  100. Num_Sommet--;
  101. int i,j;
  102. for (i=0; i<Nb_Sommet; i++){
  103. for (j=0; j<Nb_Sommet; j++){
  104. if( (M[i][j] == 1)&&(M[j][i]==1) )
  105. {
  106.  
  107.  
  108. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
  109. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
  110. {
  111. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
  112. }
  113. else
  114. {
  115. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
  116. }
  117. }
  118. else
  119. {
  120. if (M[i][j]==1)
  121. {
  122. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
  123. }
  124.  
  125. }
  126.  
  127. }
  128. }
  129.  
  130.  
  131.  
  132. }
  133.  
  134.  
  135. //main.cpp
  136.  
  137. #include <QApplication>
  138. #include "arbre.h"
  139.  
  140. int main(int argc, char* argv[])
  141. {
  142. QApplication app(argc, argv);
  143.  
  144. Arbre fenetre;
  145. fenetre.show();
  146.  
  147. return app.exec();
  148. }
To copy to clipboard, switch view to plain text mode 
Thank you in advance for your time and attention.