Results 1 to 7 of 7

Thread: Problem with QModelIndex

  1. #1
    Join Date
    Oct 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problem with QModelIndex

    Hi everybody, I'm writing a program with QT 4.4
    I'm writing a program to read email from a SQLite database, I used a QSqlQueryModel and a QTableView, when I select a row of the table it returns a QModelIndex
    Qt Code:
    1. connect(myTable, SIGNAL(clicked(QModelIndex)),myTextBrowser, SLOT(dispayMail(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    I want that instead a QModelIndex it returns the email id, or else, how i get the email id from the QModelIndex?

    Here is the code: (just a part of it, if you need more I will post it)

    Qt Code:
    1. QSqlQuery query("SELECT email.id,addresses.name,email.subject,email.date FROM email,addresses WHERE addresses.id=email.'from'");
    2. model = new QSqlQueryModel;
    3. model->setQuery(query);
    4. model->removeColumn(0); // don't show the ID
    5. model->setHeaderData(0, Qt::Horizontal, tr("Da"));
    6. model->setHeaderData(1, Qt::Horizontal, tr("Soggetto"));
    7. model->setHeaderData(2, Qt::Horizontal, tr("Data"));
    8.  
    9. this->setShowGrid(FALSE);
    10. this->verticalHeader()->hide();
    11. this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    12. this->setSelectionMode(QAbstractItemView::ContiguousSelection);
    13. this->setSelectionBehavior(QAbstractItemView::SelectRows);
    14. this->setSelectionMode(QAbstractItemView::ExtendedSelection);
    15. this->setModel(model);
    16. this->show();
    To copy to clipboard, switch view to plain text mode 

    Thanks

    Ps: Sorry for my english, but I'm swiss
    Last edited by wysota; 30th October 2008 at 07:54. Reason: Changed [qtclass] to [code]

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QModelIndex

    Qt Code:
    1. QString email = index.data(Qt::EditRole).toString();
    To copy to clipboard, switch view to plain text mode 

    PS. please, use tags code.

  3. #3
    Join Date
    Oct 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with QModelIndex

    Quote Originally Posted by spirit View Post
    Qt Code:
    1. QString email = index.data(Qt::EditRole).toString();
    To copy to clipboard, switch view to plain text mode 

    PS. please, use tags code.
    Thanks a lot for the quick answer.
    If I use your code I get the content of the cell I clicked, but I want get the Id (from the database) of the row I clicked
    this is the table

    Da | Soggetto | Data

    and this is the query

    Qt Code:
    1. QSqlQuery query("SELECT email.id,addresses.name,email.subject,email.date FROM email,addresses WHERE addresses.id=email.'from'");
    To copy to clipboard, switch view to plain text mode 
    I just don't display the id in the QTableView

    Thanks

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QModelIndex

    then try this
    Qt Code:
    1. ...
    2. if (!index.isValid())
    3. return;
    4.  
    5. if (!index.column())
    6. idx = index;
    7. else
    8. idx = index.model()->index(0, index.row());//0 -- id field
    9.  
    10. int id = index.data(Qt::EditRole).toInt();
    11. ...
    To copy to clipboard, switch view to plain text mode 
    or you also can determinate id column by name and then pass it in method index.
    it's just an example/

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QModelIndex

    or you can use this method
    QSqlRecord QSqlQueryModel::record ( int row ) const
    Qt Code:
    1. if (!index.isValid())
    2. return;
    3.  
    4. int id = qobject_cast<QSqlTableModel *>(index.model())->record(index.row()).value("email.id").toInt();
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 30th October 2008 at 08:27. Reason: updated contents

  6. #6
    Join Date
    Oct 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with QModelIndex

    Thanks a lot man, you are a genius
    I used this
    Qt Code:
    1. ...
    2. if (!index.isValid())
    3. return;
    4.  
    5. if (!index.column())
    6. idx = index;
    7. else
    8. idx = index.model()->index(0, index.row());//0 -- id field
    9.  
    10. int id = index.data(Qt::EditRole).toInt();
    11. ...
    To copy to clipboard, switch view to plain text mode 

    and it worked fine.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QModelIndex

    Quote Originally Posted by Kesy View Post
    Thanks a lot man, you are a genius
    I used this
    Qt Code:
    1. ...
    2. if (!index.isValid())
    3. return;
    4.  
    5. if (!index.column())
    6. idx = index;
    7. else
    8. idx = index.model()->index(0, index.row());//0 -- id field
    9.  
    10. int id = index.data(Qt::EditRole).toInt();
    11. ...
    To copy to clipboard, switch view to plain text mode 

    and it worked fine.
    you are wellcome. but I suggest you to use code from my last post, it is more clear to use (for this task), but in general case this one more preferred.

Similar Threads

  1. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14
  2. QModelIndex problem!
    By landonmkelsey in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2008, 19:46
  3. Problem with TreeView
    By init2null in forum Qt Programming
    Replies: 8
    Last Post: 25th May 2008, 09:56
  4. QAbstractItemModel newbie question
    By okellogg in forum Qt Programming
    Replies: 14
    Last Post: 18th February 2008, 12:30
  5. Replies: 6
    Last Post: 21st September 2007, 13:51

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.