PDA

View Full Version : How to save Qstring from QTableView



wodzu-96
23rd October 2011, 16:01
Hello
I have been writing in qt for 2 months, so I am green and I have one problem. I want to save every cell of table ms sql in QString. I use driver QODBC. This is what I've already written:

QString *copy;
copy=new QString[20];
QItemSelectionModel *selectionM = ui->tableView->selectionModel();
QModelIndexList selectionL = selectionM->selectedIndexes();
selectionL.takeFirst();
for (int h=0; h<20; h++){
copy[h] = model->data(selectionL.takeFirst()).toString();
}
QMessageBox::information(this, "Kopiowanie", copy[1]);
When I want to open this function in the program which I have written there appears a notification about the error : ,,This application has requested the Runtime to terminate it in an unusual way." Thanks in advance and I count for a simple explanation

d_stranz
23rd October 2011, 22:00
That error is the standard Microsoft message and tells you nothing except that your program has a fatal runtime error. Running a release build of a program cannot help you solve runtime errors.

You first need to build a debug version of your application and run it in a debugger. Set a breakpoint on the first executable line of your code above, then step along until you see where the error occurs. My guess is that your selection model pointer is probably NULL, and when you use the NULL pointer to get the selectedIndexes() the program crashes. You are also using a variable called "model"; if this is NULL, using it to call the data() method will also cause a crash.

ChrisW67
24th October 2011, 03:07
... or the selectionList is empty and you are blindly executing takeFirst() (line 5 and 7) which assumes that the list is not empty. If you do not want the removed value returned then removeFirst() is the function for you (line 5).
... or there are not 21 selected items in the list and you exhaust the list before you reach the end of the loop. See above.

Before you go much further you should replace your heap-allocated C-style array of QStrings with a QStringList (or other managed container) because it will save you a whole bunch of effort worrying about memory management and null pointers.

It's not clear what the relationship between the user's selected indexes and "every cell of table ms sql" is. You can get all the records from the table without reference to the user selection. Perhaps you have not explained your requirement very well.

wodzu-96
24th October 2011, 16:36
Thank you very much for response, but i have still this error. When I run this function I see in qt error like this:
ASSERT: "!isEmpty()" in file c:\QtSDK\Desktop\Qt\4.7.3\mingw\include/QtCore/qlist.h, line 273
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
When i read your response I modify my code and i have this:

QString copy;
QStringList list;
QItemSelectionModel *selectionM = ui->tableView->selectionModel();
QModelIndexList selectionL = selectionM->selectedIndexes();
selectionL.removeFirst();
foreach ( const QModelIndex& index, selectionL)
{
list = index.data().toStringList() ;
}
copy =list.join(", ");
QMessageBox::information(this, "Kopiowanie", copy);
This error results from "selectionL.removeFirst();" but when i removed this line i don't have a char in QString. Sorry for my language but I live in Poland.

R-Type
24th October 2011, 16:47
selectedIndexes() gives you only selection and it seems you haven't selected anything. Threrfore selectionL contains nothing and you have to check whether selectionL empty or not before removeFirst() call.
Overwriting list inside loop is also obviously not what you want. You probably want to append data to the list, so list.append() should be the way.

wodzu-96
24th October 2011, 17:42
Ok I check this and selectionL is empty but i don't know why and what should I do. Sorry but I am inexperianced. I create table in this function:

model = new QSqlTableModel(this);
model ->setTable(ntabeli);
model ->select();
ui->tableView->setModel(model);