Results 1 to 4 of 4

Thread: Select complete row, get/set data in row by columns header names

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Select complete row, get/set data in row by columns header names

    Hello!

    I have two questions about models and hope that you can help me.


    1.)
    I want to select a complete row and read out the data. The following function is called on Signal clicked() from QTableView:

    Qt Code:
    1. QModelIndex index = m_customerTableView->currentIndex();
    2. int i = index.row(); // now you know which record was selected
    3. m_customerTableView->selectRow(i);
    To copy to clipboard, switch view to plain text mode 

    Now the row is selected and I can read out the data using

    Qt Code:
    1. QModelIndexList indexes = m_customerTableView->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

    So far so good, this works, but since I want only one row to be selected I want to use

    Qt Code:
    1. m_customerTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    2. m_customerTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 

    and instead of selectedIndexes() I want to use

    Qt Code:
    1. QModelIndexList indexes = m_customerTableView->selectionModel()->selectedColumns();
    To copy to clipboard, switch view to plain text mode 

    But I could not get this to work. The QModelIndexList is empty.

    Is it possible to use it in the way I want or is the method I mentioned first the better way? Or is there another solution?

    2.)
    So far I read/write the data from/to the rows one field after another. The column-order is set via QMaps, e.g.

    Qt Code:
    1. map_customer.insert(0,"Customer No.");
    2. map_customer.insert(1,"Title");
    3. map_customer.insert(2,"First name");
    4. map_customer.insert(3,"Last name");
    To copy to clipboard, switch view to plain text mode 

    but this is not very low-maintance if something in the data structure is changed.

    Is there an easy possibility to read/write data in a row by the models header name?

    Pseudo-code could be:

    Qt Code:
    1. //set headers
    2. model->setHeaderData(column, Qt::Horizontal, "FirstName");
    3. //write data to column with given header name
    4. int row = 2; //set row to write
    5. model(row, "FirstName")->setData("SomeData")
    To copy to clipboard, switch view to plain text mode 

    Or better solution?

    Thanks in advance.

    Kind regards,
    HomeR

  2. #2
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Select complete row, get/set data in row by columns header names

    Ok, I found an answer to question 2. I will solve this using enums.

    I will search for an answer myself now, but is it possible to pass an enum as a parameter to a function?
    So that it can define all enums in a central place and pass them e.g. to my DataReader to put the data correctly into a model?

  3. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Select complete row, get/set data in row by columns header names

    Why not simply put a few defines into the headerfile of the DataModel?
    Qt Code:
    1. #define COLUMN_FIRST_NAME 0
    2. #define COLUMN_LAST_NAME 1
    3. etc
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Select complete row, get/set data in row by columns header names

    Thanks for you answer. Hm, I guess that would be similar to an enum. But since I am moving away from a xml-file based storage towards an SQLite-database this is easier to handle anyway.

    Kind regards,
    HomeR

Similar Threads

  1. qabstracttablemodel/qtableview: vector data columns
    By xyfix in forum Qt Programming
    Replies: 0
    Last Post: 15th September 2010, 12:51
  2. Selectable header columns
    By eekhoorn12 in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2009, 16:33
  3. Set the same header to two columns
    By ProTonS in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2009, 08:10
  4. Select columns from a QTableWidget
    By toglez in forum Qt Programming
    Replies: 10
    Last Post: 7th October 2007, 15:15
  5. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 16:37

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.