Qt World Summit: Submit your Presentation

[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:

    void MainWindow::addTab(){
    mainTab *tab=new mainTab(this);
    ui->tabWidget->addTab(tab,"Download "+QByteArray::number(ui->tabWidget->count()+1));
    void MainWindow::tabChanged(){

    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 :)

  • What is find() supposed to be? A slot of mainTab?

    In addition, I most probably wouldn't disconnect() / connect() at all; add a slot to MainWindow which is connected to ui->actionDownload and simply calls actualTab->find().

  • 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;
    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;
    void MainWindow::addTab(){
    tab->append(new mainTab(this));
    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.

  • Lifetime Qt Champion


    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 :)

  • Lifetime Qt Champion

    You're welcome !

Log in to reply