[SOLVED] Using two different QTableWidgets in the same window. Focus and selection.



  • Good morning everybody;

    I have two different QTableWidgets in the same window, but sharing the data (although this doesn't matter). It's like having two ways of displaying the data.

    What I pursue is that, when I click to select one item in one table, the other one shows focused and selected the corresponding item. I success to do it 'one way', but not in both ways. So these are my questions:

    1. Is there anything to check if a QTableWidget is focused, something like ui->tableWidget->isFocused()?
      (I've try isActive(), isItemSelected(selectedItem),... without success). I want this because I'm using signals/slots to trigger this everytime the item selection changes in any of the tables:

    @
    void Class::posChanged
    {
    if ( tableA -> isFocused ( ) == true )
    tableB -> setCurrentCell ( );

     if ( tableB -> isFocused ( ) == true )
            tableA -> setCurrentCell ( );
    

    }
    @

    1. Is there any way to keep the default blue selected background when the table isn't focused? I mean, if I focus on one of the tables, the focused table shows the selection with the normal blue background, but the not focused table turns the blue background into grey. I want to have both with the blue background (and avoid using setStyleSheet)

    2. In one of the tables, because of the number of rows, I have scroll bars. When I select an item in the other, in this table the corresponding selection is always at the bottom of the view, it doesn't show in the middle of the view area. Can the selected row of a table be forced to be in the middle of the view area?

    Thank you in advance.

    Edit: Maybe the only solution to get 1) working is split into two different slots for the differents tables...?


  • Moderators

    1. The method is called hasFocus(). Also, inside the slot, you can check which widget sent the signal by calling sender() mehod.

    2. I know you said no styleSheet but it's actually the easiest way and can be made generic(ie. no hardcoded blue): @tableWidget->setStyleSheet("QTableView { selection-background-color: palette(Highlight); }");@
      Another way is by modifying the palette itself: @auto palette = tableWidget->palette();
      auto brush = palette.brush(QPalette::Active, QPalette::Highlight);
      palette.setBrush(QPalette::Inactive, QPalette::Highlight, brush);
      tableWidget->setPalette(palette);@

    3. This should work: @auto idx = tableWidget->model()->index(row, column);
      tableWidget->scrollTo(idx, QAbstractItemView::PositionAtCenter);
      @



  • Thanks once again Chris.

    I didn't know sender method, it's what I was looking for when connecting several signals to the same slot...

    Best regards.



  • Hi again,

    I want to modify another thing from the QtableWidget, and i can't find how:

    I want to have the QtableWidget with the vertical header in descencending order, so the bottom row is 1. But not only change the labels, I really want to change the indexes of the vertical header (and the rows) to have something like a XY representation.

    This way, if I read XY coordinates from a file, say CSV, the table is representing the position in the XY graph...

    Thanks ;)


  • Moderators

    bq. (...) so the bottom row is 1.

    In c+++ the more natural way is to start indexing from 0.

    So basically you want to invert the row index. That's easy. Make a method like this:
    @
    int invertedRow(int row) { return tableWidget->rowCount() - row -1; }
    @
    And use it whenever you need to pass a row index somewhere.

    Another, more encapsulated, way would be to inherit QTableWidget and reimplement all the methods that take row index as a parameter and call the base implementation with this inverted index. That's more work but a cleaner use afterwards. Your call.



  • Thanks for your help.

    Everything is working.

    Even not being the cleanest way, the invertedRow method is perfect, because I'm making the table only once in a for loop, so it's not that dirty...

    Best.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.