Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: putting QTableWidgetItem (s) in a boolean array

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

    Default Re: putting QTableWidgetItem (s) in a boolean array

    Thank you for your response,
    val receives finally the value filled in the QTablWidget but there are an error that occured while the instruction of debugg is executed :QTableWidget: cannot insert an item that is already owned by another QTableWidget
    Qt Code:
    1. //le header
    2. #ifndef FENPRINCIPALE_H
    3. #define FENPRINCIPALE_H
    4. #include <QtGui>
    5. #include <vector>
    6. class FenPrincipale : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. FenPrincipale();
    11. bool M[2][2];
    12.  
    13. private slots:
    14. void FillIn();
    15. private:
    16. QTableWidget *tabM;
    17. //QTableWidgetItem *it;
    18. QPushButton * generer;
    19. QVBoxLayout * vbox;
    20. };
    21. #endif
    22.  
    23. //FenPrincipale.cpp
    24. #include "qDebug.h"
    25. #include "FenPrincipale.h"
    26. FenPrincipale::FenPrincipale()
    27. {
    28.  
    29. // QTableWidget *tabM =new QTableWidget(this);
    30. tabM =new QTableWidget(this);
    31. tabM->setRowCount(2);
    32. tabM->setColumnCount(2);
    33. generer = new QPushButton("&Générer !",this);
    34. // generer->move(50,50);
    35. QVBoxLayout* vbox = new QVBoxLayout();
    36. vbox->addWidget(generer);
    37. vbox->addWidget(tabM);
    38. this->setLayout(vbox);
    39.  
    40. for (int r = 0; r < 2; r++) {
    41. for (int c = 0; c < 2; c++) {
    42.  
    43. tabM->setItem(r, c, it);
    44. it->setData(Qt::EditRole, 0);
    45. //qDebug() <<it;
    46. }
    47. }
    48.  
    49. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    50. }
    51. void FenPrincipale::FillIn()
    52. {
    53. for (int r = 0; r < 2; r++) {
    54. for (int c = 0; c < 2; c++) {
    55.  
    56. it = tabM->item(r,c);
    57.  
    58. Q_ASSERT(it);
    59.  
    60. it=(tabM->item(r,c));
    61. tabM->setItem(r, c, it);
    62. qDebug() <<it;
    63. int val = (it->data(Qt::EditRole)).toInt();
    64. M[r][c] = (val == 1)? true: false;
    65.  
    66. qDebug() <<M[r][c];
    67. }
    68. }
    69. }
    70.  
    71.  
    72.  
    73. //main.cpp
    74.  
    75.  
    76. #include <QApplication>
    77. #include "FenPrincipale.h"
    78. int main(int argc, char* argv[])
    79. {
    80. QApplication app(argc, argv);
    81.  
    82. FenPrincipale fenetre;
    83. fenetre.show();
    84. return app.exec();
    85. }
    To copy to clipboard, switch view to plain text mode 
    Could you please help me find out the where the problem is beacause even if I create an QTableWidgetItem for every (r,c) the problem remains.
    Thank you in advance for your help.

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

    Default Re: putting QTableWidgetItem (s) in a boolean array

    The problem is solved by erazing some useless lines .
    Here is the code:
    Qt Code:
    1. //header
    2. #ifndef FENPRINCIPALE_H
    3. #define FENPRINCIPALE_H
    4.  
    5. #include <QtGui>
    6.  
    7. class FenPrincipale : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. FenPrincipale();
    13. bool M[2][2];
    14.  
    15. private slots:
    16. void FillIn();
    17.  
    18. private:
    19. QTableWidget *tabM;
    20. QPushButton *generer;
    21. };
    22.  
    23. #endif
    24.  
    25.  
    26. //FenPrincipale.cpp
    27. #include <QDebug>
    28. #include "FenPrincipale.h"
    29.  
    30. FenPrincipale::FenPrincipale()
    31. {
    32. tabM =new QTableWidget(2, 2, this);
    33. generer = new QPushButton("&Générer !",this);
    34. QVBoxLayout *vbox = new QVBoxLayout;
    35. vbox->addWidget(generer);
    36. vbox->addWidget(tabM);
    37. setLayout(vbox);
    38.  
    39. for (int r = 0; r < tabM->rowCount(); r++) {
    40. for (int c = 0; c < tabM->columnCount(); c++) {
    41. tabM->setItem(r, c, it);
    42. it->setData(Qt::EditRole, 0);
    43. }
    44. }
    45.  
    46. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    47. }
    48.  
    49. void FenPrincipale::FillIn()
    50. {
    51. for (int r = 0; r < tabM->rowCount(); r++) {
    52. for (int c = 0; c < tabM->columnCount(); c++) {
    53. QTableWidgetItem *it = tabM->item(r,c);
    54. Q_ASSERT(it);
    55. qDebug() << it;
    56. int val = (it->data(Qt::EditRole)).toInt();
    57. M[r][c] = (val == 1) ? true : false;
    58. qDebug() << M[r][c];
    59. }
    60. }
    61. }
    62.  
    63.  
    64. //main.cpp
    65. #include <QApplication>
    66. #include "FenPrincipale.h"
    67.  
    68. int main(int argc, char* argv[])
    69. {
    70. QApplication app(argc, argv);
    71.  
    72. FenPrincipale fenetre;
    73. fenetre.show();
    74. return app.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 
    Thank you all for your help , I really apreciate it .
    Thank you again .

    hello,
    The problem is solved erazing some useless lines.
    Here is the code:
    Qt Code:
    1. //header
    2. #ifndef FENPRINCIPALE_H
    3. #define FENPRINCIPALE_H
    4.  
    5. #include <QtGui>
    6.  
    7. class FenPrincipale : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. FenPrincipale();
    13. bool M[2][2];
    14.  
    15. private slots:
    16. void FillIn();
    17.  
    18. private:
    19. QTableWidget *tabM;
    20. QPushButton *generer;
    21. };
    22.  
    23. #endif
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30. //FenPrincipale.cpp
    31. #include <QDebug>
    32. #include "FenPrincipale.h"
    33.  
    34. FenPrincipale::FenPrincipale()
    35. {
    36. tabM =new QTableWidget(2, 2, this);
    37. generer = new QPushButton("&Générer !",this);
    38. QVBoxLayout *vbox = new QVBoxLayout;
    39. vbox->addWidget(generer);
    40. vbox->addWidget(tabM);
    41. setLayout(vbox);
    42.  
    43. for (int r = 0; r < tabM->rowCount(); r++) {
    44. for (int c = 0; c < tabM->columnCount(); c++) {
    45. tabM->setItem(r, c, it);
    46. it->setData(Qt::EditRole, 0);
    47. }
    48. }
    49.  
    50. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    51. }
    52.  
    53. void FenPrincipale::FillIn()
    54. {
    55. for (int r = 0; r < tabM->rowCount(); r++) {
    56. for (int c = 0; c < tabM->columnCount(); c++) {
    57. QTableWidgetItem *it = tabM->item(r,c);
    58. Q_ASSERT(it);
    59. qDebug() << it;
    60. int val = (it->data(Qt::EditRole)).toInt();
    61. M[r][c] = (val == 1) ? true : false;
    62. qDebug() << M[r][c];
    63. }
    64. }
    65. }
    66.  
    67.  
    68. //main.cpp
    69. #include <QApplication>
    70. #include "FenPrincipale.h"
    71.  
    72. int main(int argc, char* argv[])
    73. {
    74. QApplication app(argc, argv);
    75.  
    76. FenPrincipale fenetre;
    77. fenetre.show();
    78. return app.exec();
    79. }
    To copy to clipboard, switch view to plain text mode 
    Thank you all for your help.
    I really apreciate it .
    Thanks.

Similar Threads

  1. Replies: 2
    Last Post: 12th November 2010, 14:42
  2. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 13:48
  3. 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
  4. Strore Boolean value
    By bismitapadhy in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 09:32
  5. [delegate] wrong commit for boolean
    By lauranger in forum Qt Programming
    Replies: 3
    Last Post: 26th October 2006, 08:14

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.