Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I detect which column was clicked in a QTableview header

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

Scheduled Pinned Locked Moved Solved General and Desktop
qheaderviewqtableviewcolumn clicked
8 Posts 4 Posters 9.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mmikeinsantarosaM Offline
    mmikeinsantarosaM Offline
    mmikeinsantarosa
    wrote on last edited by
    #1

    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?

    S 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      2
      • mmikeinsantarosaM mmikeinsantarosa

        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?

        S Offline
        S Offline
        Sabri
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mmikeinsantarosaM Offline
          mmikeinsantarosaM Offline
          mmikeinsantarosa
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            In what way they don't match ?

            From you code, both have one int parameter.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • mmikeinsantarosaM Offline
              mmikeinsantarosaM Offline
              mmikeinsantarosa
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • mmikeinsantarosaM Offline
                mmikeinsantarosaM Offline
                mmikeinsantarosa
                wrote on last edited by
                #7

                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.

                mrjjM 1 Reply Last reply
                0
                • mmikeinsantarosaM mmikeinsantarosa

                  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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved