Qt/C++ QTableWidget: Doing something when a header

2019-02-18 15:21发布

问题:

I have a QTableWidget in my form and I want to do something when a user doubleclicks on the header of a row or column. I am using the following connect statement:

connect(ui->tblResults->horizontalHeader(),SIGNAL(doubleClicked(QModelIndex)),this,SLOT(tableDoubleClicked(QModelIndex)));

Where this is the window class and ui->tblResults is the QTableWidget on the window's form. When I try doubleclicking the header, nothing happens. The slot I'm connecting to is:

void wndSearch::tableDoubleClicked(QModelIndex tmp){
    QMessageBox::information(0,"DERP!","TEST");
}

Very simple, just testing to see if the slot gets called. I never receive this messagebox. I am not getting any runtime error on the connect() call.

Am I using the wrong signal? Is something else wrong? Please let me know if you need anymore information, and thanks for the help!

[edit] Someone in #qt on freenode helped me out. SIGNAL I was looking for was sectionDoubleClicked(int)

回答1:

http://doc.qt.nokia.com/latest/qheaderview.html#sectionDoubleClicked

You can get the QHeaderView via QTableWidget::horizontalHeader() or QTableWidget::verticalHeader().