Results 1 to 4 of 4

Thread: QTableView and selectionModel problem

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableView and selectionModel problem

    Hi,

    I have a QWidget with QTableView inside.When add button is clicked it opens a dialog.When dialog is closed it updates the data editing the records.
    I use the signal:
    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 
    to check whether row was selected and if is true it enables the button because otherwise the app crushes.
    The problem is that when i update the model this signal doesn't work.What i mean: when i open the widget everything works properly, when row is selected the button becomes enabled when is not selected become disabled.But, when i open the dialog and update the model editing some of the records and close the dialog the buttons remain only enabled or only disabled which means that selectionModel is not available or something like that.

    HELP PLEASE

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and selectionModel problem

    You need to rephrase your question. At least I am not able to get the picture. I think you are implying that selectionChanged() is not getting emitted. As you might know selectionChanged() only works on qmodelindexes. If the data pointed by qmodelindex is changed, selectionChanged() will not be emitted.
    You should rather look at the dataChanged() of the model.

  3. #3
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView and selectionModel problem

    OK, i'll try to make my question clearer:

    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 
    i use selectionChange() to check in setBool() wether to make buttons disabled or enabled. Generally it works, but when i add or edit record i run the following function in order to update the table:
    Qt Code:
    1. //UPDATE MAIN TABLEVIEW
    2. void Clients::updateClientTable()
    3. {
    4.  
    5. QString searchValue = ui->lineEdit_search_clients->text();
    6. QSqlQueryModel *model=new QSqlQueryModel(this);
    7.  
    8. proxyModel->setSourceModel(model);
    9. proxyModel->setFilterRegExp(QRegExp(searchValue, Qt::CaseInsensitive, QRegExp::FixedString));
    10. proxyModel->setFilterKeyColumn(-1);
    11.  
    12. connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
    13. proxyModel, SLOT(setFilterFixedString(QString)));
    14. connect(proxyModel, SIGNAL(hasSelection()),
    15. ui->lineEdit_search_clients, SLOT(setText()));
    16.  
    17. model->setQuery("SELECT id, ClientName,ClientCity, ClientAddress, ClientMol, ClientEik, ClientVat, ClientTel, ClientMail FROM clients");
    18. model->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
    19. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    20. model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
    21. model->setHeaderData(5, Qt::Horizontal, QObject::tr("EIK"));
    22.  
    23.  
    24. ui->tableView_clients->setModel(proxyModel);
    25. //ui->tableView_clients->setSortingEnabled(true);
    26. ui->tableView_clients->setColumnWidth(0,20);//ID
    27. ui->tableView_clients->setColumnWidth(1,173);//ClientName
    28. ui->tableView_clients->setColumnWidth(2,110);//ClientCity
    29. ui->tableView_clients->setColumnWidth(3,0);//ClientAddress
    30. ui->tableView_clients->setColumnWidth(4,0);//ClientMol
    31. ui->tableView_clients->setColumnWidth(5,100);//ClientEik
    32. ui->tableView_clients->setColumnWidth(6,0);//ClientVat
    33. ui->tableView_clients->setColumnWidth(7,0);//ClientTel
    34. ui->tableView_clients->setColumnWidth(8,0);//ClientMail
    35. ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    36. ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
    37. ui->tableView_clients->show();
    To copy to clipboard, switch view to plain text mode 

    Unfortunately after running it the signal selectionChanged() doesn't work anymore.I select different rows but nothing happens.

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and selectionModel problem

    Go through the documentation of QAbstracitItemView::setModel(). It states very clearly that each time you do that, you create a new selection model. qabstractitemview.html#setModel
    So you cannot expect your old connect to work anymore! Create a new connect, read the documentation and follow it.

Similar Threads

  1. QTableView - CSS Problem
    By Sir Rogers in forum Qt Programming
    Replies: 0
    Last Post: 14th February 2011, 20:10
  2. Qtableview problem
    By kira411 in forum Qt Programming
    Replies: 0
    Last Post: 30th July 2010, 11:34
  3. Replies: 3
    Last Post: 12th December 2009, 09:51
  4. SelectionModel
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2007, 14:27
  5. Problem about QTableView.
    By fengtian.we in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2007, 00:13

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.