def non_empty_rows_count(layout):
    """
    Determine how many rows of the QGridLayout are not empty
    """
    count = 0
    for row in range(layout.rowCount()):
        for col in range(layout.columnCount()):
            item = layout.itemAtPosition(row, col)
            if item is not None and item.widget() is not None and item.widget().isVisible():
                count += 1
                break
    return count