开发者

Trouble understanding and handling QHeaderView Signals

开发者 https://www.devze.com 2023-03-12 00:47 出处:网络
I\'m cur开发者_JAVA技巧rently 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

I'm cur开发者_JAVA技巧rently 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)));


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

精彩评论

暂无评论...
验证码 换一张
取 消