Results 1 to 8 of 8

Thread: Prompt before closing a Tab

  1. #1
    Join Date
    Dec 2016
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Prompt before closing a Tab

    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!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,172
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Prompt before closing a Tab

    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().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2016
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Prompt before closing a Tab

    Qt Code:
    1. void Home::slot(int tabIndex)
    2. {
    3.  
    4. QString currentTabText = ui->tabWidget->tabText(tabIndex);
    5.  
    6. qDebug() <<"tabIndex: "<<tabIndex;
    7. qDebug() <<"currentTabText:"<<currentTabText;
    8.  
    9. if(currentTabText == "CustomTabTitle"){
    10. qDebug() << "Detected CustomTabTitle";
    11. //Do some work
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Output:

    tabIndex: 0
    currentTabText: ""

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,172
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Prompt before closing a Tab

    Are you sure that ui->tabWidget is the same widget that you connected the slot to? Did you click the first tab (index 0)?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Prompt before closing a Tab

    Quote Originally Posted by padma View Post
    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.

  6. #6
    Join Date
    Dec 2016
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Prompt before closing a Tab

    Quote Originally Posted by d_stranz View Post
    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,SLO T(on_tabWidget_tabCloseRequested(int)));

    I used the connect statement in mainwindow.cpp.

    I defined the slot in the mainwindow class.


    Added after 45 minutes:


    Quote Originally Posted by genecode View Post
    yes.
    1. Find a book and learn it.
    2. Done.
    ggwp. \(^_^)/
    Last edited by padma; 19th September 2017 at 03:04.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,172
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Prompt before closing a Tab

    I am not sure what is going on in your code. Here is a very simple QMainWindow-based app:

    Qt Code:
    1. // TabApp.h
    2.  
    3. #ifndef TABAPP_H
    4. #define TABAPP_H
    5.  
    6. #include <QtWidgets/QMainWindow>
    7.  
    8. class QTabWidget;
    9.  
    10. class TabApp : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. TabApp(QWidget *parent = 0);
    16. ~TabApp();
    17.  
    18. private slots:
    19. void onTabWidgetTabCloseRequested( int index );
    20.  
    21. private:
    22. void addTab();
    23.  
    24. private:
    25. QTabWidget * mpTabWidget;
    26. };
    27.  
    28. #endif // TABAPP_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // TabApp.cpp
    2.  
    3. #include "TabApp.h"
    4.  
    5. #include <QTabWidget>
    6. #include <QDebug>
    7.  
    8. TabApp::TabApp(QWidget *parent)
    9. : QMainWindow(parent)
    10. {
    11. mpTabWidget = new QTabWidget( this );
    12. mpTabWidget->setTabsClosable( true );
    13.  
    14. addTab();
    15. addTab();
    16.  
    17. connect( mpTabWidget, &QTabWidget::tabCloseRequested, this, &TabApp::onTabWidgetTabCloseRequested );
    18. setCentralWidget( mpTabWidget );
    19. }
    20.  
    21. TabApp::~TabApp()
    22. {
    23. }
    24.  
    25. void TabApp::onTabWidgetTabCloseRequested( int index )
    26. {
    27. qDebug() << "Index:" << index;
    28. qDebug() << "Text:" << mpTabWidget->tabText( index );
    29. }
    30.  
    31. void TabApp::addTab()
    32. {
    33. int count = mpTabWidget->count();
    34.  
    35. QString tabText = QString( "Widget %1" ).arg( count + 1 );
    36. QWidget * pTab = new QWidget( this );
    37. mpTabWidget->addTab( pTab, tabText );
    38. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2.  
    3. #include "TabApp.h"
    4. #include <QtWidgets/QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. TabApp w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    padma (19th September 2017)

  9. #8
    Join Date
    Dec 2016
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Prompt before closing a Tab

    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.

Similar Threads

  1. Replies: 1
    Last Post: 26th November 2014, 06:50
  2. Where can i found QT 5.1.0 command prompt
    By AsfK in forum General Programming
    Replies: 2
    Last Post: 2nd September 2013, 15:44
  3. Qt Command Prompt
    By stijn in forum Installation and Deployment
    Replies: 0
    Last Post: 4th June 2012, 15:15
  4. Suggestions on Login Prompt
    By fnmblot in forum Qt Programming
    Replies: 9
    Last Post: 22nd May 2009, 20:54
  5. calling Cmd prompt
    By jsmith in forum Qt Programming
    Replies: 1
    Last Post: 8th May 2009, 10:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.