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
connect clicked(const QModelIndex &) with onClicked()
- {
- qDebug() <<"index.row()"<< index.row() <<" index.column()"<< index.column();
- corDialog()->getCCTableView()->selectRow(index.row());
- }
11 replies
you can use the SetFlags ();
Look this: http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setFlag
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.
I tried the following one,it does not work:
- cstDialog()->getLogsTableView()->selectionModel()->select(index.row(),QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
you can use the SetFlags ();Look this: http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setFlag
I did,it does not work
Hi,
Check the working example:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QtGui>
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- private slots:
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- #include <QDebug>
- {
- setCentralWidget( c );
- c->layout()->addWidget( tableView );
- int rows = 10;
- int columns = 5;
- for( int r = 0; r < rows; ++r ) {
- for( int c = 0; c < columns; ++c ) {
- model->setItem( r, c, sti );
- }
- }
- tableView->setModel( model );
- }
- MainWindow::~MainWindow()
- {
- }
- {
- qDebug() << index;
- //tableView->selectRow( index.row() ); //what for?
- }
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
- tableView->selectRow( index.row() );
is not necessary – Qt does that for you.
I’m also guesing that instead of:
- ui->setupUi(this);
- ...
You have:
- ...
- ui->setupUi(this);
so the settings in form are used instead of yours.
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:
- {
- emit tableSelectionChanged();
- }
You must log in to post a reply. Not a member yet? Register here!




