Results 1 to 10 of 10

Thread: QTabWidget - Catching move events

  1. #1
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QTabWidget - Catching move events

    Hey all,

    Would it be possible to catch the event of a tab being moved in a QTabWidget?

    Lets say, in a horrible example, we have 5 tabs. I dynamically change the tab text to include "1 - ", "2 - " etc for all the tabs in their current order. If the user moves a tab, would it be possible to catch that, and rename all the tabs so the number order is still correct?

    If I try this using the 'currentChanged' signal, I believe the index is being changed and thus causes a nasty crash.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    Hmmm I think that currentChanged() is not be emitted when you move the tab, only when you click on a new one after the tab has been moved.

  3. The following user says thank you to Spitfire for this useful post:

    RobC (9th March 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    A very good point. I hadn't tried that when experimenting.

    So, ideally a signal on the move - but there isn't one?

  5. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    I don't think there is.

    You could try subclassing the QTabWidget and reimplement void QTabWidget::tabInserted ( int index ) [virtual protected], maybe you can emit the signal from there.

  6. The following user says thank you to Spitfire for this useful post:

    RobC (9th March 2012)

  7. #5
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    No such luck. It emits a signal when the form is first set, but nothing after that point. I guess it doesn't get used when the tabs are moved. I've tried several ways, to no avail.

    Time for Plan B. Well, a cup of tea and then Plan B.

  8. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTabWidget - Catching move events

    Quote Originally Posted by RobC View Post
    So, ideally a signal on the move - but there isn't one?
    See QTabBar::tabMoved(int, int). QTabWidget::tabBar() is protected so you will still need to subclass QTabWidget.

  9. The following user says thank you to norobro for this useful post:

    RobC (9th March 2012)

  10. #7
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    Forgive the question, but how do I reimplement QTabBar if I subclass QTabWidget?

  11. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTabWidget - Catching move events

    You don't need to reimplement QTabBar. You just need to be able to call the protected function tabBar() which you can do in a subclass of QTabWidget.

    Using your example:
    Qt Code:
    1. #include <QtGui>
    2. class TabWidget : public QTabWidget
    3. {
    4. Q_OBJECT
    5. public:
    6. TabWidget() {
    7. setMinimumSize(300,100);
    8. setMovable(true);
    9. for(int i=0;i<5;++i)
    10. addTab(new QWidget,QString("Tab %1").arg(i+1));
    11. connect(tabBar(),SIGNAL(tabMoved(int,int)),this,SLOT(relabelTabs(int,int)));
    12. }
    13. public slots:
    14. void relabelTabs(int /* from */, int /* to */){
    15. for(int i=0;i<count();++i)
    16. setTabText(i,QString("Tab %1").arg(i+1));
    17. }
    18. };
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QApplication app(argc, argv);
    23. TabWidget tw;
    24. tw.show();
    25. app.exec();
    26. }
    27. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    HTH
    Last edited by norobro; 9th March 2012 at 22:33. Reason: updated contents

  12. The following user says thank you to norobro for this useful post:

    RobC (9th March 2012)

  13. #9
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget - Catching move events

    Ah, I getcha. Sorted. Thanks for the help folks!

  14. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTabWidget - Catching move events

    You're welcome.

    Rather than iterating over all of the tabs, here's a better version of the slot:
    Qt Code:
    1. void relabelTabs(int from, int to){
    2. QString hold= tabText(from);
    3. setTabText(from,tabText(to));
    4. setTabText(to,hold);
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [Qt Widget + libvlc] catching mouse events
    By mentalmushroom in forum Qt Programming
    Replies: 9
    Last Post: 6th March 2012, 13:39
  2. Catching Mouse Events on Top-Level Window
    By andyp in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 10:26
  3. Catching events for tabBar of tabbed QDockWidget
    By koosh in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2009, 23:17
  4. Problem with catching keyPress events ?
    By arbi in forum Qt Programming
    Replies: 12
    Last Post: 1st September 2008, 12:35
  5. Catching X Events
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2006, 12:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.