Results 1 to 7 of 7

Thread: How to read data from column 0 when I've a QModelIndex

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default How to read data from column 0 when I've a QModelIndex

    Hi,

    I have const QModelIndex &index and I can read the data by example:
    Qt Code:
    1. index.model()->data(index, Qt::DisplayRole).toString()
    To copy to clipboard, switch view to plain text mode 

    but how can I read the data from column = 0 (or any other column) and row = index.row() from this same model?

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to read data from column 0 when I've a QModelIndex

    Qt Code:
    1. QAbstractItemModel * model = index.model();
    2. model->data(model->index(row, col), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read data from column 0 when I've a QModelIndex

    Quote Originally Posted by numbat View Post
    Qt Code:
    1. QAbstractItemModel * model = index.model();
    2. model->data(model->index(row, col), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    Tanks, but I get:

    error: invalid conversion from `const QAbstractItemModel*' to `QAbstractItemModel*'
    in line:
    Qt Code:
    1. QAbstractItemModel * model = index.model();
    To copy to clipboard, switch view to plain text mode 

    :/

  4. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to read data from column 0 when I've a QModelIndex

    Try:
    Qt Code:
    1. const QAbstractItemModel * model = index.model();
    2. model->data(model->index(row, col), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: How to read data from column 0 when I've a QModelIndex

    Would you kindly add more of your code, I have a similar questions and I found this post, I'm having problem accessing the right data.

    in my main constructor, I have the following connect statement:
    Qt Code:
    1. connect( viewAllPatients, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(rowDoubleClicked(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    Then, using an example I found online ... my slot is defined below, but I get error with the view widget 'viewAppPatients' (which is a QTableView)

    Qt Code:
    1. void CentralPage::rowDoubleClicked(const QModelIndex &QMI)
    2. {
    3. QString sRec = "not valid column";
    4. QVariant value = viewAllPatients->model()->data(QMI,0);
    5. if (value.isValid())
    6. sRec = value.toString();
    7. qDebug() << sRec;
    8. }
    To copy to clipboard, switch view to plain text mode 

    So, these posts make wonder where would I define/use AbstracItemModel, because this is how I defined model in my constructor:
    Qt Code:
    1. model->setQuery("SELECT FirstName,LastName FROM Clients");
    2. model->setHeaderData(0,Qt::Horizontal, tr("First"));
    3. model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
    4.  
    5. QTableView *viewAllPatients = new QTableView;
    6. viewAllPatients->setModel(model);
    7. viewAllPatients->setAlternatingRowColors(true);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: How to read data from column 0 when I've a QModelIndex

    typo, I meant:
    I get error with the view widget 'viewAllPatients' (which is a QTableView)

  7. #7
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: How to read data from column 0 when I've a QModelIndex

    Update: I fixed the issue and found why I was getting errors.

    In my header file under private, I defined the QTableView:

    Qt Code:
    1. QTableView *viewAllPatients;
    To copy to clipboard, switch view to plain text mode 
    So my Slot callback reads:
    Qt Code:
    1. void CentralPage::rowDoubleClicked(const QModelIndex &QMI)
    2. {
    3. QString sRec = "not valid column";
    4. QVariant value = viewAllPatients->model()->data(QMI,0);
    5. if (value.isValid())
    6. sRec = value.toString();
    7. qDebug() << sRec;
    8. }
    To copy to clipboard, switch view to plain text mode 
    And now, I don't have the error message:
    Qt Code:
    1. error: ‘viewAllPatients’ was not declared in this scope
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 17:31
  2. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14
  3. How can I read binary data to QString?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 29th July 2008, 20:03
  4. Read Data in formated manner
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 1st January 2008, 12:28
  5. setting data of buffer to QTreeWidget column
    By thomasjoy in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 17:12

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.