Results 1 to 8 of 8

Thread: [SOLVED] How to add tabs dynamically to a QTabWidget?

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1

    Question [SOLVED] How to add tabs dynamically to a QTabWidget?

    Hey Guys!

    As you all can see I'm new to Qt and to this forum so please be patient with me ;-)
    After playing around with a few tutorials I considered myself to be able to design a mainwindow-application that features dynamic tabs. That's means I want to allow the user to add a new instance of a predesigned tab while the program is running. I used the Qt Designer to build the forms. So here is my code, I hope you guys can help me. Thank you for your attention.

    The QAction "actionNeues_PlotTab" is part of the menubar in the mainwindow and after compiling it runs correctly, means I get to read my qDebug messages. So it's basicly all about the addPlotTab() slot I guess. The Ui_PlotTab class is also something I generated with the Qt Designer and hopefully adds the right child-widgets to the one widget that will become my new tab.

    Compiling doesn't make any trouble but when running and using the menubar entry that should reveal a new tab nothing happens although the new tabs seem to have an index.

    mainwindowimpl.h
    Qt Code:
    1. #ifndef MAINWINDOWIMPL_H
    2. #define MAINWINDOWIMPL_H
    3. //
    4. #include <QMainWindow>
    5. #include "ui_mainwindow.h"
    6. //
    7. class MainWindowImpl : public QMainWindow, public Ui::MainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
    12. private slots:
    13. void addPlotTab();
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    mainwindowimpl.cpp
    Qt Code:
    1. #include "mainwindowimpl.h"
    2. #include "ui_plottab.h"
    3. #include <QtGui>
    4. //
    5. MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
    6. : QMainWindow(parent, f)
    7. {
    8. setupUi(this);
    9.  
    10. connect(actionNeues_PlotTab, SIGNAL(activated()), this, SLOT(addPlotTab()));
    11. }
    12. //
    13. void MainWindowImpl::addPlotTab()
    14. {
    15. QWidget NewTab;
    16. QString name="NewTab";
    17. Ui_PlotTab Ui_NewTab;
    18. Ui_NewTab.setupUi(&NewTab);;
    19. tabWidget->setUpdatesEnabled(true);
    20. tabWidget->addTab(&NewTab, name);
    21. int a = tabWidget->indexOf(&NewTab);
    22. QWidget NewTab2;
    23. QString name2="NewTab2";
    24. Ui_NewTab.setupUi(&NewTab2);
    25. tabWidget->addTab(&NewTab2, name2);
    26. int b = tabWidget->indexOf(&NewTab2);
    27. qDebug() << "I'm still not working!" << a << "\t" << b;
    28. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindowimpl.h"
    3. //
    4. int main(int argc, char ** argv)
    5. {
    6. QApplication app( argc, argv );
    7. MainWindowImpl win;
    8. win.show();
    9. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by madcat2709; 26th March 2009 at 17:27. Reason: It's solved

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Add Tabs dynamically to a QTabWidget

    Quote Originally Posted by madcat2709 View Post
    Qt Code:
    1. void MainWindowImpl::addPlotTab()
    2. {
    3. QWidget NewTab;
    4. QString name="NewTab";
    5. Ui_PlotTab Ui_NewTab; //created
    6. Ui_NewTab.setupUi(&NewTab);;
    7. tabWidget->setUpdatesEnabled(true);
    8. tabWidget->addTab(&NewTab, name);
    9. int a = tabWidget->indexOf(&NewTab);
    10. QWidget NewTab2;
    11. QString name2="NewTab2";
    12. Ui_NewTab.setupUi(&NewTab2);
    13. tabWidget->addTab(&NewTab2, name2);
    14. int b = tabWidget->indexOf(&NewTab2);
    15. qDebug() << "I'm still not working!" << a << "\t" << b;
    16. }//destroyed!
    To copy to clipboard, switch view to plain text mode 
    Hi,

    you have to create your object on the heap with new! because the way you do (creating on the stack), the new pages will be deleted at the end of the scope.

    Lykurg

  3. The following user says thank you to Lykurg for this useful post:

    madcat2709 (26th March 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1

    Default Re: How to add tabs dynamically to a QTabWidget?

    Thank very much you for your fast answer!
    I've had that exact problem before at some other point and did not even think about it...
    Hopefully I learned now! Same think with my QWidget Instance - to remain after the scope they all need to be on heap ;-)

  5. #4
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: [SOLVED] How to add tabs dynamically to a QTabWidget?

    Hello, i recently started with qt and i wanted to insert tabs dynamically whenever i pressed a centrain button.
    I've based myself on the code above, but it doenst succeed 100%

    i can insert new tabs when clicking that button but the ui/layout is not what i defined in the designer. In fact there is no 'design', only new tabs can be inserted.
    I have created a ui using the designer and did the same thing the guy above did.

    The coded i've corrected a bit :
    Qt Code:
    1. QWidget NewTab;
    2. QString name="NewTab";
    3. Ui_PlotTab *Ui_NewTab= new Ui_PlotTab; // This is implemented
    4. Ui_NewTab->setupUi(NewTab); //but makes no difference when i delete these lines
    5. tabWidget->addTab(NewTab, name);
    To copy to clipboard, switch view to plain text mode 

    i cant seem to find my/his mistake.
    Why is QT just adding a new tab and not using my ui in that tab?

    thank you very much!

  6. #5
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1

    Default Re: [SOLVED] How to add tabs dynamically to a QTabWidget?

    Hey!

    Im for sure not a professional at these issus but to me at first it wasn't clear that I have to design the layout of my tabs not at the first tab that the TabWidget in the Designer shows. One has to create another Designer file with the Widget draft of the Designer and create an whole independent UI. In the lines you've deletet I used to create a new QWidget, fill it with this mentioned independent layout and put this Widget afterwards into a new tab.

    So if you have only designed the layout of your first tab the new tabs will only contain empty widgets and apear to be amtpy tabs. You have to fill them with Widgets that already contain an UI.

    Maybe that's the point maybe not. Hope it will help you ;-)

  7. #6
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: [SOLVED] How to add tabs dynamically to a QTabWidget?

    Thank you for your reply
    i have added a screenshot to show you where i'm at :-)

    as you can see: i can dynamically add tabs using your code
    (btw: i havent deleted those lines, it was just to show it didn't make any difference)
    but then it doesnt load the ui i created and which you can also see in the screenshot.

    my code is:
    Qt Code:
    1. QWidget *NewTab=new QWidget(this);
    2. const QString name(name.c_str());
    3. Ui_Form * Ui_NewTab=new Ui_Form; //This is the ui file for the ui in the screen
    4. Ui_NewTab->setupUi(NewTab);
    5. tabWidget->addTab(NewTab, name);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  8. #7
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: [SOLVED] How to add tabs dynamically to a QTabWidget?

    This is what i have created

    Qt Code:
    1. QWidget *NewTab=new QWidget(this); // create tab
    2. const QString names(name.c_str());
    3. intensityframe2 * w=new intensityframe2(NewTab); // fill the tab with my design
    4. w->show(); // show it
    5. tabWidget->addTab(NewTab, names); // and add it to the tab holder
    To copy to clipboard, switch view to plain text mode 

    output:
    it creates the tab, but does not doesnt add my layout to it; it's just an empty tab.
    Although, when i change 'NewTab' on line 3 to 'this', it draws my ui in the mainscreen, which offcourse isnt the purpose but then i know the ui wasnt the problem.

    The design i use in the tab is inherited from QFrame

    Can anybody tell why it isnt showing my layout in the tab?

  9. #8
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: [SOLVED] How to add tabs dynamically to a QTabWidget?

    Qt Code:
    1. QWidget *NewTab = new [U]QWidget([/U]this); // create tab
    2. const QString names(name.c_str());
    3. intensityframe2 * w=new intensityframe2(NewTab); // fill the tab with my design
    4. w->show(); // show it
    5. tabWidget->addTab(NewTab, names); // and add it to the tab holder
    To copy to clipboard, switch view to plain text mode 
    try this
    Qt Code:
    1. tabWidget->addTab(new Intensityframe2, name.c_str());
    To copy to clipboard, switch view to plain text mode 
    If your class inherits QObject, just pass this to Intensityframe2 instead of create a redundant QWidget. But when you add the widget to addTab(). The tabwidget will take control of ownership if you had read the documentation on QTabWidget and calls show itself too. I don't know why you're using c_str() and casting to const when you can just pass it to the constructor as is. and QWidget *parent should be the last parameter in the constructor not the first just so you can add a parent of the object or not. and have a default value of 0. QWidget *parent = 0; And just new the IntensityFrame2 inside the constructor unless you need to do stuff with it and add a QList to hold it.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

Similar Threads

  1. Changing names of QTabWidget tabs
    By RThaden in forum Qt Programming
    Replies: 7
    Last Post: 29th January 2019, 21:25
  2. QTabWidget with same tabs
    By Djony in forum Qt Programming
    Replies: 20
    Last Post: 24th December 2011, 12:20
  3. Replies: 1
    Last Post: 16th January 2009, 15:48
  4. Delayed Rendering of QTabWidget Tabs
    By mclark in forum Qt Tools
    Replies: 13
    Last Post: 14th May 2007, 22:53
  5. Switching off all tabs in QTabWidget
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 30th August 2006, 17:10

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.