Results 1 to 3 of 3

Thread: How to forward primary key of selected row QTableView

  1. #1
    Join Date
    Feb 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default How to forward primary key of selected row QTableView

    Hi everyone, i am new in programming world, so please forgive me if i ask stupid question....
    I was make my Sql connection to database and use QSqlTableModel to fill QSqlTableView. This is my code:
    ui->setupUi(this);
    QSqlTableModel *model=new QSqlTableModel(this);
    model->setTable("baza");
    model->select();
    ui->tableRadnici->setModel(model);
    ui->tableRadnici->setEditTriggers(QAbstractItemView::NoEditTriggers );
    and table "baza":
    mysql> describe baza;
    +----------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +----------+--------------+------+-----+---------+-------+
    | id | int(14) | NO | PRI | NULL | |
    | name | varchar(100) | YES | | NULL | |
    | lastname | varchar(100) | YES | | NULL | |
    | sector | varchar(100) | YES | | NULL | |
    +----------+--------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)

    My question is how can i forward id of table row to some function, example if i do double click on field name to forward value of field ID to function
    void DialogListaRadnika:n_tableRadnici_activated(const QModelIndex &index);

    I was looking for solution for about mount, and i fail with that, so please help me.
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to forward primary key of selected row QTableView

    You have the model index of whatever cell got activated.
    You can use the row information to get an index of the first column, which holds your id.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to forward primary key of selected row QTableView

    Thanks for quick replay
    I was solve this problem by this syntax
    ui->tableRadnici->model()->data(ui->tableRadnici->model()->index(index.row(),0)).toString();
    where ui->tableRadnici is my QTableView in my form.
    Cheers

Similar Threads

  1. QTableView, selected row?
    By postb99 in forum Newbie
    Replies: 1
    Last Post: 25th February 2014, 14:57
  2. QTableView get selected row
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 12th July 2013, 03:35
  3. Replies: 2
    Last Post: 21st January 2011, 17:12
  4. QTableView Signal when nothing is selected
    By djjkotze in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2010, 15:00
  5. Deleting selected row from a QTableView
    By Ferric in forum Newbie
    Replies: 9
    Last Post: 15th January 2010, 08:32

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.