Results 1 to 11 of 11

Thread: saving and retrieving the contents of qtabelwidget

  1. #1
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default saving and retrieving the contents of qtabelwidget

    In my application i need to save some information of qtabelwidget .Can any one explain me with an example how to save the contents of Qtabel widget in an array.
    and can i restrict the cell text maximum length like an lineedit.?


    Thanks.......

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: saving and retrieving the contents of qtabelwidget

    To store contents in an array you can convert each row to a QString and save it in an array of QStrings.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QString str;
    8. QString *strArray;
    9. int rowCount, colCount;
    10.  
    11. w.insertRow(0);
    12. w.insertRow(0);
    13. w.insertColumn(0);
    14. w.insertColumn(0);
    15.  
    16.  
    17. item = new QTableWidgetItem("Qt");
    18. w.setItem(0, 0, item);
    19.  
    20. item = new QTableWidgetItem("4.7");
    21. w.setItem(0, 1, item);
    22.  
    23. item = new QTableWidgetItem("Qt");
    24. w.setItem(1, 0, item);
    25.  
    26. item = new QTableWidgetItem("4.8");
    27. w.setItem(1, 1, item);
    28.  
    29. rowCount = w.rowCount();
    30. colCount = w.columnCount();
    31.  
    32. strArray = new QString[rowCount];
    33.  
    34. for (int i = 0; i < rowCount; i++)
    35. {
    36. for (int j = 0; j < colCount; j++)
    37. {
    38. str += w.item(i,j)->text();
    39. str += '\t';
    40. }
    41.  
    42. strArray[i] = str;
    43. str.clear();
    44. }
    45.  
    46. w.insertRow(0);
    47.  
    48. item = new QTableWidgetItem(strArray[0] + strArray[1]); //To view the result
    49. w.setItem(0, 0, item);
    50.  
    51. w.setColumnWidth(0, 300);
    52.  
    53. w.resize(600, 400);
    54. w.show();
    55.  
    56. return app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 

  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: saving and retrieving the contents of qtabelwidget

    Quote Originally Posted by vani.pv View Post
    and can i restrict the cell text maximum length like an lineedit.?
    Yes. Use a QTableWidgetItem subclass that discards/truncates text longer than what ever limit you wish to impose or provide the QTableWidget with a QStyledItemDelegate subclass that provides a length-limited QLineEdit when asked for the editor.

  4. #4
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saving and retrieving the contents of qtabelwidget

    thanks ashkan and chris.

  5. #5
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saving and retrieving the contents of qtabelwidget

    Here is another problem.
    when i am trying to set the max length of a text i dint find any function to do it.

    can i set the text length for a particular column.
    please provide me an example or a piece of code.

    And in my application i want to give an option to the user through a button.when the user clicked on that button a row should be added to my table.
    my code is.

    onbuttonclicked()
    {
    ui->mytablewiget->insertrow(ui->mytablewiget->rowcount());
    }

    but no row is added while running.

    is there any other way>


    Thanks.

  6. #6
    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: saving and retrieving the contents of qtabelwidget

    Quote Originally Posted by vani.pv View Post
    Here is another problem.
    when i am trying to set the max length of a text i dint find any function to do it.
    No, it is the same question you started with.
    can i set the text length for a particular column.
    please provide me an example or a piece of code.
    You already have the answer. Use a delegate or have the model item silently discard the extra characters. The are examples in the docs and this forum.
    Qt Code:
    1. onbuttonclicked()
    2. {
    3. ui->mytablewiget->insertrow(ui->mytablewiget->rowcount());
    4. }
    To copy to clipboard, switch view to plain text mode 
    This will not even compile (unless you have subclassed QTableWidget to add insertrow()). If you are going to post code, copy and paste it from a compiling program and use [code] [/code] tags.

    You have the correct function but unfortunately "but no row is added while running" is an inadequate description of the problem, why you think it is broken, what you have done to diagnose it etc.

  7. #7
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saving and retrieving the contents of qtabelwidget

    i have gone through the documentation but still dont know hoe to use QStyledItemDelegate to limit the text length.can u please explain with an example.?
    and i am trying to read the data from a row.and set the text of columns of perticular row to lineedits.
    i did like this.

    Qtcode:

    void trial:: on_tableWidget_doubleClicked(const QModelIndex &index)
    {
    item = new QTableWidgetItem(0);
    item = ui->tableWidget->itemFromIndex(index);
    }

    while running it shows the error

    /usr/include/QtGui/qtablewidget.h:333: error: 'QTableWidgetItem* QTableWidget::itemFromIndex(const QModelIndex&) const' is protected.

    how t solve it or is there any other way to read the complete row?

  8. #8
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saving and retrieving the contents of qtabelwidget

    Hi chris.
    I try to understand the usage of item delegate but not get the required points.
    so Can u give me the a piece of code to limit the text length of qtablewidget .

  9. #9
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: saving and retrieving the contents of qtabelwidget

    u need to set the property on the cell widget.

    hope it helps
    Bala

  10. #10
    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: saving and retrieving the contents of qtabelwidget

    Quote Originally Posted by vani.pv View Post
    I try to understand the usage of item delegate but not get the required points.
    so Can u give me the a piece of code to limit the text length of qtablewidget .
    It has been a whole month. Have you tried anything at all for yourself? Have you showed what you have tried and asked for help identifying where it fails? Have you asked for help interpreting the documentation you read or examples you have studied?

    Here... a complete example that you can copy and paste without understanding:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class LimitedLengthDelegate : public QStyledItemDelegate
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. LimitedLengthDelegate(QObject *parent = 0): QStyledItemDelegate(parent) { }
    9.  
    10. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    11. {
    12. Q_UNUSED(option);
    13. Q_UNUSED(index);
    14.  
    15. QLineEdit *editor = new QLineEdit(parent);
    16. editor->setMaxLength(7);
    17. return editor;
    18. }
    19. };
    20.  
    21.  
    22. int main(int argc, char **argv)
    23. {
    24. QApplication app(argc, argv);
    25.  
    26. QTableWidget t(3, 3);
    27. t.setItemDelegate(new LimitedLengthDelegate(&t));
    28. t.show();
    29.  
    30. return app.exec();
    31. }
    32. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    The entire working part of the solution is ten lines of code and they follow the pattern described in the docs I gave you directly and the things those pages reference.

  11. #11
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saving and retrieving the contents of qtabelwidget

    thanks chris.

    Yes it was a hole month but i worked for one day only to get item delegate.i have gone through stardelegate example.I understood create editor function n all but dint get any clue for my problem.
    Any way i can get it with ur code....

    Thanku...

Similar Threads

  1. Problem saving txtfile contents to database
    By cutie.monkey in forum Qt Programming
    Replies: 0
    Last Post: 6th January 2010, 07:11
  2. Accessibility and QTabelWidget
    By spacer2004 in forum Qt Programming
    Replies: 0
    Last Post: 11th September 2009, 14:52
  3. QWebView - retrieving information?
    By lamera in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2008, 17:53
  4. Retrieving a file's icon
    By sbeltz in forum Qt Programming
    Replies: 8
    Last Post: 10th September 2008, 12:35
  5. Storing and Retrieving a QFlags
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 4th October 2007, 18:53

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.