Results 1 to 17 of 17

Thread: QTableWidget save data

  1. #1
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default QTableWidget save data

    Hello All,

    When menu option -> one. is clicked it will create and displays a grid using QTableWidget. After filling the cells.

    another menu option -> two, when pressed this will save the data to .txt or csv file.

    It crashes. because the link is not working properly.

    How to communicate them, using connect signals or any other method. Help me with small example to save data.

    Thank you,
    Answers Appreciated.
    Last edited by marvic_44; 25th December 2016 at 19:53. Reason: spelling

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    And what does the debugger tell you about the reason for the crash?
    <=== 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.

  3. #3
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    Exact error in Save() slot, when j=3 it will crash. in attach error.jpg
    Attached Images Attached Images
    Last edited by marvic_44; 26th December 2016 at 10:05. Reason: image change

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

    Default Re: QTableWidget save data

    You are calling write() on all items without checking if the item exists.
    Are you sure all your table cells are non-empty?

    Cheers,
    _

  5. #5
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    Quote Originally Posted by anda_skoa View Post
    You are calling write() on all items without checking if the item exists.
    Are you sure all your table cells are non-empty?

    Cheers,
    _
    Yes, all the table cells are filled.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    Yes, all the table cells are filled.
    But nonetheless, you are not checking to see that "m_table->item(i,j)" is not NULL before using the pointer to call the write() method. The cause of your crash is almost certainly a NULL pointer to a table item.
    <=== 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.

  7. #7
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    Thank you, how to do that, simple example line.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    Thank you, how to do that, simple example line.
    Are you serious? You don't know how to write code that checks to see if a pointer is not NULL in C++? Go back to your C++ textbook and look it up.
    <=== 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.

  9. #9
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    Ok, now it able to save the data using menu option save. But, on second time opening table is crashing,
    before entering the data in table cells. Debug attach.
    string-error.jpg

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    The screenshot is useless. All it says is that the debugger is looking for qstring.cpp and can't find it.

    You need to run your program in the debugger, set a breakpoint where you "open the table" (whatever that means) and step through the code until you see the cause of the crash.
    <=== 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.

  11. #11
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    That screen shot is raised during debugging using debugger.

    Qt Code:
    1. In .h
    2. QTableWidget *m_table;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. m_table = new QTableWidget();
    4. }
    5.  
    6.  
    7. void MainWindow::newFile()
    8. {
    9.  
    10. //Set table row count 4 and column count 5
    11. m_table->setRowCount(4);
    12. m_table->setColumnCount(5);
    13.  
    14. QFont font;
    15. font.setBold(true);
    16. font.setStyleName("Arial");
    17. m_table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    18.  
    19. //Add Table items here
    20. m_table->setItem(0, 0, new QTableWidgetItem("One"));
    21. m_table->item(0, 0)->setFont(font);
    22. m_table->setItem(1, 0, new QTableWidgetItem("Two"));
    23. m_table->item(1, 0)->setFont(font);
    24. m_table->setItem(2, 0, new QTableWidgetItem("Three"));
    25. m_table->item(2, 0)->setFont(font);
    26. m_table->setItem(3, 0, new QTableWidgetItem("Four"));
    27. m_table->item(3, 0)->setFont(font);
    28.  
    29. m_table->verticalHeader()->hide();
    30. m_table->horizontalHeader()->hide();
    31.  
    32. setCentralWidget(m_table);
    33. }
    To copy to clipboard, switch view to plain text mode 
    After setCentralWidget, that screen raised.
    Last edited by marvic_44; 31st December 2016 at 05:21.

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    Move this

    Qt Code:
    1. setCentralWidget(m_table);
    To copy to clipboard, switch view to plain text mode 

    to the constructor, and modify the constructor like this:

    Qt Code:
    1. MainWindow::MainWindow( QObject * parent )
    2. : QMainWindow( parent )
    3. {
    4. m_table = new QTableWidget( this );
    5. setCentralWidget(m_table);
    6. }
    To copy to clipboard, switch view to plain text mode 

    And in the newFile() slot, call "m_table->clearContents()" before adding new items to the table. If all of your items have the same font, then you can call setFont() on the table when you create it in the constructor and all new items wil inherit that setting.

    I don't know what else you are doing wrong, but I am guessing you are still not doing enough error checking to see that the pointers you are getting are valid before using them.
    <=== 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.

  13. #13
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    If the menu options, except new and save are disabled , it is not crashing. If it reversed it, works fine. (disabling new and save, enable other options)

    All the other menu options do display some data, it means "setCentralWidget()" function is called in all the options (slots) to display the data.

    I think this will not work properly. Unless if there a function to deactivate the central widget. before or after entering into the new slots.

    Thanks for help.

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    it means "setCentralWidget()" function is called in all the options (slots) to display the data.
    No, this idea is completely wrong. You do not seem to understand the purpose of this function. It is not to load data into the table, it is to tell QMainWindow that the QTableWidget is the main widget in the application window, and that all of the other widgets (menu, toolbar, dock widgets, etc.) are to be laid out around it. As I showed in the last post, you should call the method once, when you construct the main window, not every time you want to update the table. At the same time, you also make the connections between signals and slots for the menu items, table, and whatever other GUI elements you have that need to talk to each other. You do this once, in the MainWindow constructor.

    The fact that you can do random things with your menus and it causes crashes tells me that you have memory bugs in a large part of your code, once again probably due to using pointers that are not valid. It could be due to uninitialized pointers, pointers to objects that have been deleted, or failing to check that a pointer returned by some other function is not NULL before you use it. It could also be due to trying to access an element of an array that is out-of-bounds, but in your case I think it is probably trying to use a bad pointer.
    <=== 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.

  15. #15
    Join Date
    Dec 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded

    Default Re: QTableWidget save data

    What i mean to say is, "setCentralWidget()" this function is used in all the menu options (slots). Not in particular function or slots.

    Switching between the menu options work fine only when i do not press new and save menu options which deals with QtableWidget.

    or

    If other options are disable and except new and save options works fine, means table created and data stored in file properly.

    When i switch between all the options, it crashes when back to new option ie QTableWidget.

    This kind features worked fine before, One option for widgets other options for different displays and saving data etc. Even i remember worked perfectly in Symbian S60 and UIQ.
    Last edited by marvic_44; 2nd January 2017 at 18:46.

  16. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTableWidget save data

    What i mean to say is, "setCentralWidget()" this function is used in all the menu options (slots). Not in particular function or slots.
    Yes, and that is completely wrong. It should be used once in the MainWindow constructor.

    Sorry, you obviously are not interested in learning from me how to fix your program to make it correct and not crash. Maybe someone else will be more interested in helping you. Good luck.
    <=== 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.

  17. #17
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6

    Default Re: QTableWidget save data

    Please read QMainWindow documentation, understand the layout used by Qt and the purpose of setCentralWidget(). And follow the suggestions of d_stranz.

Similar Threads

  1. Save binary data into XML file
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2013, 12:16
  2. Replies: 19
    Last Post: 8th August 2011, 10:11
  3. Save added Data to a GUI
    By bostero22 in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2010, 16:27
  4. Replies: 2
    Last Post: 8th August 2008, 02:54
  5. EditStrategy, how to save data
    By JeanC in forum Qt Programming
    Replies: 33
    Last Post: 9th January 2008, 17:31

Tags for this Thread

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.