October 5, 2011

M_31 M_31
Lab Rat
98 posts

[SOLVED] Changing QTabWidget’s tab Name at runtime..

 

I want to change the QTabWidget ‘s tab name at runtime..

i searched in the internet and found like

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

Apart from the above proposed idea..is there any other way to achive this task .?? or do we have any build-in function to do the same?…like widget’s editItem..

5 replies

October 5, 2011

Andre Andre
Area 51 Engineer
6076 posts

I assume that you mean that you want to user to be able to rename tabs by double clicking the tab, or something like that?

If so, than I guess the proposed outline is valid. If you just want to set the label from somewhere else, you only need to call setTabText(), of course.

Note that you do not even need to subclass QTabWidget. You can do it by creating a QObject-derived class (lets call it TabRenamerCharm), and use the eventFilter system to listen in on the events send to the QTabBar widget. The QTabBar is the widget you’d need to subclass anyway, as QTabWidget uses that one internally too. This way you can make your renaming work on QTabBar and QTabWidget, as well as any external widget derived from it or embedding it. Downside is of course that you have an additional object to handle, and you need to connect to that instead of the actual tab widget.

The TabRenamerCharm class can encapsulate the process outlined in your post, and provide a signal to notifify anyone interested on the change. Interesting corner case is of course a tab with east or west orientation :-)

Edit:
Just for the fun of it, I tried implementing the aproach above. It works, and it works quite nicely I might add. After a bit more spit & polish, I guess I will publis the code as a snippet on the wiki here.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

October 6, 2011

M_31 M_31
Lab Rat
98 posts

Thanks Andre…Now i am able to show QLineEdit with corresponding Tab Text.

But i triggered this QLineEdit by pressing some edit button on my Dialog window..not by pressing the mouse button on the Selected QTabWidget’s Tab..becoz there is no viewport() funtion for this QTabWidget

  1. //ui->tabWidget->viewport()->installFilter( this );
  2. //! The above function viewport is not avilable for my QTabWidget..

if its available then only ..with the help of eventFilter()…i can get the mouse press event to get the Edit tab operation.

Please let me know…whether i can have Viewport for my QTabWidget ..in some other way???

October 6, 2011

Andre Andre
Area 51 Engineer
6076 posts

EventFilters work on any QObject, not just on viewPort() (which will return a pointer to a QWidget, which is a QObject). I guess the example you were looking just used that one.

I will show you my eventFilter implementation.

  1. bool TabRenamerCharm::eventFilter(QObject * o, QEvent * e)
  2. {
  3.     bool result = QObject::eventFilter(o, e);
  4.  
  5.     if (o == m_tabBar) {
  6.         if (e->type() == QEvent::MouseButtonPress) {
  7.             //see if the user is trying to move away from the editing by clicking another tab
  8.             QMouseEvent* me = static_cast<QMouseEvent*>(e);
  9.             int clickedTabId = m_tabBar->tabAt(me->pos());
  10.             if (m_currentTab!=-1 && m_currentTab != clickedTabId)
  11.                 editFinished();
  12.             return false;
  13.         }
  14.         if (e->type() == QEvent::MouseButtonDblClick) {
  15.             //perhaps we need to start a new name editing action...
  16.             QMouseEvent* me = static_cast<QMouseEvent*>(e);
  17.             int clickedTabId = m_tabBar->tabAt(me->pos());
  18.             if (clickedTabId == -1)
  19.                 return result;
  20.             if (!m_allowRenamingDisabledTabs && !m_tabBar->isTabEnabled(clickedTabId))
  21.                 return result;
  22.             triggerRename(clickedTabId);
  23.             return true; //no further handling of this event is required
  24.         }
  25.     }
  26.  
  27.     //handle some events on the line edit to make it behave itself nicely as a rename editor
  28.     if (o == m_lineEdit) {
  29.         if (e->type() == QEvent::KeyPress) {
  30.             QKeyEvent* ke = static_cast<QKeyEvent*>(e);
  31.             if (ke->key() == Qt::Key_Escape) {
  32.                 m_lineEdit->deleteLater();
  33.                 return true; //no further handling of this event is required
  34.             }
  35.         }
  36.     }
  37.  
  38.     return result;
  39. }

As you can see, the TabRenamerCharm class handles events for two widgets: the tabbar, and the line edit itself. That means of course that it has to be installed on both of them as an eventfilter as well. The code that triggers the display of the line edit is the part that deals with the MouseButtonDblClick event. The actual code to create and position the line edit is in the triggerRename method, and that is also where I connect to its editingFinished() and destroyed() signals, where I install the event filter on it, and where I set the current tab name as the label and select it.

The installation of the eventfilter on the tabbar itself is done from the constructor. I have two constructors, one that takes a QTabBar directly, and one that takes a QTabWidget. The later one then simply finds the QTabBar child within the QTabWidget and installs itself on that widget.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

October 6, 2011

M_31 M_31
Lab Rat
98 posts

Thanks Andree..Thanks for your wounderfull Explanation..
its working fine..

October 6, 2011

Andre Andre
Area 51 Engineer
6076 posts

Glad that you got it to work. Marking as Solved.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ QML ListElement with translation      How QGraphicsTextItem can be not sized during scaling of QGraphicView ››

You must log in to post a reply. Not a member yet? Register here!