Results 1 to 14 of 14

Thread: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

  1. #1
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Hello!

    this is the code, I wrote:

    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. this->setGeometry(30,30,800,750);
    7. rank = new QLineEdit("Enter the Rank", this);//rank->setGeometry(10,0,20,20);
    8. ok = new QPushButton("Ok", this); ok->setGeometry(150, 0,80,30);
    9. calculate = new QPushButton("calculate",this); calculate->setGeometry(250,0,80,30);
    10.  
    11. coppy = new QTableWidgetItem;
    12.  
    13. connect(ok,SIGNAL(clicked()),this,SLOT(ok_clicked()));
    14. connect(calculate,SIGNAL(clicked()),this,SLOT(calculate_clicked()));
    15. }
    16.  
    17. Widget::~Widget()
    18. {
    19.  
    20. }
    21.  
    22. void Widget::ok_clicked()
    23. {
    24.  
    25. rank_of_Matrix = rank->text().toDouble();
    26. tbl=new QTableWidget(rank_of_Matrix,rank_of_Matrix,this);
    27. tbl->setGeometry(20,50,770,250);
    28. tbl->show();
    29.  
    30. tbl->setItem(2,2,ptwi = new QTableWidgetItem(QString::number(40)));
    31.  
    32.  
    33. Action_toArray(); // wrote all information into Array A[i][j] and make 0 all elements equals to zero
    34.  
    35. }
    36.  
    37. void Widget::calculate_clicked()
    38. {
    39. tbl = new QTableWidget(rank_of_Matrix,rank_of_Matrix,this);
    40.  
    41. tbl ->setGeometry(20,350,770,250);
    42. tbl->setItem(2,2, coppy->clone());
    43. tbl ->show();
    44. }
    45.  
    46. void Widget::Action_toArray()
    47. {
    48.  
    49. A= new float* [rank_of_Matrix];
    50. for(int i=0;i<rank_of_Matrix;i++){
    51. A[i]=new float [rank_of_Matrix];
    52. }
    53.  
    54. for(int i=0;i<rank_of_Matrix;i++){
    55. for(int j=0;j<rank_of_Matrix;j++){
    56. //wrote into Array from QTableWidget
    57. A[i][j] = tbl->item(i,j)->data(Qt::DisplayRole).toFloat();
    58. }
    59. }
    60.  
    61. //make all elements equals to 2
    62. for(int i=0;i<rank_of_Matrix;i++){
    63. for(int j=0;j<rank_of_Matrix;j++){
    64. if(A[i][j]==2){
    65. A[i][j]=0;
    66. }
    67. }
    68. }
    69. }
    To copy to clipboard, switch view to plain text mode 

    My question is: how to write changed Array into Table2 after clicking button "Calculate"? Do I get it right that I need to convert float into QTableWidgetItem and then using command tbl->setItem(rank_of_Matrix,rank_of_Matrix, coppy->clone()); in loop write it into Table?

    Thank you in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Quote Originally Posted by Blitzor DDD View Post
    My question is: how to write changed Array into Table2 after clicking button "Calculate"?
    You only have one table, "tbl"

    Quote Originally Posted by Blitzor DDD View Post
    Do I get it right that I need to convert float into QTableWidgetItem
    No, you format the number as a QString and set that as the table widget item's text.
    You can additionally store the actual number through setData(), using Qt::EditRole.


    Quote Originally Posted by Blitzor DDD View Post
    and then using command tbl->setItem(rank_of_Matrix,rank_of_Matrix, coppy->clone());
    No, you just create a new QTableWidgetItem, passing the text as its constructor argument.

    Quote Originally Posted by Blitzor DDD View Post
    in loop write it into Table?
    yes

    Cheers,
    _

  3. #3
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    You only have one table, "tbl"
    I meant tbl of course. I am going to use the same tbl and created it on the Widget below. Is it possible?

    setData(), using Qt::EditRole.
    but setDate() has type bool. http://doc.qt.io/qt-5/qabstractitemmodel.html#setData

    No, you just create a new QTableWidgetItem, passing the text as its constructor argument. You can additionally store the actual number through setData(), using Qt::EditRole.
    Could you please provide an example?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Quote Originally Posted by Blitzor DDD View Post
    I meant tbl of course. I am going to use the same tbl and created it on the Widget below. Is it possible?
    If you want to create a new table every time the button is clicked and just keep the old table for viewing, sure, why not.

    Quote Originally Posted by Blitzor DDD View Post
    That is a different setData(), but which of the three arguments is of type bool?
    I see QModelIndex, QVariant and int.

    Quote Originally Posted by Blitzor DDD View Post
    Could you please provide an example?
    Sure, basic C++:
    Qt Code:
    1. item->setData(Qt::EditRole, value);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    So, I tried this, but program goes out since I click "calculate"

    Qt Code:
    1. #include "widget.h"
    2. #include <QAbstractItemModel>
    3.  
    4. Widget::Widget(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. this->setGeometry(30,30,800,750);
    8. rank = new QLineEdit("Enter the Rank", this);//rank->setGeometry(10,0,20,20);
    9. ok = new QPushButton("Ok", this); ok->setGeometry(150, 0,80,30);
    10. calculate = new QPushButton("calculate",this); calculate->setGeometry(250,0,80,30);
    11.  
    12.  
    13.  
    14. connect(ok,SIGNAL(clicked()),this,SLOT(ok_clicked()));
    15. connect(calculate,SIGNAL(clicked()),this,SLOT(calculate_clicked()));
    16. }
    17.  
    18. Widget::~Widget()
    19. {
    20.  
    21. }
    22.  
    23. void Widget::ok_clicked()
    24. {
    25.  
    26. rank_of_Matrix = rank->text().toDouble();
    27. tbl=new QTableWidget(rank_of_Matrix,rank_of_Matrix,this);
    28. tbl->setGeometry(20,50,770,250);
    29. tbl->show();
    30. }
    31.  
    32. void Widget::calculate_clicked()
    33. {
    34. tbl_2 = new QTableWidget(rank_of_Matrix,rank_of_Matrix,this);
    35.  
    36. tbl_2 ->setGeometry(20,350,770,250);
    37. Action_toArray();
    38. tbl_2 ->show();
    39. }
    40.  
    41. void Widget::Action_toArray()
    42. {
    43.  
    44. A= new float* [rank_of_Matrix];
    45. for(int i=0;i<rank_of_Matrix;i++){
    46. A[i]=new float [rank_of_Matrix];
    47. }
    48.  
    49. //read from Table 1 tbl
    50. for(int i=0;i<rank_of_Matrix;i++){
    51. for(int j=0;j<rank_of_Matrix;j++){
    52.  
    53. A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
    54. }
    55. }
    56.  
    57. //operation with Array
    58. for(int i=0;i<rank_of_Matrix;i++){
    59. for(int j=0;j<rank_of_Matrix;j++){
    60. if(A[i][j]==2)
    61. {
    62. A[i][j]=0;
    63. }
    64. }
    65. }
    66.  
    67. //write to Table2 tbl_2
    68. QVariant tableData;
    69. for(int i=0;i<rank_of_Matrix;i++){
    70. for(int j=0;j<rank_of_Matrix;j++){
    71. tableData = A[i][j];
    72. tbl_2->item(i,j)->setData(Qt::EditRole,tableData);
    73. }
    74. }
    75. }
    To copy to clipboard, switch view to plain text mode 

    So, could you please tell me what is wrong and how I can fix it?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    You are accessing tbl->item(i, j), where did you create these items?
    You are also accessing tbl_2->item(i, j), where did you create these items?

    Cheers,
    _

  7. #7
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    I thought, that item is a function to access to element of the Table and this is not an object to create it, isn't it?

    this is header-file

    .h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QTableWidget>
    6. #include <QLabel>
    7. #include <QLineEdit>
    8. #include <QPushButton>
    9. #include <QTableWidgetItem>
    10.  
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Widget(QWidget *parent = 0);
    17. ~Widget();
    18.  
    19. private:
    20. QTableWidget * tbl;
    21. QTableWidgetItem * ptwi = 0;
    22. QStringList * lst;
    23. QLineEdit * rank;
    24. QPushButton * calculate;
    25.  
    26. int rank_of_Matrix=1;
    27. float x=0;
    28. float **A;
    29.  
    30.  
    31. private slots:
    32. void ok_clicked();
    33. void calculate_clicked();
    34. void Action_toArray();
    35. };
    36.  
    37. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Quote Originally Posted by Blitzor DDD View Post
    I thought, that item is a function to access to element of the Table and this is not an object to create it, isn't it?
    QTableWidget::item():
    Returns the item for the given row and column if one has been set; otherwise returns 0.
    You don't seem to set items anywhere, so it is most likely that you are getting a null pointer back.
    You don't check for "not null" so your crash is very likely the attempted dereferencing of that null pointer.

    Cheers,
    _

  9. #9
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Ok, I added line in .h
    QTableWidgetItem * item;
    and in .cpp
    item = new QTableWidgetItem();

    but nevetherless, here:

    Qt Code:
    1. for(int i=0;i<rank_of_Matrix;i++){
    2. for(int j=0;j<rank_of_Matrix;j++){
    3. //записали в массив то, что есть в таблице 1
    4. A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
    5. qDebug() <<"А"<<i<<" "<<j<<" = "<<A[i][j];
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I checked with qDebug() and it worked, data has been set into Array

    and item in this loop has nothing to do with item connected to QTableWidget....

    So, I still have no idea what is going on and what should I do with that...
    Could you please tell me again?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Quote Originally Posted by Blitzor DDD View Post
    Ok, I added line in .h
    QTableWidgetItem * item;
    And you did this why?

    Quote Originally Posted by Blitzor DDD View Post
    and in .cpp
    item = new QTableWidgetItem();
    So you have a single tabel widget item stored in a member of the class.
    How will that help solve your problem?

    Quote Originally Posted by Blitzor DDD View Post
    Qt Code:
    1. for(int i=0;i<rank_of_Matrix;i++){
    2. for(int j=0;j<rank_of_Matrix;j++){
    3. //записали в массив то, что есть в таблице 1
    4. A[i][j] = tbl->item(i,j)-> data(Qt::DisplayRole).toFloat();
    5. qDebug() <<"А"<<i<<" "<<j<<" = "<<A[i][j];
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I checked with qDebug() and it worked, data has been set into Array
    Oh, maybe you have manually entered data into that table?

    Quote Originally Posted by Blitzor DDD View Post
    So, I still have no idea what is going on and what should I do with that...
    Could you please tell me again?
    Check the stack trace of your program when it crashes.

    I am pretty sure it crashes in
    Qt Code:
    1. tbl_2->item(i,j)->setData(Qt::EditRole,tableData);
    To copy to clipboard, switch view to plain text mode 
    because you are calling setData() on a null pointer.

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    Blitzor DDD (17th July 2016)

  12. #11
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Yes, you are right.
    And yes I enter it manually.

    Problem was solved when I changed problem line to:
    tbl_2->setItem(i,j, ptwi = new QTableWidgetItem(QString::number( A[i][j] )));

  13. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    @BlitzorDDD: You could save yourself a lot of time and head-pounding if you used Google and looked up some of the many, many examples on how to use QTableWidget:

    https://wiki.qt.io/How_to_Use_QTableWidget
    http://www.codeprogress.com/cpp/libr...m=QTableWidget

    and you would make many fewer mistakes if you learned how to write a good Qt application by studying the examples and tutorials on the Qt web site:

    http://doc.qt.io/qt-5/qtexamplesandtutorials.html

    We could spend many hours fixing each new bug you create, or you could teach yourself not to make bugs in the first place.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #13
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Yes, yes, I would and I will.
    This is just my third program on Qt.
    Thank you very much for your links!

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write new data into QTableWidgetItem, convert float into QTableWidgetItem

    Quote Originally Posted by Blitzor DDD View Post
    Yes, you are right.
    And yes I enter it manually.
    Which, unsurprisingly, results in items being added to the table.
    Your tbl_2, on the other hand, remains empty.

    Quote Originally Posted by Blitzor DDD View Post
    Problem was solved when I changed problem line to:
    tbl_2->setItem(i,j, ptwi = new QTableWidgetItem(QString::number( A[i][j] )));
    Not sure what you have the "ptwi" in there for, but yes.
    As already clarified in comment #2, btw.

    Cheers,
    _

Similar Threads

  1. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2011, 07:35
  2. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2011, 08:59
  3. How to subclass QTableWidgetItem
    By codemonkey in forum Qt Programming
    Replies: 4
    Last Post: 4th October 2009, 12:11
  4. Im became fool with QTableWidgetItem
    By zorro68 in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2007, 13:17
  5. 3 Different Color for in QTablewidgetItem
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2006, 12:38

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.