Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Can't set tab order after rearranging widgets
Forum Updated to NodeBB v4.3 + New Features

Can't set tab order after rearranging widgets

Scheduled Pinned Locked Moved Unsolved Qt for Python
13 Posts 4 Posters 674 Views 3 Watching
  • 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.
  • J Offline
    J Offline
    JTHundley
    wrote on last edited by
    #1

    I have a verticalLayout in a scrollArea that I add custom widgets to. When I do this, the tab order is properly set. I then have radioButtons to change the order and sort these widgets. I've tried about 4 different ways to change the tab order to respect this new ordering, but every time it's ignored. I have a feeling I might be doing something more fundamentally wrong somewhere else, like in the way I re-order these custom widgets.

    Here's the section in question, cards is a list of the custom widget:

        def _sort_cards(self, cards: list) -> 
            "Helper method to sort cards into the scrollarea. cards is the sorted list of Card objects to use."
            print("_sort_cards")
            # add all the cards and show the line:
            for card in reversed(cards):
                self.verticalLayout_player_cards.insertWidget(0, card)
                card.show_line()
            # set tab order:
            last_card = None
            for card in 
                if last_card:
                    self.setTabOrder(last_card, card)
                    print("setTabOrder:", last_card, card)
                last_card = card
            self._hide_top_card_line()
    

    The full code is here and I can also share the whole project if it's helpful: https://0x0.st/XDHU.bin

    I'm new to the forums and have been eagerly using and learning qt for a little bit now :) Thanks in advance!

    Pl45m4P 1 Reply Last reply
    0
    • J JTHundley

      I have a verticalLayout in a scrollArea that I add custom widgets to. When I do this, the tab order is properly set. I then have radioButtons to change the order and sort these widgets. I've tried about 4 different ways to change the tab order to respect this new ordering, but every time it's ignored. I have a feeling I might be doing something more fundamentally wrong somewhere else, like in the way I re-order these custom widgets.

      Here's the section in question, cards is a list of the custom widget:

          def _sort_cards(self, cards: list) -> 
              "Helper method to sort cards into the scrollarea. cards is the sorted list of Card objects to use."
              print("_sort_cards")
              # add all the cards and show the line:
              for card in reversed(cards):
                  self.verticalLayout_player_cards.insertWidget(0, card)
                  card.show_line()
              # set tab order:
              last_card = None
              for card in 
                  if last_card:
                      self.setTabOrder(last_card, card)
                      print("setTabOrder:", last_card, card)
                  last_card = card
              self._hide_top_card_line()
      

      The full code is here and I can also share the whole project if it's helpful: https://0x0.st/XDHU.bin

      I'm new to the forums and have been eagerly using and learning qt for a little bit now :) Thanks in advance!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @JTHundley said in Can't set tab order after rearranging widgets:

      I have a verticalLayout in a scrollArea

      You usually don't do this.
      It's counterproductive in a QScrollArea and might break the scroll functionality.
      Use QScrollArea::setWidget to add your large, scrollable widget (with layout) instead.

      Haven't looked at your code, but when doing the above correctly,

      every time it's ignored

      this might work then.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply
      1
      • J Offline
        J Offline
        JTHundley
        wrote on last edited by
        #3

        Thanks for your response, but I'm not sure I understand. The scrolling actually does work fine the way it is, but I removed the verticalLayout from the scrollArea and found that it all works the same. Sorting and scrolling still work, but when rearranging the widgets within the scrollArea the tab order remains the way it was when the widgets were first added.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You need to use setTabOrder after you moved your widgets around.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You need to use setTabOrder after you moved your widgets around.

            J Offline
            J Offline
            JTHundley
            wrote on last edited by
            #5

            @SGaist Like I did in the code snippet? Or did I do it wrong? I've used setTabOrder successfully before which is why I'm so confused.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Sorry, I somehow did not see it.
              However, your loop code seems broken or is it a copy paste error ?

              That said, you could use the Python zip function to create the pairs of widgets. The first list being the original minus the last one and the second list the original with the first item removed.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                Sorry, I somehow did not see it.
                However, your loop code seems broken or is it a copy paste error ?

                That said, you could use the Python zip function to create the pairs of widgets. The first list being the original minus the last one and the second list the original with the first item removed.

                J Offline
                J Offline
                JTHundley
                wrote on last edited by
                #7

                @SGaist Yes, that was a paste error, "for card in cards:" :)

                So basically you're saying I should delete the widgets and recreate them so they can be freshly inserted or added into the scrollArea, right? This is my latest attempt that I'm trying, mixed results so far.

                SGaistS 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @JTHundley said in Can't set tab order after rearranging widgets:

                  I have a verticalLayout in a scrollArea

                  You usually don't do this.
                  It's counterproductive in a QScrollArea and might break the scroll functionality.
                  Use QScrollArea::setWidget to add your large, scrollable widget (with layout) instead.

                  Haven't looked at your code, but when doing the above correctly,

                  every time it's ignored

                  this might work then.

                  S Offline
                  S Offline
                  Sollani
                  wrote on last edited by Sollani
                  #8
                  This post is deleted!
                  J 1 Reply Last reply
                  0
                  • S Sollani

                    This post is deleted!

                    J Offline
                    J Offline
                    JTHundley
                    wrote on last edited by
                    #9

                    @Sollani What? Are you a spam bot?

                    1 Reply Last reply
                    0
                    • J JTHundley

                      @SGaist Yes, that was a paste error, "for card in cards:" :)

                      So basically you're saying I should delete the widgets and recreate them so they can be freshly inserted or added into the scrollArea, right? This is my latest attempt that I'm trying, mixed results so far.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JTHundley said in Can't set tab order after rearranging widgets:

                      @SGaist Yes, that was a paste error, "for card in cards:" :)

                      So basically you're saying I should delete the widgets and recreate them so they can be freshly inserted or added into the scrollArea, right? This is my latest attempt that I'm trying, mixed results so far.

                      Not at all, I was talking about how to build the input of your for loop.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JTHundley
                        wrote on last edited by
                        #11

                        Ah. Well again my code pasted in wrong and the loop does work, the widgets get rearranged into the order I want them in, but then fixing them with setTabOrder does nothing. The way I rearrange the widgets in the scrollArea is by inserting them into the layout at the beginning. This moves them instead of duplicating them. I still have no idea why setTabOrder isn't working and my days of searching haven't turned up much.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JTHundley
                          wrote on last edited by
                          #12

                          I figured it out! My in-progress solution is to add each of these custom widgets to a temporary layout to re-parent them and then add them back to the layout in the order I want them to tab to:

                          def _sort_cards(self, cards: list) -> None:
                                  "Helper method to sort cards into the scrollarea. cards is the sorted list of Card objects to use."
                                  print("_sort_cards")
                          
                                  # create a temporary layout so we can add all cards to this and then back to the regular layout
                                  temp_parent = QtWidgets.QWidget()
                                  temp_layout = QtWidgets.QVBoxLayout(temp_parent)
                                  for card in cards:
                                      temp_layout.insertWidget(0, card)
                          
                                  # add all the cards and show the line:
                                  for index, card in enumerate(cards):
                                      self.verticalLayout_player_cards.insertWidget(index, card)
                                      card.show_line()
                          
                                  self._hide_top_card_line()
                          
                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            JTHundley
                            wrote on last edited by
                            #13

                            I made a full release if anyone wants the full context: https://sourceforge.net/projects/inscryption-hydra-tracker/
                            I hope this thread helps others who find setTabOrder not working for them. It took me a week to figure this out, never give up!

                            Here's the finished sort method:

                                def _sort_cards(self, cards: list) -> None:
                                    "Helper method to sort cards into the scrollarea. cards is the sorted list of Card objects to use."
                                    # create a temporary widget and reparent all the cards to it:
                                    temp_parent = QtWidgets.QWidget()
                                    for card in cards:
                                        card.setParent(temp_parent)
                                    # add all the cards and show the line:
                                    for index, card in enumerate(cards):
                                        # for some reason adding the card to the layout makes it hidden, so capture and restore that state:
                                        hidden = card.isHidden()
                                        self.verticalLayout_player_cards.insertWidget(index, card)
                                        card.setHidden(hidden)
                                        card.show_line()
                                    self._hide_top_card_line()
                            

                            Thanks to everyone who helped and responded!

                            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