PDA

View Full Version : tabsClosable not working...! how to close tabs?



aurora
12th December 2011, 04:50
I have Qt creator 2.3.0(based on Qt 4.7.4)
In my program i created a tab using designer...and adding tabs to it dynamically...(wrote code for that)
That works fine...But i needed close button on the tabs....
so add the

ui->tabWidget->setTabsClosable(true);

but it displays close button, but when i click on that tab wont close!!!
whats wrong here? please somebody help me

Jonny174
12th December 2011, 05:01
QTabWidget *tabWidget = new QTabWidget( this );
tabWidget->setTabsClosable( true );
connect( tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(slotCloseTab()) );

Slot:


void MainWindow::slotCloseTab()
{
QTreeWidget *m_pProps = dynamic_cast<QTreeWidget *>( tabWidget->currentWidget() );

disconnect( m_pProps, 0, 0, 0 );

m_pProps->close();
m_pProps = NULL;
}

aurora
12th December 2011, 05:51
OMG....
My main window contains tabwidget....
In that i'm adding tabs dynamically.....when i tried the code given by u, it closed whole window, instead of that single tab!!!!!!!!!!!!!!
please help me whats wrong, here?

Jonny174
12th December 2011, 09:12
Write your example of a code of addition and tab closing

aurora
12th December 2011, 11:52
I’m creating TabWidget using designer, before running the program and adding widget in runtime…in this case....i'm creating "tab one", "tab two", "tab three"....
if i close “tab three”, then “tab three” closes thats fine,,,,
But if i close “tab two”, then both “tab two” and “tab three” closes
and if i close “tab one”, then all tab closes…i,e “tab one “, “tab two” and “tab three” closes….
whats wrong here? please help me,,,,,

MainWin.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public slots:
void slotCloseTab(int index);
}
class MainWindow : public QMainWindow
{
Q_OBJECT

public slots:
void slotCloseTab(int index);
}



MainWin.cpp


void MainWindow::DisplayFileContent(QString string)
{
textBox = new QTextBrowser;
ui->tabWidget->addTab(textBox,string);
connect(ui->tabWidget,SIGNAL(tabCloseRequested(int)),this,SLOT (slotCloseTab(int)));
f.close();
}



void MainWindow::slotCloseTab(int index)
{
this->ui->tabWidget->removeTab(index);
}

MarekR22
12th December 2011, 13:55
This code looks fine (f.close() looks strange).
From behavior description it looks like your slot is called more then once! When index value is bigger then number of pages this has no impact, that is why last pages are closed.
Put break point there (void MainWindow::slotCloseTab(int index)) and check callstack for the is sources of this extra calls.


edit: I know what is a problem. Every time you are creating new tab, you are making new connection between tabWidget and main window.
Move connect from void MainWindow::DisplayFileContent(QString string) to constructor of main window (after setupUi) and it will work as it should.

aurora
13th December 2011, 04:27
This code looks fine (f.close() looks strange).
From behavior description it looks like your slot is called more then once! When index value is bigger then number of pages this has no impact, that is why last pages are closed.
Put break point there (void MainWindow::slotCloseTab(int index)) and check callstack for the is sources of this extra calls.


edit: I know what is a problem. Every time you are creating new tab, you are making new connection between tabWidget and main window.
Move connect from void MainWindow::DisplayFileContent(QString string) to constructor of main window (after setupUi) and it will work as it should.


THANKS A LOT..:) I moved connect to constructor...it worked fine...:D