难以理解和处理 QHeaderView 信号
我目前正在开发一个用于各种应用程序的基本 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要求 connect 方法将
doubleclickedsignal
连接到属于 QHeaderview 类的名为sortByHeader
的插槽。因此它不会调用您的插槽。如果您在自己的名为Class1
的类中定义了sortByHeader()
槽,那么您应该这样做,You are asking the connect method to connect the
doubleclickedsignal
to a slot calledsortByHeader
belonging to QHeaderview class. hence it isn't calling your slot. If you have defined thesortByHeader()
slot in your own class namedClass1
, then you should do this,