[SOLVED] Renaming a tab by double click and then setting Main Window title to new name



  • Hey everyone,

    I am working on a program where I have a class MainWindow that is derived from QMainWindow. In that class, I initialize mTabWidget = new QTabWidget, then I set this as my central widget (setCentralWidget(mTabWidget). Then to add a tab I use mTabWidget->addTab(scrollArea, fileName), where scrollArea is a QScrollArea pointer and fileName is a QString. Then I set the title of the main window using setWindowTitle(fileName).

    Now my goal is to be able to double click on a tab to rename it. I’ve read that I need to subclass the QTabBar class as well as the QTabWidget to do this. I have this working, but can anyone give me an example on how they would do this or an alternate idea.

    The second part of my problem is that when I double click the tab to rename, I can get it to rename the tab but not set the window title to the new tab name. In my subclass I used the setWindowTitle() method again, but it does nothing.

    I am stumped on this issue and any help would be extremely appreciated.

    Thanks!


  • Lifetime Qt Champion

    Hi,

    This "thread":http://qt-project.org/forums/viewthread/10390 might help you to get started



  • Thanks SGaist! I implemented my own TabWidget class derived from QTabWidget so I can access the TabBar() member in order to access the double click event of the TabBar. What's really got me puzzled is how to call the setWindowTitle() method of the MainWindow class that the TabWidget object is initialized in. I need the main window title to match the current tab name. Is there some sort of way to monitor if there is a tab rename?


  • Lifetime Qt Champion

    Add a signal to your custom QTabWidget e.g. tabTitleChanged(const QString& newTitle) and emit it once you finished the editing part.

    You can then connect it to setWindowTitle



  • That worked perfectly. Thanks so much brother! Take care.


  • Lifetime Qt Champion

    You're welcome !

    Since you have it all working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.