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 To Add Select All Checkbox in QTableWidget QHeaderView
QtWS25 Last Chance

How To Add Select All Checkbox in QTableWidget QHeaderView

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqheaderviewcheckboxselect all
3 Posts 2 Posters 12.7k 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.
  • M Offline
    M Offline
    maximo
    wrote on last edited by maximo
    #1

    Is there a way to add a "Select All Checkbox" to a QTableWidget in the first header column? I mean, I found that I can add checkboxes on rows beneath the header, just not a checkbox in the header itself.

    I tried the following, but it shows no change:

    // item(0,0) is a cell with a checkbox in it (first row after the header, first column)
    QTableWidgetItem *oItem = ui->myTable->item(0,0)->clone();
    oItem->setCheckState(Qt::Unchecked);
    ui->myTable->setHorizontalHeaderItem(0,oItem);
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximo
      wrote on last edited by
      #2

      The answer is in the Qt FAQ:

      https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F

      I found that this also worked using an existing QTableWidget that was drawn on the page, like so:

      MyHeader *myHeader = new MyHeader(Qt::Horizontal, ui->myTable);
      ui->myTable->setHorizontalHeader(myHeader);
      

      And even though it does setHorizontalHeader and looks like it might replace your existing header, it appears to only affect the first header column.

      mrjjM 1 Reply Last reply
      0
      • M maximo

        The answer is in the Qt FAQ:

        https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F

        I found that this also worked using an existing QTableWidget that was drawn on the page, like so:

        MyHeader *myHeader = new MyHeader(Qt::Horizontal, ui->myTable);
        ui->myTable->setHorizontalHeader(myHeader);
        

        And even though it does setHorizontalHeader and looks like it might replace your existing header, it appears to only affect the first header column.

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

        @maximo
        Hi
        It creates a new Header that draws a check box image on itself since its not possible to insert a real widget.
        It then response to mouse press to make the image work as a real check box.

        it checks with
        if (logicalIndex == 0)

        so I think it only paints this in first column.

        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