[Solved]Dynamically connect parent signal to current tab function in tab widget.
-
I have mainwindow with tab widget and I want dynamically connect signals from mainwindow menu bar to actual tab widget function. For example if I have 2nd tab action find will find matches in 2nd tab, if I have 3rd tab it'll look in 3rd tab etc. But I just can't find out how to access the function in actual tab widget, I have code:
@
//in constructor:
connect(ui->actionNew_tab,SIGNAL(triggered()),this,SLOT(addTab()));
connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(tabChanged()));void MainWindow::addTab(){
mainTab *tab=new mainTab(this);
ui->tabWidget->addTab(tab,"Download "+QByteArray::number(ui->tabWidget->count()+1));
}
void MainWindow::tabChanged(){
disconnect(ui->actionDownload,SIGNAL(triggered()),oldTab,SLOT(find()));
connect(ui->actionDownload,SIGNAL(triggered()),actualTab,SLOT(find()));
}
@but the problem is I can't access function find(), I tried ui->tabWidget, ui->tab and ui->tabWidget->currentWidget() but neither contains it. (I don't even know how to disconnect previous signal because I cant access it). Please help a newbie out with his problem, thanks :)
-
Sorry my post is a bit confusing, I had terrible headache when I was typing it. Find() is slot of each tab and I want to use it to look to find matches in it's QStringList variable. Uhm I should probably try only make one Find() slot in MainWindow and look in actual tab list.
I found out why I can't access the data, it was simple, I had to declare mainTab* tab as global variable. My actual problem is when I declare it this way:
@
//in header file
mainTab* tab;
//function
void MainWindow::addTab(){
tab=new mainTab(this);
ui->tabWidget->addTab(tab,"Download "+QByteArray::number(ui->tabWidget->count()+1));
}
@
tab is pointing only to last tab created, so I can't acces variables from previous declared tabs, I tried to do it this way:
@
//in header
QList<mainTab> tab;
//function
void MainWindow::addTab(){
tab->append(new mainTab(this));
ui->tabWidget->addTab(tab->at(tab->count()-1),"Download"+QByteArray::number(ui->tabWidget->count()+1));
}@
but this gave me errors:
error: no matching function for call to ‘QList<mainTab>::append(mainTab)’
error: no matching function for call to ‘QTabWidget::addTab(const mainTab&, const QByteArray)’If someone can help me with this it would be nice, I think this isn't the best way to make tabs so if there is some better way please correct me, or if I can access the variables from previous made tabs declared the first method I did. I have to go afk now, will look at it later today, thanks for any help.
-
Hi,
QList<mainTab> is "wrong", you can't have QList of QObject as the copying QObject is forbidden.
QList<mainTab> * is also a bad idea, generally you don't need pointers to QList object.
Following your code you should have a QList<mainTab*>.
You could however also use the "findChildren":http://qt-project.org/doc/qt-4.8/qobject.html#findChildren template function to get a list of mainTab from your QTabWidget.
Hope it helps
-
[quote author="SGaist" date="1364376934"]
Following your code you should have a QList<mainTab*>.[/quote]Thank you very much this is working :)
-
You're welcome !