April 4, 2012

pmjz pmjz
Lab Rat
89 posts

highlight entire row

 

Hi,guys

I create a QTableView and display a table with lots of rows.I wanna highlight the entire selected row after I clicked one cell.but when I click on other cells in different rows. the view highlights several rows not just only one row,any suggestion.thanks a lot

  1. ui->ccTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  2. ui->ccTableView->setSelectionMode(QAbstractItemView::SingleSelection);

connect clicked(const QModelIndex &) with onClicked()

  1. void CorrelationPresenter::onClicked(const QModelIndex &index)
  2. {
  3.     qDebug() <<"index.row()"<< index.row() <<" index.column()"<< index.column();
  4.     corDialog()->getCCTableView()->selectRow(index.row());
  5. }

11 replies

April 4, 2012

Renatosantos Renatosantos
Lab Rat
45 posts

you can use the SetFlags ();

Look this: http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setFlag

 Signature 

Qt Developer.

“Imagination is more important than knowledge.”
http://twitter.com/Renatos20

April 4, 2012

Andre Andre
Area 51 Engineer
6031 posts

Use the selectionBehavior property (defined in QAbstractItemView, the base class of QTableView).

I’m sorry, you already tried that.

Edit:
If all else fails, you might want to subclass QItemSelectionModel, and reimplement the select slots. You can then instantiate an instance of this selection model, and set it on your view as the selection model to use for your view.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

please give me more details about your solutions,I can not clearly understand that

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

I tried the following one,it does not work:

  1. cstDialog()->getLogsTableView()->selectionModel()->select(index.row(),QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

Renatosantos wrote:
you can use the SetFlags ();

Look this: http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setFlag

I did,it does not work

April 4, 2012

Renatosantos Renatosantos
Lab Rat
45 posts

Sorry, now I see that it is tableView. really is not right.

  • my english is not good, but I’m trying to help. :)
 Signature 

Qt Developer.

“Imagination is more important than knowledge.”
http://twitter.com/Renatos20

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

Renatosantos wrote:
Sorry, now I see that it is tableView. really is not right.

  • my english is not good, but I’m trying to help. :)

thanks though

April 4, 2012

Renatosantos Renatosantos
Lab Rat
45 posts

why you don’t use QTableWidget or QTreeWidget?

 Signature 

Qt Developer.

“Imagination is more important than knowledge.”
http://twitter.com/Renatos20

April 4, 2012

gmaro gmaro
Lab Rat
50 posts

Hi,

Check the working example:

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui>
  5.  
  6. class MainWindow : public QMainWindow
  7. {
  8.     Q_OBJECT
  9.    
  10. public:
  11.     explicit MainWindow(QWidget *parent = 0);
  12.     ~MainWindow();
  13.    
  14. private:
  15.     QTableView *tableView;
  16.  
  17. private slots:
  18.     void onClicked( QModelIndex index );
  19. };
  20.  
  21. #endif // MAINWINDOW_H

  1. #include "mainwindow.h"
  2. #include <QDebug>
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent)
  6. {
  7.     QWidget *c = new QWidget();
  8.     c->setLayout( new QHBoxLayout() );
  9.     setCentralWidget( c );
  10.  
  11.     tableView = new QTableView();
  12.     tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  13.     tableView->setSelectionMode(QAbstractItemView::SingleSelection);
  14.     c->layout()->addWidget( tableView );
  15.  
  16.     int rows = 10;
  17.     int columns = 5;
  18.     QStandardItemModel *model = new QStandardItemModel( rows, columns );
  19.     for( int r = 0; r < rows; ++r ) {
  20.         for( int c = 0; c < columns; ++c ) {
  21.             QStandardItem *sti = new QStandardItem( QString("%1,%2").arg(r).arg(c) );
  22.             model->setItem( r, c, sti );
  23.         }
  24.     }
  25.  
  26.     tableView->setModel( model );
  27.     connect( tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(onClicked(QModelIndex)) );
  28. }
  29.  
  30. MainWindow::~MainWindow()
  31. {
  32. }
  33.  
  34. void MainWindow::onClicked( QModelIndex index )
  35. {
  36.     qDebug() << index;
  37.     //tableView->selectRow( index.row() ); //what for?
  38. }

Not sure what’s going on in your code and which version you’re using but this works perfectly fine on 4.8.0.
Calling

  1. tableView->selectRow( index.row() );

is not necessary – Qt does that for you.

I’m also guesing that instead of:

  1. ui->setupUi(this);
  2. ...
  3. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  4. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);

You have:
  1. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  2. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
  3. ...
  4. ui->setupUi(this);

so the settings in form are used instead of yours.

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

you are right, your example tells me nothing wrong with Qt, but dawn on me something is not right in my code. and I check through my system and find that in one of class inheired from QTableView,I reimplement selectionChange(). so, that problem is fixed. the code is the following:

  1. void selectionChanged(const QItemSelection &selected,
  2.                           const QItemSelection &deselected)
  3.     {
  4.         emit tableSelectionChanged();
  5.         *QTableView::selectionChanged(selected,deselected);*__
  6.     }

April 4, 2012

pmjz pmjz
Lab Rat
89 posts

just add one line:

  1. QTableView::selectionChanged(selected,deselected);

thank you guys and appreciate all of ideas and discussion

 
  ‹‹ Best practice for passing pointers as sender for async signals      ’qt_metatype_id’ is not a member of... ››

You must log in to post a reply. Not a member yet? Register here!