PDA

View Full Version : QTableWidget save data



marvic_44
25th December 2016, 19:47
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.

d_stranz
25th December 2016, 22:57
And what does the debugger tell you about the reason for the crash?

marvic_44
26th December 2016, 08:19
Exact error in Save() slot, when j=3 it will crash. in attach 12266

anda_skoa
26th December 2016, 12:30
You are calling write() on all items without checking if the item exists.
Are you sure all your table cells are non-empty?

Cheers,
_

marvic_44
26th December 2016, 12:37
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.

d_stranz
27th December 2016, 02:45
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.

marvic_44
28th December 2016, 09:23
Thank you, how to do that, simple example line.

d_stranz
29th December 2016, 18:44
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.

marvic_44
30th December 2016, 06:49
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.
12268

d_stranz
30th December 2016, 18:00
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.

marvic_44
31st December 2016, 05:05
That screen shot is raised during debugging using debugger.


In .h
QTableWidget *m_table;

MainWindow::MainWindow()
{
m_table = new QTableWidget();
}


void MainWindow::newFile()
{

//Set table row count 4 and column count 5
m_table->setRowCount(4);
m_table->setColumnCount(5);

QFont font;
font.setBold(true);
font.setStyleName("Arial");
m_table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

//Add Table items here
m_table->setItem(0, 0, new QTableWidgetItem("One"));
m_table->item(0, 0)->setFont(font);
m_table->setItem(1, 0, new QTableWidgetItem("Two"));
m_table->item(1, 0)->setFont(font);
m_table->setItem(2, 0, new QTableWidgetItem("Three"));
m_table->item(2, 0)->setFont(font);
m_table->setItem(3, 0, new QTableWidgetItem("Four"));
m_table->item(3, 0)->setFont(font);

m_table->verticalHeader()->hide();
m_table->horizontalHeader()->hide();

setCentralWidget(m_table);
}

After setCentralWidget, that screen raised.

d_stranz
1st January 2017, 19:25
Move this


setCentralWidget(m_table);

to the constructor, and modify the constructor like this:


MainWindow::MainWindow( QObject * parent )
: QMainWindow( parent )
{
m_table = new QTableWidget( this );
setCentralWidget(m_table);
}


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.

marvic_44
2nd January 2017, 14:26
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.

d_stranz
2nd January 2017, 17:43
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.

marvic_44
2nd January 2017, 18:39
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.

d_stranz
2nd January 2017, 22:07
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.

NameRakes
3rd January 2017, 00:39
Please read QMainWindow documentation, understand the layout used by Qt and the purpose of setCentralWidget(). And follow the suggestions of d_stranz.