Results 1 to 3 of 3

Thread: From QTableWidget to QPainter

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

    Default From QTableWidget to QPainter

    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.

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

    Default Re: From QTableWidget to QPainter

    You can only paint from within a paintEvent(). Calculate your stuff, call update() (which will eventually call paintEvent()) and do the painting in the event handler.
    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.


  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: From QTableWidget to QPainter

    See the earlier thread for more on this same problem.

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2011, 04:19
  2. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  3. Replies: 5
    Last Post: 7th September 2009, 20:57
  4. QPainter::save and QPAinter::restore()
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 17th June 2006, 22:11
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.