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. [SOLVED] Changing QTabWidget's tab Name at runtime..
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 23.0k 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.
  • M Offline
    M Offline
    M_31
    wrote on last edited by
    #1

    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..

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        M_31
        wrote on last edited by
        #3

        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

        @
        //ui->tabWidget->viewport()->installFilter( this );
        //! 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???

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.
          @
          bool TabRenamerCharm::eventFilter(QObject * o, QEvent * e)
          {
          bool result = QObject::eventFilter(o, e);

          if (o == m_tabBar) {
              if (e->type() == QEvent::MouseButtonPress) {
                  //see if the user is trying to move away from the editing by clicking another tab
                  QMouseEvent* me = static_cast<QMouseEvent*>(e);
                  int clickedTabId = m_tabBar->tabAt(me->pos());
                  if (m_currentTab!=-1 && m_currentTab != clickedTabId)
                      editFinished();
                  return false;
              }
              if (e->type() == QEvent::MouseButtonDblClick) {
                  //perhaps we need to start a new name editing action...
                  QMouseEvent* me = static_cast<QMouseEvent*>(e);
                  int clickedTabId = m_tabBar->tabAt(me->pos());
                  if (clickedTabId == -1)
                      return result;
                  if (!m_allowRenamingDisabledTabs && !m_tabBar->isTabEnabled(clickedTabId))
                      return result;
                  triggerRename(clickedTabId);
                  return true; //no further handling of this event is required
              }
          }
          
          //handle some events on the line edit to make it behave itself nicely as a rename editor
          if (o == m_lineEdit) {
              if (e->type() == QEvent::KeyPress) {
                  QKeyEvent* ke = static_cast<QKeyEvent*>(e);
                  if (ke->key() == Qt::Key_Escape) {
                      m_lineEdit->deleteLater();
                      return true; //no further handling of this event is required
                  }
              }
          }
          
          return result;
          

          }
          @

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            M_31
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bandora
                wrote on last edited by
                #7

                Hi,

                Sorry for reopening/bumping an old thread but, I didn't think it is necessary to create a whole new thread while I'm having the same issue as described here. I am modifying a project called EasyPaint (found on github), and I am trying to implement a tab name change (double click on the tab to change name)..

                I tried implementing it the way it is described by Andre, but I wasn't successful.

                I'm still a beginner when it comes to Qt, so any help is greatly appreciated!

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  So... what have you tried already? Where did you get stuck?

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bandora
                    wrote on last edited by
                    #9

                    Thank you for replying back, well like I said I'm a beginner so please forgive me about what I'm going to say lol..

                    Are those m_currentTab, m_tabBar, tabAt..etc made up by you? Or are they functions from any of the libraries? And what are they suppose to do basically. I tried to copy the code that you have to put it in EasyPaint (I knew I had to change a couple of things, but I guess I didn't fully understand what your code is doing exactly; again beginner's problems! xD)

                    Also, I'm not sure of what I've done to initialize the class is this correct?

                    @class TabRenamerCharm : public QObject
                    {

                          bool eventFilter(QObject * o, QEvent * e);
                    

                    }@

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Hmmm... right.

                      Those are quite basic questions, and I'm not sure that this technique will be suitable for you then. The code I gave above is just a part of the whole class, not all of it. There are lots of bits and pieces missing to make it work (though the method is the most important one).

                      The m_* are member variables. m_* is a naming pattern for these that is quite common, in order to distinguish them from local variables in function implementations (that don't get such a prefix). So in a sense, I did make them up. Your declaration (not initialization) is not completely correct either, and certainly not complete. For one, you will need to add all these member variables to it.

                      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