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

    @
    ui->ccTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->ccTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    @
    connect clicked(const QModelIndex &) with onClicked()

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



  • you can use the SetFlags ();

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



  • -Use the selectionBehavior property (defined in [[doc:QAbstractItemView]], the base class of QTableView).-

    I'm sorry, you already tried that.

    Edit:
    If all else fails, you might want to subclass [[doc: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.



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



  • I tried the following one,it does not work:
    @cstDialog()->getLogsTableView()->selectionModel()->select(index.row(),QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);@



  • [quote author="Renatosantos" date="1333535748"]you can use the SetFlags ();

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

    I did,it does not work



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

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


  • [quote author="Renatosantos" date="1333563704"]Sorry, now I see that it is tableView. really is not right.

    • my english is not good, but I'm trying to help. :)[/quote]

    thanks though



  • why you don't use QTableWidget or QTreeWidget?



  • Hi,

    Check the working example:

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    QTableView *tableView;

    private slots:
    void onClicked( QModelIndex index );
    };

    #endif // MAINWINDOW_H
    @

    @
    #include "mainwindow.h"
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    QWidget *c = new QWidget();
    c->setLayout( new QHBoxLayout() );
    setCentralWidget( c );

    tableView = new QTableView();
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    c->layout()->addWidget( tableView );
    
    int rows = 10;
    int columns = 5;
    QStandardItemModel *model = new QStandardItemModel( rows, columns );
    for( int r = 0; r < rows; ++r ) {
        for( int c = 0; c < columns; ++c ) {
            QStandardItem *sti = new QStandardItem( QString("%1,%2").arg(r).arg(c) );
            model->setItem( r, c, sti );
        }
    }
    
    tableView->setModel( model );
    connect( tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(onClicked(QModelIndex)) );
    

    }

    MainWindow::~MainWindow()
    {
    }

    void MainWindow::onClicked( QModelIndex index )
    {
    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);
    ...
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    @
    You have:
    @
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ...
    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:
    @
    void selectionChanged(const QItemSelection &selected,
    const QItemSelection &deselected)
    {
    emit tableSelectionChanged();
    QTableView::selectionChanged(selected,deselected);__
    }
    @



  • just add one line:
    @
    QTableView::selectionChanged(selected,deselected);
    @

    thank you guys and appreciate all of ideas and discussion


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.