2
votes

I have a QTableWidget containing the output of various math functions. The user needs to be able to click the number next to a row (the vertical header) and I need to get the index of the row that was clicked. I've been searching for a while but I can't find exactly what I need. I've read something about using the verticalHeader() sectionDoubleClicked() signal but I can't get this working. Normally I'd just right click the widget in the designer and go to the slot I wanted but it isn't there.

I've tried

QObject::connect(ui->table->verticalHeader(),SIGNAL(sectionDoubleClicked(int)),this,SLOT(termSelect(int)));

where termSelect is

void termSelect(int index)
{
    cout << "row selected: " << index;
}

but I get a no such slot error.

I tried doing this in the header file

public Q_SLOTS:
    void cTermSelect(int);

but then I get a linking error.

I feel like I'm making a horribly butchered mess of this and that there has to be an easier way.

1
Did you forget to add Q_OBJECT macro in your class definition?hank

1 Answers

1
votes

Note that the sectionDoubleClicked signal is triggered on double click, while your question is about single click. You can use sectionClicked signal as well if you like.

Your slot declaration is cTermSelect but you refer to it in the code you use termSelect. Probably this is the cause of the issue. The rest of the code is fine.

When user clicks a vertical header, the clicked row is selected and becomes current by default. I hope that you don't want to do something surprisingly different on header click because it would ruin user experience. So tracking table selection should be enough. You can use QTableWidget's itemSelectionChanged or currentCellChanged signal to detect it.