Results 1 to 8 of 8

Thread: Changing names of QTabWidget tabs

  1. #1
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Changing names of QTabWidget tabs

    Hi all,

    any hint, how to make it possible to edit the names on the tabs of a QTabWidget?
    I have an application where the tabs have default names but the user should be able to give them his own desired names.
    My idea was to react on right mouse click on the tabs and open an editor right where the QTabBar is or alternatively open a small window where the new name can be filled in.

    Any direction where I can look at?
    Context menu at QTabWidget? Or QTabBar?

    Any hint appreciated,

    Rainer

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Changing names of QTabWidget tabs

    Subclass QTabBar, catch necessary event(s), provide a temporary QLineEdit at QTabBar::tabRect() filled with QTabBar::tabText(). Once QLineEdit::editingFinished() is emitted, use QTabBar::setTabText() and get rid of the temporary QLineEdit.
    J-P Nurmi

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

    RThaden (28th August 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Changing names of QTabWidget tabs

    Thanks jpn,

    that was exactly what I was looking for. Shouldn't be a big problem for me now.

    Kind regards,

    Rainer

  5. #4
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    23
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Changing names of QTabWidget tabs

    Got same situation and that's what I exactly did. Also, in mousePressEvent I'm hiding QLineEdit if it's visible in order to avoid situation when tabRect() of the tab being currently edited changes (could also update QLineEdit's geometry, but prefer to do it in that way). One unsolved problem still exists - when tool buttons responsible for rewinding tabs are visible - clicking them WON'T change QLineEdit position nor hide it - and I can't find a way to do that, because those buttons are part of QTabBarPrivate, which's inaccessible for me (can't catch any event, signal or whatever). (Put briefly: when rewinding tabs, QLineEdit remains visible, in wrong position and covers other tabs; It should disappear.)
    Any ideas how to avoid it?
    Last edited by Fenix Voltres; 20th February 2010 at 01:46.

  6. #5
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    23
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Changing names of QTabWidget tabs

    BUMP
    Anyone can help?

  7. #6
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    23
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Changing names of QTabWidget tabs

    If anyone would be interested in that problem: only method I found working is to remember i. e. QTabBar::tabRect(0).left(), and check in reimplemented paintEvent() if it changes - if it does and QLineEdit is visible - hide it.

  8. #7

    Default Re: Changing names of QTabWidget tabs

    Just a guess:
    if another button on the widget is pressed, or the widget is resized, the QLineEdit looses focus. So you could just override QLineEdit::focusOutEvent() and close/hide the (now custom) line edit.
    I did my own implementation of this poblem, but the QLineEdit is just floating above the tab. also it closes itself if its done and if the user presses esc, the entered text is discarded. an event "publishText" is emitted, if the user successfully entered text. Its not perfect, but it works. If you have any suggestions, go ahead.
    Qt Code:
    1. class IndependentLineEdit: public QLineEdit {
    2. Q_OBJECT
    3. public:
    4. IndependentLineEdit(QWidget* parent = 0);
    5. IndependentLineEdit(const QString & contents, QWidget* parent = 0 );
    6. public slots:
    7. void setText(const QString &text);
    8. signals:
    9. void newTextEntered(const QString &text);
    10. protected:
    11. void keyPressEvent(QKeyEvent* event);
    12. void focusOutEvent(QFocusEvent* event);
    13. private slots:
    14. void publishText();
    15. void setupWidget();
    16. };
    17.  
    18. IndependentLineEdit::IndependentLineEdit(QWidget* parent) :
    19. QLineEdit(parent) {
    20. setupWidget();
    21. }
    22.  
    23. IndependentLineEdit::IndependentLineEdit(const QString & contents, QWidget* parent) :
    24. QLineEdit(contents, parent) {
    25. setupWidget();
    26. }
    27.  
    28. void IndependentLineEdit::setText(const QString &text) {
    29. QLineEdit::setText(text);
    30. setFocus();
    31. selectAll();
    32. show();
    33. }
    34.  
    35. void IndependentLineEdit::keyPressEvent(QKeyEvent* event) {
    36. QLineEdit::keyPressEvent(event);
    37. if (event->key() == Qt::Key_Escape) {
    38. setText("");
    39. hide();
    40. }
    41. }
    42.  
    43. void IndependentLineEdit::focusOutEvent(QFocusEvent* event) {
    44. QLineEdit::focusOutEvent(event);
    45. emit editingFinished();
    46. }
    47.  
    48. void IndependentLineEdit::publishText() {
    49. if (text() != "") {
    50. emit newTextEntered(text());
    51. }
    52. hide();
    53. }
    54.  
    55. void IndependentLineEdit::setupWidget() {
    56. connect(this, SIGNAL(editingFinished()), this, SLOT(publishText()));
    57. setWindowFlags(Qt::CustomizeWindowHint);
    58. }
    To copy to clipboard, switch view to plain text mode 

    I use it with a custom QTabBar and a custom QTabWidget:

    Qt Code:
    1. class EditableTabBar: public QTabBar {
    2. Q_OBJECT
    3. public:
    4. EditableTabBar(QWidget* parent = 0);
    5. virtual ~EditableTabBar();
    6. signals:
    7. void tabTextChanged(int index, const QString &text);
    8. protected:
    9. void mouseDoubleClickEvent(QMouseEvent* event);
    10. private slots:
    11. void setCurrentTabText(const QString &text);
    12. private:
    13. IndependentLineEdit _edit;
    14. };
    15.  
    16. EditableTabBar::EditableTabBar(QWidget* parent) :
    17. QTabBar(parent), _edit("", 0) {
    18. connect(&_edit, SIGNAL(newTabTextEntered(const QString &)), this,
    19. SLOT( setCurrentTabText(const QString &)));
    20. }
    21.  
    22. EditableTabBar::~EditableTabBar() {
    23. // TODO Auto-generated destructor stub
    24. }
    25.  
    26. void EditableTabBar::mouseDoubleClickEvent(QMouseEvent* event) {
    27. _edit.setText(tabText(currentIndex()));
    28. _edit.move(mapToGlobal(event->pos()));
    29. }
    30.  
    31. void EditableTabBar::setCurrentTabText(const QString &text) {
    32. setTabText(currentIndex(), text);
    33. emit tabTextChanged(currentIndex(), text);
    34. }
    To copy to clipboard, switch view to plain text mode 

    the tabTextChanged() signal is needed for my application, but its not needed for everything to work.

    The tabwidget:
    Qt Code:
    1. class EditableTabWidget : public QTabWidget {
    2. Q_OBJECT
    3. public:
    4. EditableTabWidget(QWidget* parent = 0);
    5. signals:
    6. void tabTextChanged(int index,const QString &text);
    7. };
    8.  
    9. EditableTabWidget::EditableTabWidget(QWidget* parent) :
    10. QTabWidget(parent) {
    11. EditableTabBar* t = new EditableTabBar();
    12. connect(t, SIGNAL(tabTextChanged(int, const QString &)), this,
    13. SIGNAL(tabTextChanged(int, const QString &)));
    14. setTabBar(t);
    15. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing names of QTabWidget tabs

    Thanks very much.

    I edited your code as follows to display the _edit inside the tab label area. Also fixed the window flags.

    Qt Code:
    1. EditableTabBar::EditableTabBar(QWidget* parent) :
    2. QTabBar(parent), _edit("", 0) {
    3.  
    4. _edit.setWindowFlags(Qt::Popup);
    5. connect(&_edit, SIGNAL(newTextEntered(const QString &)), this,
    6. SLOT( setCurrentTabText(const QString &)));
    7. }
    8.  
    9. void EditableTabBar::mouseDoubleClickEvent(QMouseEvent* event) {
    10. Q_UNUSED(event)
    11. QRect rect = this->tabRect(currentIndex());
    12. _edit.setFixedSize(rect.size());
    13. _edit.move(mapToGlobal(rect.topLeft()));
    14.  
    15. _edit.setText(tabText(currentIndex()));
    16. //_edit.move(mapToGlobal(event->pos()));
    17. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTabWidget with same tabs
    By Djony in forum Qt Programming
    Replies: 20
    Last Post: 24th December 2011, 13:20
  2. Delayed Rendering of QTabWidget Tabs
    By mclark in forum Qt Tools
    Replies: 13
    Last Post: 14th May 2007, 23:53
  3. Changing QTabWidget's tabs color
    By troorl_ua in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 22:49
  4. Switching off all tabs in QTabWidget
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 30th August 2006, 18:10
  5. changing the size of the tab width: QTabWidget
    By nikita in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2006, 09:31

Tags for this Thread

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.