Qt Qtableview 没有收到标题项点击信号

发布于 2024-09-28 02:14:51 字数 1093 浏览 0 评论 0原文

我使用 QtableviewQsqlTableModel 来填充表数据。我想根据列标题上的用户选择对列进行排序。

我尝试了 QTableView 排序信号? 中提到的方式来获取信号(从 QtableView 并连接信号 sectionclicked(int逻辑索引) 但当我单击列标题时,没有发出相同的信号,

请找到完成连接的代码:

。成员变量:

QHeaderView *m_horiz_header;

.cpp 文件

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 ) ));

用于排序的槽函数:

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

单击列标题时不会调用此函数,

您能帮我解决一下吗?这,我哪里错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-10-05 02:14:51

我知道信号连接失败的原因了。

连接时不应提及参数名称。

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

通过像这样修改上面的代码,它起作用了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文