PDA

View Full Version : how to remove tab with closable button.



ishkabible
20th September 2010, 00:42
so i have a QTabWidget and i want the tabs to be removable via the provided close button for each tab. how do i set it up so that 1) they will close when i click it and 2) i can delete what they where storing becuase i dynamically allocated there page widgits.

wysota
20th September 2010, 01:06
QTabWidget::tabCloseRequested()
QObject::deleteLater()


Next time, please at least take a look at the docs before asking a question.

ishkabible
20th September 2010, 01:13
i looked this time just didn't find it, i think i can remember to look 20min after you told me to in another thread :)

wysota
20th September 2010, 01:25
So next time browse the docs two times before asking a question :rolleyes:

ishkabible
20th September 2010, 01:29
i still dont understand how implement these functions to work, how do i add checks for things to the "event loop" exactly?

would i use a function like this?


MainWindow::eventFilter(QObject* obj, QEvent event) {
if(obj==this) {
for(unsigned int i=0;i<Tabs->size();++i) {
if(tabCloseRequested(i)) {
delete Tabs->widget(i)->deleteLater();
Tabs->removeTab(i);
}
}
}
}

wysota
20th September 2010, 01:35
i still dont understand how implement these functions to work,
The signal tells you which tab requested to be closed. Connect a slot to it and remove the tab in question.


how do i add checks for things to the "event loop" exactly?
What checks and what things?

ishkabible
20th September 2010, 01:52
ok i'm pretty confused, i thought that i had to add a function to check for a signal to emitted in the event to the event loop, i am guessing this is done with slots but i don't know how to use slots. i use the QObject::connect function to connect a signal and a slot right?, could you explain how this is done and what two objects should be connected for this to be acomplised?

wysota
20th September 2010, 01:57
i use the QObject::connect function to connect a signal and a slot right?
Yes.

could you explain how this is done and what two objects should be connected for this to be acomplised?
You have to create your own slot compatible with the signal I gave you and connect() the two. The slot will receive the index of the tab that wishes to be closed and you will just have to remove and delete it.

If that's still not clear enough (and even if it is) you should read about signals and slots in the docs.

ishkabible
20th September 2010, 02:03
ok so i tried(did not try to handle memory leeks)


QObject::connect(Tabs, SIGNAL(tabCloseRequested(int)),
Tabs, SLOT(removeTab(int)));

i thought that if this worked then i would make a function in my MainWindow class that would both delete the content and remove the tab but this did not work, what am i doing wrong?

i got it to work!! i used a function that deleted the allocated widgit with deleteLater, i used


QObject::connect(Tabs, SIGNAL(tabCloseRequested(int)),
this, SLOT(RemoveTab(int)));
, thanks for helping :)