Setting up tabs



  • Hello All!

    I have been trying to figure out how to make my tabs work to no avail. I have 5 files called mainwindow.cpp, mainwindow.ui, tabs.cpp, tabs.h, and tabs.ui. My mainwindow.ui contains a tab widget and a push button called 'new tab'. My tabs.ui contains a webview and various other buttons/lineedits. I would like it so that when I push the 'new tab' button it creates a new tab that is filled with the contents of tabs.ui. Is this possible? If so how would I go about doing that?

    Thanks for all your time!



  • Hi nlcky j
    You can set QWidget corresponding to eachtab.and set all contents of tabs.ui on QWidget.



  • Hey thanks for the reply! How would I go about doing that?



  • you have to implement some mapping logic using Qt 's containers like QMap, QSet etc.



  • Hi, When your tabs class (cpp/hpp and ui) are a QWidget based, yes this is very well possible.
    Connect a signal when a button is clicked to a slot that will be creating the new tab widget. In that slot, create a new widget, append to the tabWidget in your mainwindow and if needed store the pointer to that widget in a mainwindow class member variable.
    @
    // short example:
    void MainWindow::AddTabFromButton(void) // You could also use automatic slot creation etc, what every you like
    {
    QWidget NewTab = dynamic_cast<QWidget>(new MyTabs(this));
    m_TabWidget->addTab(NewTab, MyTabName); // Ok, MyTabName is the QString that will be placed in the tab itself.
    m_TabList.append(NewTab);
    }
    @
    Your m_TabWidget is then the TabWidget mainwindow class member (or if designer is used: ui->TabWidget.addTab());
    The m_TabList is a mainwindow class member of the type QList<QWidget*> m_TabList.
    Hope this snippet helps.


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, why the type cast ?

    A bit of nitpicking: since it's QWidget *, it should be a qobject_cast if you really want to cast



  • Hi,
    Yup, just a coding standard option here in my company. You are very right that no type cast is needed because of the base class pointer stuff. And yes, I have forgotten the qobject_cast, thx, will remember next time.
    But to the casting point, our coding standard explicitly states that even when it is logical and obvious we still need explicit typecast! This to have no confusion between programmers. It's more a C thing really.
    Greetz


  • Lifetime Qt Champion

    Ok, fair enough, thanks for the explanation :)



  • Okay, sorry to bring up an old thread.

    Ive read your posts, and they have helped a lot.
    this is where I am at with the project:
    @#include "mainwindow.h"
    #include <QToolButton>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    newTabButton = new QToolButton(this);
    connect(newTabButton, SIGNAL(clicked()), SLOT(newTabButtonClicked()));

    webview = new QWebView(this);
    QString Url = "http://www.google.com/";
    webview->load(Url);
    
    URLbar = new QLineEdit(this);
    
    SearchBar = new QLineEdit(this);
    
    tabWidget = new QTabWidget(this);
    
    //resides within mainLayout. Level 3 Layout.
    QToolBar *navBar = addToolBar(tr("Navigation"));
    navBar->addWidget(URLbar);
    navBar->addWidget(SearchBar);
    
    //resides within masterLayout. Level 2 Layout.
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(navBar);
    mainLayout->addWidget(webview);
    
    //contains mainLayout
    QWidget *tabContent = new QWidget;
    tabContent->setLayout(mainLayout);
    
    //master layout. Highest Layout.
    QVBoxLayout *masterLayout = new QVBoxLayout;
    masterLayout->addWidget(newTabButton);
    masterLayout->addWidget(tabWidget);
    
    QWidget *masterWidget = new QWidget;
    masterWidget->setLayout(masterLayout);
    
    tabWidget->setLayout(mainLayout);
    tabWidget->addTab(tabContent, "1st Tab");
    
    setCentralWidget(masterWidget);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::newTabButtonClicked()
    {
    tabWidget->addTab(tabContent, "New Tab");
    }
    @
    I am running qt creator on my mac OS X 10.9 (Mavericks). Whenever I hit the newTabButton, the application crashes for no apparent reason. How can I fix this?


  • Lifetime Qt Champion

    Did you run your code through the debugger ?

    Did you initialize tabContent to something valid ?

    Looking at your code you did not. In your constructor you are shadowing tabContent.



  • SGaist thanks for the reply!

    Where/how do I initialize tabContent to something valid?


  • Lifetime Qt Champion

    Like I said before, in your constructor you are shadowing your class member variable which is probably not what you wanted to do


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.