PDA

View Full Version : Prompt before closing a Tab



padma
17th September 2017, 20:18
I have a tabwidget with a bunch of tabs.
If the tab title is a customtext, then on closing the tab, I want to show a prompt before deciding if I need to close the tab or not.

I connected the tabcloserequested signal to the slot
I tried to print the tab title of the closed tabindex obtained.
But it currently prints an empty string.


Can someone please point me to ways I might be able to implement this?

Thank you! :)

d_stranz
18th September 2017, 06:07
I think you need to show the code for your slot. The QTabWidget::tabText() method should give you the text.

Unfortunately, if you set tabs to be closable, there is no way that you can prevent a tab from being closed once the close button has been clicked. The tabCloseRequested() signal is there so you can clean up anything which might need cleaning up before the tab is closed and the widget on it is deleted. Even if you show a prompt in your slot, the tab and its contents are already on the way out and you can't stop it.

If you want control over closing tabs, probably the best way to do it is to implement a context menu for the QTabBar with a Close menu item and connect the action to a slot that can post your prompt and close the tab if desired using removeTab().

padma
18th September 2017, 18:24
void Home::slot(int tabIndex)
{

QString currentTabText = ui->tabWidget->tabText(tabIndex);

qDebug() <<"tabIndex: "<<tabIndex;
qDebug() <<"currentTabText:"<<currentTabText;

if(currentTabText == "CustomTabTitle"){
qDebug() << "Detected CustomTabTitle";
//Do some work
}
}

Output:

tabIndex: 0
currentTabText: ""

d_stranz
18th September 2017, 22:31
Are you sure that ui->tabWidget is the same widget that you connected the slot to? Did you click the first tab (index 0)?

GeneCode
19th September 2017, 02:00
I have a tabwidget with a bunch of tabs.
If the tab title is a customtext, then on closing the tab, I want to show a prompt before deciding if I need to close the tab or not.

I connected the tabcloserequested signal to the slot
I tried to print the tab title of the closed tabindex obtained.
But it currently prints an empty string.


Can someone please point me to ways I might be able to implement this?

Thank you! :)

Yes.
1. Find a book and learn it.
2. Done.

padma
19th September 2017, 03:04
Are you sure that ui->tabWidget is the same widget that you connected the slot to? Did you click the first tab (index 0)?

I have a main window with a tabwidget(ui->tabWidget). I opened a single tab, and then closed it(the first tab).

connect(ui->tabWidget,SIGNAL(tabCloseRequested(int)),this,SLOT (on_tabWidget_tabCloseRequested(int)));

I used the connect statement in mainwindow.cpp.

I defined the slot in the mainwindow class.

Added after 45 minutes:


yes.
1. Find a book and learn it.
2. Done.

ggwp. \(^_^)/

d_stranz
19th September 2017, 18:04
I am not sure what is going on in your code. Here is a very simple QMainWindow-based app:



// TabApp.h

#ifndef TABAPP_H
#define TABAPP_H

#include <QtWidgets/QMainWindow>

class QTabWidget;

class TabApp : public QMainWindow
{
Q_OBJECT

public:
TabApp(QWidget *parent = 0);
~TabApp();

private slots:
void onTabWidgetTabCloseRequested( int index );

private:
void addTab();

private:
QTabWidget * mpTabWidget;
};

#endif // TABAPP_H




// TabApp.cpp

#include "TabApp.h"

#include <QTabWidget>
#include <QDebug>

TabApp::TabApp(QWidget *parent)
: QMainWindow(parent)
{
mpTabWidget = new QTabWidget( this );
mpTabWidget->setTabsClosable( true );

addTab();
addTab();

connect( mpTabWidget, &QTabWidget::tabCloseRequested, this, &TabApp::onTabWidgetTabCloseRequested );
setCentralWidget( mpTabWidget );
}

TabApp::~TabApp()
{
}

void TabApp::onTabWidgetTabCloseRequested( int index )
{
qDebug() << "Index:" << index;
qDebug() << "Text:" << mpTabWidget->tabText( index );
}

void TabApp::addTab()
{
int count = mpTabWidget->count();

QString tabText = QString( "Widget %1" ).arg( count + 1 );
QWidget * pTab = new QWidget( this );
mpTabWidget->addTab( pTab, tabText );
}




// main.cpp

#include "TabApp.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TabApp w;
w.show();
return a.exec();
}


If I click the close button on the second tab ("Widget 2"), qDebug() prints:

Index: 1
Text: "Widget 2"

If I click the close button the first tab ("Widget 1"), qDebug() prints:

Index: 0
Text: "Widget 1"

One thing I did learn from this is that I was wrong about the tab being closed automatically. When I click the close button, my slot gets called, but the tab does not close automatically. So you can do what you intended - to put up a prompt and allow the user to choose whether the tab should be closed or not.

You should probably compare your code to this and see if there are any significant differences (aside from the fact that you define your UI in a Qt Designer file and I have created mine in code - but that makes no difference at runtime).



1. Find a book and learn it.
2. Done.


Looked for a book that had this example in it, didn't find one. Had to write it myself.

padma
19th September 2017, 18:25
Thank you very much for the example, it really helps! :)

I'll cross compare, and figure it out from here, I think I made a silly mistake somewhere.