Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: putting QTableWidgetItem (s) in a boolean array

  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 putting QTableWidgetItem (s) in a boolean array

    Hello,
    As the title is saying , I am trying to put the result of a user input , in a QTableWidget, and take this QTableWidget to fillin an array.
    I have read the documentation but infortunetly I can not do it .
    Here is the code:
    Qt Code:
    1. //the 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. QTableWidgetItem * element;
    13. private slots:
    14. private:
    15. QTableWidget* tableWidget;
    16.  
    17.  
    18. };
    19. #endif
    20.  
    21.  
    22.  
    23. //FenPrincipale.cpp
    24.  
    25.  
    26.  
    27. #include "qDebug.h"
    28. #include "FenPrincipale.h"
    29.  
    30.  
    31. FenPrincipale::FenPrincipale()
    32. {
    33. int o,p;
    34.  
    35. tableWidget = new QTableWidget(2,2,this);
    36. element = new QTableWidgetItem (1000);
    37. for (o=0; o<2; o++)
    38. {
    39. for (p=0; p<2; p++)
    40. {
    41. (M[o][p])=false;
    42.  
    43. }
    44. }
    45.  
    46. for (o=0; o<2; o++)
    47. {
    48. for (p=0; p<2; p++)
    49. {
    50. element=(tableWidget->itemAt((o+1),(p+1)));
    51. if ((element->toString)="1")
    52. {
    53. M[o][p]=true;
    54. }
    55. else
    56. {
    57. M[o][p]=false;
    58. }
    59. }
    60. }
    61.  
    62. }
    63.  
    64.  
    65. //main.cpp
    66.  
    67.  
    68. #include <QApplication>
    69. #include "FenPrincipale.h"
    70. int main(int argc, char* argv[])
    71. {
    72. QApplication app(argc, argv);
    73.  
    74. FenPrincipale fenetre;
    75. fenetre.show();
    76. return app.exec();
    77. }
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for the attention given to this message.

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    What is the error? isn`t the program going out of boundries with that +1??Are you sure its a custom type bool(as you are passing the 1000 to the constructor)?

  3. #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: putting QTableWidgetItem (s) in a boolean array

    Thank you for your answer :
    here is a modification of the Fenprincipale.cpp , but it is still not working:



    //FenPrincipale.cpp


    #include "qDebug.h"
    #include "FenPrincipale.h"


    FenPrincipale::FenPrincipale()
    {
    int o,p;

    tableWidget = new QTableWidget(2,2,this);
    element = new QTableWidgetItem (1000);
    for (o=0; o<2; o++)
    {
    for (p=0; p<2; p++)
    {
    (M[o][p])=false;

    }
    }

    for (o=0; o<2; o++)
    {
    for (p=0; p<2; p++)
    {
    element=(tableWidget->item((o+1),(p+1)));
    if ((element->text())=="1")
    {
    M[o][p]=true;
    }
    else
    {
    M[o][p]=false;
    }
    }
    }

    }




    Thank you in advance for your help.

  4. #4
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 11 Times in 11 Posts

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

    Hi,
    Use QTableWidget->item,
    Because QTableWidget->itemAt use coordinat location.
    Please check this code :

    Qt Code:
    1. ui->tableWidget->setRowCount(2);
    2. ui->tableWidget->setColumnCount(2);
    3.  
    4. int i,j;
    5. for(i=0; i<2; i++)
    6. {
    7. for(j=0; j<2; j++)
    8. {
    9. QTableWidgetItem *item = new QTableWidgetItem(QString::number(i));
    10. ui->tableWidget->setItem(i,j,item);
    11.  
    12. }
    13. }
    14.  
    15. for(i=0; i<2; i++)
    16. {
    17. for(j=0; j<2; j++)
    18. {
    19. item = ui->tableWidget->item(i,j);
    20. ui->textEdit->append(item->text());
    21.  
    22. if (item->text()="1")
    23. {
    24. M[o][p]=true;
    25. }
    26. else
    27. {
    28. M[o][p]=false;
    29. }
    30.  
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    you can modified with your problem. This code return text value from QTableWidget

    Thank you

    Best regards,

    Myta
    Last edited by myta212; 15th February 2012 at 09:00.

  5. #5
    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 very much ,

    I tried this code but an error occured because of ui .
    Can you please tell me what "ui" means?
    Thank you

  6. #6
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    basically UI represents all the objects created with the QDesigner...

    http://developer.qt.nokia.com/doc/qt...-concepts.html

    a recommendation: google what you dont know, ask what you cant find

  7. #7
    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

    Thanks,
    I tried to put "this" (which means fenetre in my case ) instead of "ui" , but it is still not working.
    Thank you again.

  8. #8
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    this->

    should work because you have the table widget declared in your .h :/

    post the code


    remember to post with [CODE ] [/CODE] please

  9. #9
    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

    Thanks a lot,
    Please check also the header maybe the error came from there.
    Here is the code :
    Qt Code:
    1. #ifndef FENPRINCIPALE_H
    2. #define FENPRINCIPALE_H
    3. #include <QtGui>
    4. #include <vector>
    5. class FenPrincipale : public QWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. FenPrincipale();
    10. bool M[2][2];
    11. QTableWidgetItem * element;
    12. private slots:
    13. private:
    14. QTableWidget* tableWidget;
    15.  
    16.  
    17.  
    18. };
    19. #endif
    20. //FenPrincipale.cpp
    21.  
    22.  
    23. //FenPrincipale.cpp
    24.  
    25.  
    26. #include "qDebug.h"
    27. #include "FenPrincipale.h"
    28.  
    29.  
    30. FenPrincipale::FenPrincipale()
    31. {
    32. this->tableWidget->setRowCount(2);
    33. this->tableWidget->setColumnCount(2);
    34.  
    35. int i,j;
    36. for(i=0; i<2; i++)
    37. {
    38. for(j=0; j<2; j++)
    39. {
    40. QTableWidgetItem *item = new QTableWidgetItem(QString::number(i));
    41. this->tableWidget->setItem(i,j,item);
    42.  
    43. }
    44. }
    45.  
    46. for(i=0; i<2; i++)
    47. {
    48. for(j=0; j<2; j++)
    49. {
    50. item = this->tableWidget->item(i,j);
    51. this->textEdit->append(item->text());
    52.  
    53. if (item->text()="1")
    54. {
    55. M[o][p]=true;
    56. }
    57. else
    58. {
    59. M[o][p]=false;
    60. }
    61.  
    62. }
    63. }
    64.  
    65. }
    66. //main.cpp
    67.  
    68.  
    69. #include <QApplication>
    70. #include "FenPrincipale.h"
    71. int main(int argc, char* argv[])
    72. {
    73. QApplication app(argc, argv);
    74.  
    75. FenPrincipale fenetre;
    76. fenetre.show();
    77. return app.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 
    Thanks one more time.

    The errors are:
    - C:\Users\SHADUS\fortementc\tabl\FenPrincipale.cpp: 33: erreur : 'class FenPrincipale' has no member named 'textEdit'
    - C:\Users\SHADUS\fortementc\tabl\FenPrincipale.cpp: 35: erreur : could not convert 'QTableWidgetItem::text() const().QString:perator=(((const char*)"1"))' to 'bool'

    the error after correction of error at line 35 (one"=" was missing) is:
    - 'class FenPrincipale' has no member named 'textEdit'

  10. #10
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    Well if you read that error carefully it means you havent declared textEdit... try delcaring QString textEdit

    The seccond error one equals means declare, two equals (==) means comparison

  11. #11
    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 time,
    I only modified the header :
    Qt Code:
    1. #ifndef FENPRINCIPALE_H
    2. #define FENPRINCIPALE_H
    3. #include <QtGui>
    4. #include <vector>
    5. class FenPrincipale : public QWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. FenPrincipale();
    10. bool M[2][2];
    11. QTableWidgetItem * element;
    12. private slots:
    13. private:
    14. QTableWidget* tableWidget;
    15. QString * textEdit;
    16.  
    17.  
    18.  
    19. };
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    after compiling , the program crash and stops.

  12. #12
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    Well of course, just think for a moment what you are doing...you are trying to retrieve an item from a table that its not initialized...you just declare the size of the table but the fields are never set to a valid value...

  13. #13
    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

    Thanks for your response ,
    I am a beginner in Qt and C++ so please tell me exactly what is wrong.

  14. #14
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    It`s like real life, you claim to have a millon dollars but actually you dont (I guess, lol) in this case you delcare a table but you never declare it...YOU CANNOT USE ANYTHING YOU HAVENT DECLARED BEFORE ....try adding this before setting the rowCount...

    tableWidget = new QTableWidget (this);
    its a very common mistake dont worry but try to read some more about c++ specially

  15. #15
    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: putting QTableWidgetItem (s) in a boolean array

    Your original code creates a table widget at line 34 and a new element at line 35. The code never puts anything in the table widget. The table widget will not be seen by the user before (line 46) you start trying to extract items from the unfilled widget. At line 50 you are using the itemAt() function, which gives the cell at certain screen coordinates (it isn't visible here), when you want the item() function that returns the item in a particular table cell. Accessing the unfilled table will, at best, give you an item containing a null QVariant() value and at worst a null pointer. As KillGabio pointed out, you also try to retrieve elements from row/column 1 or 2 of a table (line 50) that only has to row/column 0 or 1. This will return a null pointer (line 50) and your subsequent (line 51) use of the pointer will crash the program.

    myta212 has provided an example of loading the table widget with data..

    You really need to step back from the problem, polish up your C++, look at how Qt is supposed to operate, and then break the problem up into pieces. Here is an example that show one way to approach your matrix editor (I have left your matrix data structure alone)..

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MatrixWidget: public QTableWidget {
    5. Q_OBJECT
    6.  
    7. public:
    8. MatrixWidget(QWidget *p = 0): QTableWidget(p) {
    9. setRowCount(2);
    10. setColumnCount(2);
    11.  
    12. // Default population
    13. for (int r = 0; r < 2; ++r) {
    14. for (int c = 0; c < 2; ++c) {
    15. it->setData(Qt::EditRole, 0);
    16. setItem(r, c, it);
    17. }
    18. }
    19. }
    20.  
    21. void setData(bool M[2][2]) {
    22. for (int r = 0; r < 2; ++r) {
    23. for (int c = 0; c < 2; ++c) {
    24. QTableWidgetItem *it = item(r, c);
    25. Q_ASSERT(it);
    26. int val = M[r][c]? 1: 0;
    27. it->setData(Qt::EditRole, val);
    28. }
    29. }
    30. }
    31.  
    32. void getData(bool M[2][2]) {
    33. for (int r = 0; r < 2; ++r) {
    34. for (int c = 0; c < 2; ++c) {
    35. QTableWidgetItem *it = item(r, c);
    36. Q_ASSERT(it);
    37. int val = it->data(Qt::EditRole).toInt();
    38. M[r][c] = (val == 1)? true: false;
    39. }
    40. }
    41. }
    42. };
    43.  
    44. int main(int argc, char *argv[])
    45. {
    46. QApplication app(argc, argv);
    47.  
    48. bool M[2][2] = { {false, true}, {true, false} };
    49.  
    50. MatrixWidget m;
    51. m.setData(M);
    52. m.show();
    53. return app.exec();
    54. }
    55. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

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

    KillGabio (16th February 2012)

  17. #16
    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

    Hello,


    I tried to think differently and I came up with the fact that after filling the QTableWidget I push a QPushButton to generate the slot "FillIn" to fill the matrixe M but it does not work.
    The error is : QTableWidget: cannot insert an item that is already owned by another QTableWidget
    Here is the code:
    Qt Code:
    1. //The modified 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. QPushButton * generer;
    18. };
    19. #endif
    20.  
    21.  
    22.  
    23.  
    24. //FenPrincipale.cpp
    25. #include "qDebug.h"
    26. #include "FenPrincipale.h"
    27. FenPrincipale::FenPrincipale()
    28. {
    29.  
    30. QTableWidget *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.  
    36.  
    37. for (int r = 0; r < 2; ++r) {
    38. for (int c = 0; c < 2; ++c) {
    39.  
    40.  
    41. tabM->setItem(r, c, it);
    42. it->setData(Qt::EditRole, 0);
    43. //qDebug() <<it;
    44. }
    45. }
    46.  
    47. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    48. }
    49. void FenPrincipale::FillIn()
    50. {
    51. for (int r = 0; r < 2; ++r) {
    52. for (int c = 0; c < 2; ++c) {
    53.  
    54. Q_ASSERT(it);
    55. int val = it->data(Qt::EditRole).toInt();
    56. M[r][c] = (val == 1)? true: false;
    57. }
    58. }
    59. }
    60. //main.cpp
    61.  
    62.  
    63. #include <QApplication>
    64. #include "FenPrincipale.h"
    65. int main(int argc, char* argv[])
    66. {
    67. QApplication app(argc, argv);
    68.  
    69. FenPrincipale fenetre;
    70. fenetre.show();
    71. return app.exec();
    72. }
    To copy to clipboard, switch view to plain text mode 
    By the way , why after compiling only the first cell of the QTableWidget is initializes with "0" and not the others.
    Thank you very much for your attention and time.

  18. #17
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    33
    Thanked 10 Times in 10 Posts

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

    Think for a moment the error: its telling you that the *it var is used before...so basically it explains what you are doing..you need to create a new item for every cell:

    Qt Code:
    1. for (int r = 0; r < 2; r++) {
    2. for (int c = 0; c < 2; c++) {
    3.  
    4. tabM->setItem(r, c, it);
    5. it->setData(Qt::EditRole, 0);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    and remove *it from the header (.h) if you are going to do it that way...and do r++/c++ instead of what you have if you are using the variables inside the for...

  19. #18
    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

    Hello,
    Thank tou for your help , I really apreciate it.
    I change the program.
    Unfortunetly the variable "val" stay at "0" even if I inser "1" in the correspondant cell in the QTableWidget.
    Qt Code:
    1. //the modified 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. };
    20. #endif
    21.  
    22.  
    23.  
    24.  
    25. //FenPrincipale.cpp
    26. //FenPrincipale.cpp
    27. #include "qDebug.h"
    28. #include "FenPrincipale.h"
    29. FenPrincipale::FenPrincipale()
    30. {
    31.  
    32. QTableWidget *tabM =new QTableWidget(this);
    33. tabM->setRowCount(2);
    34. tabM->setColumnCount(2);
    35. generer = new QPushButton("&Générer !",this);
    36. generer->move(50,50);
    37.  
    38. for (int r = 0; r < 2; r++) {
    39. for (int c = 0; c < 2; c++) {
    40.  
    41. tabM->setItem(r, c, it);
    42. it->setData(Qt::EditRole, 0);
    43. //qDebug() <<it;
    44. }
    45. }
    46.  
    47. connect(generer, SIGNAL(clicked()), this, SLOT(FillIn()));
    48. }
    49. void FenPrincipale::FillIn()
    50. {
    51. for (int r = 0; r < 2; r++) {
    52. for (int c = 0; c < 2; c++) {
    53. Q_ASSERT(it);
    54. int val = it->data(Qt::EditRole).toInt();
    55. M[r][c] = (val == 1)? true: false;
    56.  
    57. qDebug() <<val;
    58. }
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 
    Can you please tell me why the value of val is not updated.
    Thank you.

  20. #19
    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: putting QTableWidgetItem (s) in a boolean array

    What item are you reading the value from? What is line 54 doing?

    I suggest you go back and read my example's getData() function more closely.

  21. #20
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 11 Times in 11 Posts

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

    Hi,
    I think you must read documentation about C++ and Qt first. Your problem is very simple, and we send you code how to solve your problem.
    The problem for your question is not "putting QTableWidgetItem (s) in a boolean array" but "How to Fill And Get Value from QTableWidget".
    If you want to get full code for your problem, you can get at Qt Documentation.

    But, please read the other answer above. You can use :
    Qt Code:
    1. tableWidget->setItem(i,j,item);
    To copy to clipboard, switch view to plain text mode 
    to set Item in QTableWidget and
    Qt Code:
    1. QTableWidgetItem *item = tableWidget->item(i,j);
    To copy to clipboard, switch view to plain text mode 
    to get item from QTableWidget.

    Please try and you will get how simple your problem.
    Good luck.

    Best regards,

    Myta212

Similar Threads

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