PDA

View Full Version : Rename a Tab of a QTabWidget



Qiieha
12th July 2011, 15:52
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!

mcosta
12th July 2011, 17:21
You MUST subclass QTabWidget because you have to subclass QTabBar and then use QTabBar::setTabBar that is protected.

MyTabWidget.cpp


#include "MyTabWidget.h"
#include "MyTabBar.h"

MyTabWidget::MyTabWidget(QWidget *parent) :
QTabWidget(parent)
{
MyTabBar* myTab = new MyTabBar;
setTabBar (myTab);
}


MyTabBar.cpp


#include "MyTabBar.h"

#include <QtGui/QInputDialog>
#include <QtGui/QMouseEvent>

MyTabBar::MyTabBar(QWidget *parent) :
QTabBar(parent)
{
}

void MyTabBar::mouseDoubleClickEvent(QMouseEvent *e)
{
if (e->button () != Qt::LeftButton) {
QTabBar::mouseDoubleClickEvent (e);
return;
}

int idx = currentIndex ();
bool ok = true;
QString newName = QInputDialog::getText (
this, tr ("Change Name"),
tr ("Insert New Tab Name"),
QLineEdit::Normal,
tabText (idx),
&ok);

if (ok) {
setTabText (idx, newName);
}
}

Qiieha
13th July 2011, 08:51
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.

mcosta
13th July 2011, 09:29
Why you don't want use subclassing??

Your solution needs to know the tabs position on the screen.
IMHO my solution is simpler.

Qiieha
13th July 2011, 10:09
OK thank u!
because i like to work with the designer. But your solution is better, so I subclass it.
:)

mcosta
13th July 2011, 10:19
you can subclass AND work with designer (using Widget Promoting)

Qiieha
13th July 2011, 10:26
oh, thats quite useful! I didn't know this is possible.

thank u!

greets

Doug Rogers
27th August 2021, 17:27
Why not get a signal from QTabBar?


class AlbumTabs : public QTabWidget
{
Q_OBJECT
public:

explicit AlbumTabs(QWidget* parent = nullptr);
~AlbumTabs();

public slots:
void onTabBarDoubleClicked(int index);


};

AlbumTabs::AlbumTabs(QWidget* parent) : QTabWidget(parent)
{
connect(tabBar(), &QTabBar::tabBarDoubleClicked, this, &AlbumTabs::onTabBarDoubleClicked);
}

void AlbumTabs::onTabBarDoubleClicked(int index)
{
bool ok = true;
QString newName = QInputDialog::getText(this, tr("Change Name"), tr("Insert New Tab Name"), QLineEdit::Normal, tabText(index), &ok);

if (ok)
{
setTabText(index, newName);
}
}

d_stranz
27th August 2021, 18:26
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.