Results 1 to 9 of 9

Thread: Rename a Tab of a QTabWidget

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Rename a Tab of a QTabWidget

    Hi I have a question:
    I want to rename a tab of a QTabWidget. If the User click on a tab twice, a InputDialog should open.
    But I don't know how i should implement the double click event.

    I want to use the QTableWidget without subclassing.

    thank u for your help!

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    You MUST subclass QTabWidget because you have to subclass QTabBar and then use QTabBar::setTabBar that is protected.

    MyTabWidget.cpp
    Qt Code:
    1. #include "MyTabWidget.h"
    2. #include "MyTabBar.h"
    3.  
    4. MyTabWidget::MyTabWidget(QWidget *parent) :
    5. QTabWidget(parent)
    6. {
    7. MyTabBar* myTab = new MyTabBar;
    8. setTabBar (myTab);
    9. }
    To copy to clipboard, switch view to plain text mode 

    MyTabBar.cpp
    Qt Code:
    1. #include "MyTabBar.h"
    2.  
    3. #include <QtGui/QInputDialog>
    4. #include <QtGui/QMouseEvent>
    5.  
    6. MyTabBar::MyTabBar(QWidget *parent) :
    7. QTabBar(parent)
    8. {
    9. }
    10.  
    11. void MyTabBar::mouseDoubleClickEvent(QMouseEvent *e)
    12. {
    13. if (e->button () != Qt::LeftButton) {
    14. QTabBar::mouseDoubleClickEvent (e);
    15. return;
    16. }
    17.  
    18. int idx = currentIndex ();
    19. bool ok = true;
    20. QString newName = QInputDialog::getText (
    21. this, tr ("Change Name"),
    22. tr ("Insert New Tab Name"),
    23. QLineEdit::Normal,
    24. tabText (idx),
    25. &ok);
    26.  
    27. if (ok) {
    28. setTabText (idx, newName);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 12th July 2011 at 18:21. Reason: spelling corrections
    A camel can go 14 days without drink,
    I can't!!!

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

    Qiieha (13th July 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    thank u for your reply and your great example.

    do you know a different solution for renaming, without subclassing? i thought about a contextmenu with the action rename, but there is a little problem:

    If i click on the tabwidget the contextmenu pops up everytime. But it should just open if the tab is the currentIndex.
    Last edited by Qiieha; 13th July 2011 at 10:01.

  5. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    Why you don't want use subclassing??

    Your solution needs to know the tabs position on the screen.
    IMHO my solution is simpler.
    A camel can go 14 days without drink,
    I can't!!!

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

    Qiieha (13th July 2011)

  7. #5
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    OK thank u!
    because i like to work with the designer. But your solution is better, so I subclass it.

  8. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    you can subclass AND work with designer (using Widget Promoting)
    A camel can go 14 days without drink,
    I can't!!!

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

    Qiieha (13th July 2011)

  10. #7
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Rename a Tab of a QTabWidget

    oh, thats quite useful! I didn't know this is possible.

    thank u!

    greets

  11. #8
    Join Date
    Aug 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rename a Tab of a QTabWidget

    Why not get a signal from QTabBar?
    Qt Code:
    1. class AlbumTabs : public QTabWidget
    2. {
    3. Q_OBJECT
    4. public:
    5.  
    6. explicit AlbumTabs(QWidget* parent = nullptr);
    7. ~AlbumTabs();
    8.  
    9. public slots:
    10. void onTabBarDoubleClicked(int index);
    11.  
    12.  
    13. };
    14.  
    15. AlbumTabs::AlbumTabs(QWidget* parent) : QTabWidget(parent)
    16. {
    17. connect(tabBar(), &QTabBar::tabBarDoubleClicked, this, &AlbumTabs::onTabBarDoubleClicked);
    18. }
    19.  
    20. void AlbumTabs::onTabBarDoubleClicked(int index)
    21. {
    22. bool ok = true;
    23. QString newName = QInputDialog::getText(this, tr("Change Name"), tr("Insert New Tab Name"), QLineEdit::Normal, tabText(index), &ok);
    24.  
    25. if (ok)
    26. {
    27. setTabText(index, newName);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

  12. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rename a Tab of a QTabWidget

    Thanks, but you realize you are replying to a post that is 10 1/2 years old, right?

    Beside, you do not need to subclass QTabWidget in order to do this. You can connect to the QTabWidget::tabBarDoubleClicked() signal from anywhere, and can call QTabWidget::setTabText() from anywhere as well since it is a public method of QTabWidget.

    You do not need to access the QTabBar widget inside of QTabWidget because the required signal and method are exposed by QTabWidget itself. Even if they weren't, you still don't need to subclass, because QTabWidget::tabBar() is a public method, and the QTabBar signal and setTabText() are also public members of QTabBar.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt Creator Is it possible to rename a project?
    By N3wb in forum Qt Tools
    Replies: 1
    Last Post: 3rd December 2010, 02:14
  2. rename file name
    By weixj2003ld in forum Qt Programming
    Replies: 11
    Last Post: 8th April 2010, 11:27
  3. QFile::rename()
    By Bagstone in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2008, 11:53
  4. How I add rename in QTreeWidgetItem?
    By rajesh in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 13:10

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.