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. Dragging a tab from QTabWidget into a separate window
QtWS25 Last Chance

Dragging a tab from QTabWidget into a separate window

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 12.5k 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    Hey there,

    My web browser project has a QTabWidget that works fine, but I am interested in extending its abilities. I would like to be able to drag a tab off the tab row and have it turn into its own window. Is this possible with Qt? All the major browsers like Chrome, Firefox, etc. do it.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      Should be possible. Depending on whether you want it to be nicely animated or not, it might even be very simple. Define a minimum drag distance that a tab has to be moved outside of the QTabWidget before it is removed from that tab widget and inserted into another one that comes along with its own window.

      Would need to reimplement maybe mousePressEvent(), dragLeaveEvent() and/or dragMoveEvent(). With this you should be able to make a tab leave. Then either reimplement a dropEvent() for the window that is going to receive the tab, or just make the ripped out tab start its own window and everything.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

        Thanks! How would I set the minimum distance it needs to be pulled outside the QTabWidget? The tabs are currently set to be movable so I can rearrange the order of them. Will it interfere with that feature?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thEClaw
          wrote on last edited by
          #4

          You should be able to just add it on top.

          Subclass QTabWidget, add a new property for your minimum "rip off distance" (int) and one for the "initial click position" (QPoint), then reimplement the mousePressEvent so that you record the position of the click (that's all for that function), reimplement mouseReleaseEvent and reset the "initial click position" in there.

          Finally reimplement dragMoveEvent().There you measure the distance between the "initial click position" and the current position. You probably want to restrict that measurement to the y-coordinate of the two QPoints you now have (initial and current), so that the user can only "rip off" a tab when moving it perpendicular to the orientation of the QTabWidgets tab bar. That way it won't interfere with the normal tab movement.
          So in the dragMoveEvent() you have something like:
          @
          //QPoint initialMouseClick is what you recorded in mousePressEvent
          //int ripOffDistance; could be set to maybe 30 pixels?
          if(initialMouseClick.isValid() && qabs((initialMouseClick - event.pos()).y()) >= ripOffDistance)
          {
          //take the tab, put it in a new window
          }
          QTabWidget::dragMoveEvent(event);@

          There are some finer details I didn't mention (e.g. when you reset initialMouseClick, make it invalid via initialMouseClick = QPoint(), don't set it to zero), but I hope this is enough for you to get the job done. I sadly don't have the time right now to be absolutely precise.

          PS: Maybe take a look at the "drag&drop examples":http://qt-project.org/doc/qt-5/examples-draganddrop.html .

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #5

            Yeah thats a great start, thanks for your help! How do I tell it to only rip off the tab that the cursor is on rather than the whole QTabBar?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thEClaw
              wrote on last edited by
              #6

              How about a look into the "documentation":http://qt-project.org/doc/qt-5/qtabwidget.html ?

              @ QWidget *rippedOffTab = this->currentWidget();
              this->removeTab(this->currentIndex());
              //do with rippedOffTab whatever you want@

              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