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 delete a row of Tablewidget ? If Delete button has been added using code .
Qt 6.11 is out! See what's new in the release blog

How to delete a row of Tablewidget ? If Delete button has been added using code .

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 391 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.
  • P Offline
    P Offline
    Pappu Kumar Keshari
    wrote on last edited by
    #1

    I am fetching data from data base and adding Delete button in every row using below code .
    for ( int k = 0; k <3 ; k++) {
    QPushButton *btn = new QPushButton(ui->TableWidget);
    ui->TableWidget->setCellWidget(k,2,btn);
    btn->setText("Delete");

        }
    

    I want to delete specific row in Tablewidget how can I do it ?
    I have three columns in Table widget like this -> Serial Number role Buttons
    1 user Delete
    2 admin Delete
    3 operator Delete

    JonBJ 1 Reply Last reply
    0
    • P Pappu Kumar Keshari

      I am fetching data from data base and adding Delete button in every row using below code .
      for ( int k = 0; k <3 ; k++) {
      QPushButton *btn = new QPushButton(ui->TableWidget);
      ui->TableWidget->setCellWidget(k,2,btn);
      btn->setText("Delete");

          }
      

      I want to delete specific row in Tablewidget how can I do it ?
      I have three columns in Table widget like this -> Serial Number role Buttons
      1 user Delete
      2 admin Delete
      3 operator Delete

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Pappu-Kumar-Keshari
      You are not really supposed to use setCellWidget() for all these buttons, you're supposed to use QStyledItemDelegates. But @VRonin no longer seems to frequent this forum :(

      You need somehow to identify which row each button is associated with when you get the clicked() signal. 3 ways:

      1. You will have to get the QPushButton signaller and find which row it is on in the table widget.

      2. When you connect() each button you are creating use a lambda for the slot, to which you pass an extra parameter of the row number.

      3. An alternative to #2 is to use QButtonGroup Class for each column of buttons. Use its setId() to associate the row number, which will be passed by the idClicked() signal. This is the one I would do, for neatness.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        micha_eleric
        wrote on last edited by
        #3

        how about hide, instead of delete?

        JonBJ 1 Reply Last reply
        0
        • M micha_eleric

          how about hide, instead of delete?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @micha_eleric
          Hide what, instead of delete? The OP is asking a common question about how to have a button against each row, to delete that row from the database, or similar functionality.

          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