Results 1 to 4 of 4

Thread: An array as a common variable between two classes .

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 An array as a common variable between two classes .

    Hello,

    I would like to use an boolean array "M".
    Here is what I want to do:
    -I fill the QTableWidget .
    -After a click on the QPushButton "generer" the boolean array M receives the values that I fiiled in the QTableWidget.
    -after a click on the QPushButton "dessiner" a second window is created "FenDessin" which contains a Draw related to the values of the boolean array M.
    Here is the code:
    Qt Code:
    1. ///////////the Headers//////////////////////
    2. //FenPrinicipale.h
    3. #ifndef FENPRINCIPALE_H
    4. #define FENPRINCIPALE_H
    5. #include <QtGui>
    6.  
    7. class FenPrincipale : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. FenPrincipale();
    12. bool M[10][10];
    13. private slots:
    14. void FillIn();
    15. void FenetreDessin();
    16. private:
    17. QTableWidget *tabM;
    18. QPushButton * generer;
    19. QPushButton * dessiner;
    20. QVBoxLayout * vbox;
    21. };
    22. #endif // FENPRINCIPALE_H
    23.  
    24.  
    25. //FenDessin.h
    26. #ifndef FENDESSIN_H
    27. #define FENDESSIN_H
    28. #include <QtGui>
    29. class FenDessin : public QDialog
    30. {
    31. public:
    32. FenDessin ( QWidget *parent);
    33.  
    34. private:
    35. QPushButton *fermer;
    36. QPainter *painter;
    37. private slots:
    38. void paintEvent (QPaintEvent *);
    39. };
    40. #endif // FENDESSIN_H
    41.  
    42.  
    43.  
    44. //FenPrincipale.cpp
    45. #include "qDebug.h"
    46. #include "FenPrincipale.h"
    47. #include "FenDessin.h"
    48. FenPrincipale::FenPrincipale()
    49. {
    50. tabM =new QTableWidget(this);
    51. tabM->setRowCount(10);
    52. tabM->setColumnCount(10);
    53. generer = new QPushButton("&Générer !",this);
    54. QVBoxLayout* vbox = new QVBoxLayout();
    55. vbox->addWidget(generer);
    56. vbox->addWidget(dessiner);
    57. vbox->addWidget(tabM);
    58. this->setLayout(vbox);
    59. for (int r = 0; r < 10; r++) {
    60. for (int c = 0; c < 10; c++) {
    61. tabM->setItem(r, c, it);
    62. it->setData(Qt::EditRole, 0);
    63.  
    64. }
    65. }
    66. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    67. }
    68. void FenPrincipale::FillIn()
    69. {
    70. for (int r = 0; r < 10; r++) {
    71. for (int c = 0; c < 10; c++) {
    72. QTableWidgetItem *it = tabM->item(r,c);;
    73. Q_ASSERT(it);
    74. qDebug() <<it;
    75. int val = (it->data(Qt::EditRole)).toInt();
    76. M[r][c] = (val == 1)? true: false;
    77. qDebug() <<M[r][c];
    78. }
    79. }
    80. connect(dessiner, SIGNAL(clicked()), this, SLOT(FenetreDessin()));
    81. }
    82. void FenPrincipale::FenetreDessin()
    83. {
    84. FenDessin *fenetreCC = new FenDessin( this);
    85. fenetreCC->exec();
    86. }
    87.  
    88.  
    89. //FenDessin.cpp
    90. #include "FenDessin.h"
    91. FenDessin::FenDessin( QWidget *parent = 0) :QDialog(parent)
    92. {
    93. fermer = new QPushButton("Fermer",this);
    94. paintEvent(c);
    95. connect(fermer, SIGNAL(clicked()), this, SLOT(accept()));
    96.  
    97. }
    98. void FenDessin::paintEvent(QPaintEvent *c)
    99. {
    100. QPainter painter(this);
    101. int o,p;
    102. for (o=0; o<10; o++)
    103. {
    104. for (p=0; p<10; p++)
    105. {
    106. M[o][p]=0;
    107. }
    108. }
    109. int tour=0;
    110. int x=10;
    111. int y=10;
    112. int Num_Sommet=0;
    113. int T_Coordonnees [10][3];
    114. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    115. {
    116. (T_Coordonnees[Num_Sommet][0])=x;
    117. (T_Coordonnees[Num_Sommet][1])=y;
    118. (T_Coordonnees[Num_Sommet][2])=tour;
    119.  
    120. if (tour==0)
    121. {
    122. x+=35;
    123. tour=1;
    124. }
    125. else
    126. {
    127. y+=55;
    128. tour=0;
    129. }
    130.  
    131. }
    132.  
    133. int w1=0;
    134. int w2=0;
    135. int cpt=10;
    136. Num_Sommet=0;
    137. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    138. {
    139. if ( (T_Coordonnees[Num_Sommet][2])==1)
    140. { if(w1==0)
    141. {
    142. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+500) ;
    143. w1=1;
    144. }
    145. else
    146. {
    147. if(w1==1)
    148. {
    149. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+250+cpt) ;
    150. w1=0;
    151. cpt=cpt+15;
    152. }
    153. }
    154. if (Num_Sommet==9)
    155. {
    156. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    157. }
    158.  
    159. }
    160. else
    161. {
    162. if(w2==0)
    163. {
    164. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+200;
    165. w2=1;
    166. }
    167. else
    168. {
    169. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+100+cpt;
    170. w2=0;
    171. cpt=cpt+15;
    172. }
    173.  
    174. if (Num_Sommet==4)
    175. {
    176. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    177. }
    178. }
    179. painter.drawEllipse((T_Coordonnees[Num_Sommet][0]), (T_Coordonnees[Num_Sommet][1]), 30.0, 30.0);
    180. }
    181.  
    182. int i,j;
    183. for (i=0; i<10; i++)
    184. {
    185. for (j=0; j<10; j++)
    186. {
    187. if (M[i][j]==1)
    188. {
    189. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    190. }
    191. }
    192. }
    193.  
    194.  
    195.  
    196. for (i=0; i<10; i++)
    197. {
    198. for (j=0; j<10; j++)
    199. {
    200. if ((M[i][j]==1) && (M[j][i]==1))
    201. {
    202. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    203. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
    204. {
    205. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
    206. }
    207. else
    208. {
    209. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
    210. }
    211. }
    212. }
    213. }
    214. ////////////////////////////////////////////////
    215. Num_Sommet=0;
    216. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    217. {
    218. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    219. QString str;
    220. str.setNum(Num_Sommet);
    221. painter.drawText( point,str);
    222.  
    223. }
    224. }
    225.  
    226. //main.cpp
    227. #include <QApplication>
    228. #include "FenPrincipale.h"
    229. int main(int argc, char* argv[])
    230. {
    231. QApplication app(argc, argv);
    232. FenPrincipale fenetre;
    233. fenetre.show();
    234. return app.exec();
    235. }
    To copy to clipboard, switch view to plain text mode 
    I would like to declare the array M as a global variable so that the class FenDessin can use it to make the drawing without the error:
    " M is not declared in this scope"
    Thank you for the attention given to this message.

  2. #2
    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: An array as a common variable between two classes .

    If you want access to the data in the public FenPrincipale::M member variable in another class then you need one of these:
    • Pass a pointer (or reference as appropriate) to the FenPrincipale instance to the other class and access M through that
    • Supply the other class with a pointer (or reference) to the FenPrincipale::M member directly
    • Supply the other class with a copy of the FenPrincipale::M member

    which you choose is up to you and your requirements.

    This is 100% C++ knowledge, and nothing to do with Qt.

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

    hichemnho (25th February 2012)

Similar Threads

  1. Replies: 6
    Last Post: 12th February 2012, 04:52
  2. save variable size hex string into uchar array
    By amika in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2011, 22:35
  3. Replies: 3
    Last Post: 13th July 2011, 16:12
  4. To Share a variable among classes
    By JenniferF in forum Newbie
    Replies: 2
    Last Post: 12th May 2011, 11:13
  5. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24

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.