Results 1 to 8 of 8

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.