How do I detect which column was clicked in a QTableview header



  • I need to be able to allow a user to select columns for a table they are presented with. They click on the column header to do this.
    I have a public slot defined in class HTCTableViewer as:
    void on_myTable_sectionClicked( int index);
    and connect up the tableViews header section click signal as:
    QObject::connect( ui->tblFileData->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_myTable_sectionClicked( int index) ) );
    I get No such slot HTCTableViewer::on_myTable_sectionClicked( int index)
    And I do have the slot implemented as
    void HTCTableViewer::on_myTable_sectionClicked(int index)
    {
    qDebug() << "Header clicked column - " << index;
    }
    What am I doing wrong?


  • Qt Champions 2017

    Hi
    for the connect, do not name the parameter, only type

    QObject::connect( ui->tblFileData->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_myTable_sectionClicked( int index) ) );

    index should be removed.



  • Hi,
    maybe, subclassing QTableView can achieve what you want.



  • I am only using int in the signal now.
    The connection:
    QObject::connect( ui->tblFileData->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_myTable_sectionClicked( int index) ) );

    This didn't work.
    application output:
    QMetaObject::connectSlotsByName: No matching signal for on_myTable_sectionClicked(int)
    QObject::connect: No such slot HTCTableViewer::on_myTable_sectionClicked( int index) in ../ChartTest/htctableviewer.cpp:23

    Then I removed "index" from the SLOT connection:
    QObject::connect( ui->tblFileData->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_myTable_sectionClicked( int ) ) );

    And it now works but I had to use a non-matching slot signature:
    void on_myTable_sectionClicked( int Value);

    I thought the signatures for the SIGNAL & SLOT connection expressions had to match their definitions.


  • Lifetime Qt Champion

    Hi,

    In what way they don't match ?

    From you code, both have one int parameter.



  • @mmikeinsantarosa said in How do I detect which column was clicked in a QTableview header:

    on_myTable_sectionClicked( int Value);
    the SLOT expression is missing the variable and only has the type. If the actual slot I've defined and implemented to handle the signal looks like
    on_myTable_sectionClicked( int Value);
    I'd expect the text in the connect expression to match and it doesn't.
    on_myTable_sectionClicked( int ) // no variable called Value.



  • Well dang. Now that I actually looked around a bit, I can't find any connect expression that actually includes the type and a variable. I guess I had it in my head they all did and this one was an exception.

    Thanks for the help.


  • Qt Champions 2017

    @mmikeinsantarosa
    Hi
    just to be clear
    You do not name the parameter when using a function in a connect.
    Nor for signal OR the slot.

    So if you function is like
    void MySlot( int ID, QString Name) ( in .h and cpp)

    you would only use as
    MySlot( int, QString) for connect.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.