Results 1 to 6 of 6

Thread: How to save Qstring from QTableView

  1. #1

    Default How to save Qstring from QTableView

    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:
    Qt Code:
    1. QString *copy;
    2. copy=new QString[20];
    3. QItemSelectionModel *selectionM = ui->tableView->selectionModel();
    4. QModelIndexList selectionL = selectionM->selectedIndexes();
    5. selectionL.takeFirst();
    6. for (int h=0; h<20; h++){
    7. copy[h] = model->data(selectionL.takeFirst()).toString();
    8. }
    9. QMessageBox::information(this, "Kopiowanie", copy[1]);
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by wodzu-96; 23rd October 2011 at 17:12.

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

    Default Re: How to save Qstring from QTableView

    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.

  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: How to save Qstring from QTableView

    ... 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.

  4. #4

    Default Re: How to save Qstring from QTableView

    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:
    Qt Code:
    1. QString copy;
    2. QItemSelectionModel *selectionM = ui->tableView->selectionModel();
    3. QModelIndexList selectionL = selectionM->selectedIndexes();
    4. selectionL.removeFirst();
    5. foreach ( const QModelIndex& index, selectionL)
    6. {
    7. list = index.data().toStringList() ;
    8. }
    9. copy =list.join(", ");
    10. QMessageBox::information(this, "Kopiowanie", copy);
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to save Qstring from QTableView

    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.

  6. #6

    Default Re: How to save Qstring from QTableView

    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:
    Qt Code:
    1. model = new QSqlTableModel(this);
    2. model ->setTable(ntabeli);
    3. model ->select();
    4. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    Last edited by wodzu-96; 24th October 2011 at 20:55.

Similar Threads

  1. Replies: 2
    Last Post: 11th August 2011, 15:42
  2. Replies: 8
    Last Post: 25th November 2010, 11:40
  3. Replies: 3
    Last Post: 1st April 2010, 23:56
  4. Replies: 4
    Last Post: 1st February 2010, 14:21
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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