Results 1 to 8 of 8

Thread: Qtableview, insert and selection

  1. #1
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11

    Default Qtableview, insert and selection

    Hi There,

    just playing around with Qtableview.
    What I want to do is insert a new row, scroll to it, and show it selected.

    I have a working insert with the following code connected to a button

    Qt Code:
    1. private void insert()
    2. {
    3. String Nr="999";
    4. model.insertRow(model.rowCount());
    5. model.setData(model.index(model.rowCount()-1,0), Integer.parseInt(Nr));
    6. }
    To copy to clipboard, switch view to plain text mode 

    I achieved to scroll down the view by doing:

    Qt Code:
    1. insertButton.clicked.connect(view, "scrollToBottom()");
    To copy to clipboard, switch view to plain text mode 

    But I can't accomplish to select the inserted row automaticly.
    Tried view.selectrow in my insert routine and fumbled with a selectionmodel but all failed in the end
    with a NullPointerException.

    I' am aware this is a very basic question and tried my best to squeeze the answer out of google, but
    nothing got me on track, so any hint would be greatly appreciated.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qtableview, insert and selection

    Your try with a selectionmodel is the correct one.
    Create a selectionmodel and attach it to your tableview.
    Then use the selectionmodel to select the modelindex of your inserted row.

  3. The following user says thank you to tbscope for this useful post:

    genomega (2nd May 2010)

  4. #3
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11

    Default Re: Qtableview, insert and selection

    thanks a lot for your quick respond.

    I connected the selectionmodel to the view as propsed and added

    Qt Code:
    1. SM.select(model.index(model.rowCount()-1, 0),QItemSelectionModel.SelectionFlag.Select );
    To copy to clipboard, switch view to plain text mode 

    to my insert routine, wich gives me again a NullPointerException.

    I miss something, do I!?

  5. #4
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11

    Default Re: Qtableview, insert and selection

    ah, sorted out the NullPointerException which was nothing but a typo.
    So, now I don't get any errors anymore, but the row isn't selected either.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qtableview, insert and selection


  7. #6
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11

    Unhappy Re: Qtableview, insert and selection

    Quote Originally Posted by Lykurg View Post
    thanks, for responding...
    that was my first approach,I tried:

    Qt Code:
    1. view.selectRow(model.rowCount()-1)
    To copy to clipboard, switch view to plain text mode 

    no error message, but also no selected row.
    Tried also simply selecting row nr. 1, same effect -> nothing.

    Here is some more info about how I bound model & view & SM together:
    Qt Code:
    1. model.setTable(eTable);
    2. model.sort(0, Qt.SortOrder.AscendingOrder);
    3. model.setEditStrategy(QSqlTableModel.EditStrategy.OnManualSubmit);
    4. model.select();
    5. view.setModel(model);
    6. view.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows);
    7. view.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection);
    8. view.setSelectionModel(SM);
    To copy to clipboard, switch view to plain text mode 

    Maybe something wrong there?

  8. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qtableview, insert and selection

    Here's an example with a simple QStringListModel.

    I have created a window with a listview, a lineedit and a pushbutton.

    Define the following variables:
    QStringListModel *model;
    QItemSelectionModel *selectionModel;
    QStringList data;

    Then assign them to the listview and connect the clicked signal of the button to a slot.
    data.clear();
    model = new QStringListModel(this);
    model->setStringList(data);
    selectionModel = new QItemSelectionModel(model);
    ui->listView->setModel(model);
    ui->listView->setSelectionModel(selectionModel);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(addAndSelect()));

    The most interesting happens inside the slot:
    void MainWindow::addAndSelect()
    {
    data << ui->lineEdit->text();
    model->setStringList(data);
    QModelIndex index = model->index(data.count()-1, 0);
    selectionModel->setCurrentIndex(index, QItemSelectionModel::Select);
    }

    This is what happens:
    1. Add the new line to the stringlist data;
    2. Reset the stringlist in the stringlistmodel;
    3. Get the modelindex of the last item;
    4. Set the current item in the selectionmodel to the last item.
    Last edited by tbscope; 2nd May 2010 at 09:38. Reason: qtclass tag is not for showing sourcecode, but what is?

  9. #8
    Join Date
    May 2010
    Posts
    9
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11

    Thumbs up Re: Qtableview, insert and selection

    Hi tbscope,

    thanks for your detailed example.
    Pushed me in the right direction, even though I can't see why me first experiments with
    selectionmodel didn't worked out. Maybe something wrong with obtaining the index from the model.

    Anyway, after I revised my code according to your approach, everything is working fine.

    Thanks again.

Similar Threads

  1. Replies: 0
    Last Post: 21st April 2010, 17:25
  2. Replies: 2
    Last Post: 26th November 2009, 05:45
  3. Insert data to QTableView
    By Lodhart in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 10:38
  4. Select row in QTableView after insert.
    By fede in forum Newbie
    Replies: 1
    Last Post: 14th April 2009, 16:18
  5. Insert QCheckBox into QTableView
    By wind in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2006, 17:15

Tags for this Thread

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.