Results 1 to 4 of 4

Thread: getting current Id from tableView

  1. #1
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default getting current Id from tableView

    I took this example from Qt4 examples.

    model= new QSqlRelationalTableModel(this);
    model->setTable("person");
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
    model->select();
    tableView->setModel(model);


    I added
    QObject::connect(tableView, SIGNAL(clicked(QModelIndex)), getItem, SLOT(click()));

    Now i want to take person.id from this model in the current selection. I need an example. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: getting current Id from tableView

    Make your slot accept a const QModelIndex& parameter and use QModelIndex::data() to fetch the data from the 0th column. This will be your id.

  3. #3
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting current Id from tableView

    QModelIndex retorno = tableView->currentIndex();
    int row = retorno.row();
    QVariant ok = model->data(model->index(row,0) );
    qDebug("ID:%i ", ok.toInt() );

    I made like this.
    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: getting current Id from tableView

    If you have a "clicked" signal that emits the index that was clicked, there is no point in retrieving the index manually again.

Similar Threads

  1. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  2. Drag an item from QTreeWidget and drop in TableView
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2007, 11:42
  3. Replies: 15
    Last Post: 21st April 2007, 17:46
  4. Replies: 1
    Last Post: 19th April 2007, 22:23
  5. Modified tableview
    By dkite in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2006, 01:58

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.