How to get current row of QTableWidget if I clicked on its child?

njporwal

I have created a QTableWidget in which I've used setCellWidget(QWidget*). I've set QLineEdit in the cell widget. I've also created a delete button and clicking that button sends a signal to the function deleteRow. I've also used a function currentRow() to get the current row, but it returns -1 because of the QLineEdit. The code snippet is below.

void createTable() {
    m_table = new QTableWidget(QDialog); //member variable
    for (int i = 0; i < 3; i++)
    {
        QLineEdit *lineEdit = new QLineEdit(m_table);
        m_table->setCellWidget(i, 0, lineEdit);
    }
    QPushButton *deleteBut = new QPushButton(QDiaolg);
    connect(deleteBut, SIGNAL(clicked()), QDialog, SLOT(editRow()));
}

editRow() {
    int row = m_table->currentRow(); // This gives -1
    m_table->remove(row);
}

In above scenario I click in the QLineEdit and then click on the button delete. Please help me out with a solution.

Karsten Koop

Just tried it here, it seems that currentRow of the table returns -1 when clicking the button right after program start, and when first selecting a cell, then selecting the QLineEdit and then clicking the button, the correct row is returned.

I would do the following as a workaround: Save the row number in the QLineEdit, e.g. by using QObject::setProperty:

 QLineEdit *lineEdit = new QLineEdit(m_table);
 lineEdit->setProperty("row", i);
 m_table->setCellWidget(i, 0, lineEdit);

Then, in the editRow handler, retrieve the property by asking the QTableWidget for its focused child:

int row = m_table->currentRow();
if (row == -1) {
  if (QWidget* focused = m_table->focusWidget()) {
    row = focused->property("row").toInt();
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get current row of QTableWidget if I clicked on its child?

From Dev

How can I get the selected row's value of a qtablewidget in PyQt?

From Dev

javafx tableview how to get the row I clicked?

From Dev

How can I force a css flex child to be the only on its row?

From Dev

How to get the row under the cursor for a QTableWidget

From Dev

How can I get access to ListViewItem from its child control?

From Dev

How do I get value of select option for current row

From Dev

How to get th when child td is clicked?

From Dev

The keyword this is not giving me the current object, only its parent. How do I get at the current object?

From Dev

The keyword this is not giving me the current object, only its parent. How do I get at the current object?

From Dev

How to toggle class to child only of current clicked class

From Dev

How to detect the row and column number of cellwidget user clicked on QTableWidget in PyQt4?

From Dev

How do I get a button to repeat a sound each time its clicked?

From Dev

How to get clicked row number in table in Vaadin?

From Dev

If I have a mime type how do I get its associated icon from the current appearance icons theme?

From Dev

How can I get divs inside a div to continue on a new row when the current row (screen horizontally) is full?

From Dev

How to get current category and its subcategories in magento?

From Dev

How to get the current tool SitePage and/or its Properties?

From Dev

How to get current category and its subcategories in magento?

From Dev

How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

From Dev

How do I get text from an item in a qtablewidget?

From Dev

How do I get text from an item in a qtablewidget?

From Dev

how can I get indexes of multiple selected rows in QTableWidget

From Dev

How Can I Get Current Method's Name And Its Arguments(name and value) in C#

From Dev

How to get value of row if checkbox in that row is clicked in jquery

From Dev

jQuery Datatables - How to get the index of a row when a button in that row is clicked

From Dev

How to get value of row if checkbox in that row is clicked in jquery

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

Related Related

  1. 1

    How to get current row of QTableWidget if I clicked on its child?

  2. 2

    How can I get the selected row's value of a qtablewidget in PyQt?

  3. 3

    javafx tableview how to get the row I clicked?

  4. 4

    How can I force a css flex child to be the only on its row?

  5. 5

    How to get the row under the cursor for a QTableWidget

  6. 6

    How can I get access to ListViewItem from its child control?

  7. 7

    How do I get value of select option for current row

  8. 8

    How to get th when child td is clicked?

  9. 9

    The keyword this is not giving me the current object, only its parent. How do I get at the current object?

  10. 10

    The keyword this is not giving me the current object, only its parent. How do I get at the current object?

  11. 11

    How to toggle class to child only of current clicked class

  12. 12

    How to detect the row and column number of cellwidget user clicked on QTableWidget in PyQt4?

  13. 13

    How do I get a button to repeat a sound each time its clicked?

  14. 14

    How to get clicked row number in table in Vaadin?

  15. 15

    If I have a mime type how do I get its associated icon from the current appearance icons theme?

  16. 16

    How can I get divs inside a div to continue on a new row when the current row (screen horizontally) is full?

  17. 17

    How to get current category and its subcategories in magento?

  18. 18

    How to get the current tool SitePage and/or its Properties?

  19. 19

    How to get current category and its subcategories in magento?

  20. 20

    How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

  21. 21

    How do I get text from an item in a qtablewidget?

  22. 22

    How do I get text from an item in a qtablewidget?

  23. 23

    how can I get indexes of multiple selected rows in QTableWidget

  24. 24

    How Can I Get Current Method's Name And Its Arguments(name and value) in C#

  25. 25

    How to get value of row if checkbox in that row is clicked in jquery

  26. 26

    jQuery Datatables - How to get the index of a row when a button in that row is clicked

  27. 27

    How to get value of row if checkbox in that row is clicked in jquery

  28. 28

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  29. 29

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

HotTag

Archive