难以理解和处理 QHeaderView 信号

发布于 2024-11-14 14:40:20 字数 798 浏览 3 评论 0原文

我目前正在开发一个用于各种应用程序的基本 QTGUI 表。我想要的一个功能是能够双击列标题并调用排序函数。我一直在网上浏览文档和各种帖子,但还没有完全掌握总体思路。我知道我需要将 doubleClicked 信号连接到我的 handleDoubleClick 插槽,但它并没有真正工作。

许多示例都提到创建您自己的自定义类以及如何为该类设置信号,但是当 QHeaderView 已经内置信号时我需要这样做吗?有没有办法将内置信号连接到我的插槽?

这基本上就是我所拥有的。

QHeaderView *headerView = mainTable->horizontalHeader();
headerView->setMovable(true);
headerView->setClickable(true);

QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), headerView, SLOT(sortByHeader()));

void sortByHeader()
{
    cout << "Double clicked";
}

已解决: Abhijith 的方法有效,但事实证明我还需要传递参数类型。所以这是一个语法错误。

所以是这样的

Class1* myclass = new Class1();
QObject::connect(headerView, SIGNAL(sectionDoubleClicked(int)), myclass, SLOT(sortByHeader(int)));

I'm currently working on a basic QTGui table to be used for various applications. One feature I want is to be able to double click on a header of a column and call a sort function. I've been looking through the documentation and various posts online but I haven't quite grasped the overall idea. I understand that I need to connect a doubleClicked signal to my handleDoubleClick slot, but it's not really working.

Many of the example refer to creating your own custom class and how to set up signals for that class, but do I need to do that when QHeaderView already has signals built in? Is there no way to connect the built in signals to my slot?

Here is basically what I have.

QHeaderView *headerView = mainTable->horizontalHeader();
headerView->setMovable(true);
headerView->setClickable(true);

QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), headerView, SLOT(sortByHeader()));

void sortByHeader()
{
    cout << "Double clicked";
}

Solved:
Abhijith's method worked, but it turns out I also needed to pass the parameter type. So it was a syntax error.

So it's something like this

Class1* myclass = new Class1();
QObject::connect(headerView, SIGNAL(sectionDoubleClicked(int)), myclass, SLOT(sortByHeader(int)));

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

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

发布评论

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

评论(1

为你鎻心 2024-11-21 14:40:20
QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), headerView, SLOT(sortByHeader()));

您要求 connect 方法将 doubleclickedsignal 连接到属于 QHeaderview 类的名为 sortByHeader 的插槽。因此它不会调用您的插槽。如果您在自己的名为 Class1 的类中定义了 sortByHeader() 槽,那么您应该这样做,

Class1* myclass = new Class1();
QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), myclass, SLOT(sortByHeader()));
QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), headerView, SLOT(sortByHeader()));

You are asking the connect method to connect the doubleclickedsignal to a slot called sortByHeader belonging to QHeaderview class. hence it isn't calling your slot. If you have defined the sortByHeader() slot in your own class named Class1 , then you should do this,

Class1* myclass = new Class1();
QObject::connect(headerView, SIGNAL(sectionDoubleClicked()), myclass, SLOT(sortByHeader()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文