8
votes

I am using Qtableview with QsqlTableModel for populating a table data. I want to sort the column based on user selection on column header.

I tried the way mentioned in QTableView sorting signal? for getting the signal (get the horizontal header from QtableView and connect signal sectionclicked(int logical index). But the same signal is not getting emitted when i click on column header.

Please find the code where connection is done:

Member variable:

QHeaderView *m_horiz_header;

.cpp file

m_sqltablemodel->setTable(tabel_name);
m_sqltablemodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
m_sqltablemodel->select();

m_horiz_header= m_table_view->horizontalHeader();
connect(m_horiz_header, SIGNAL(sectionClicked ( int logicalIndex ) ),
    this, SLOT(on_sectionClicked ( int logicalIndex ) ));

Slot function for sorting:

void class::on_sectionClicked ( int logicalIndex ) 
{
    m_horiz_header->setSortIndicator(logicalIndex, Qt::AscendingOrder);
    m_table_view->sortByColumn(logicalIndex);
}

This function is not getting called, when column header is clicked.

Can you please help me how to do this, where I went wrong?

1

1 Answers

8
votes

I got the reason why the signal connection failed.

argument name should not be mentioned on connect.

connect(m_horiz_header, SIGNAL(sectionClicked(int)), this, SLOT(on_sectionClicked(int)));

by modifiying the above code like this, it worked.