Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting up tabs
QtWS25 Last Chance

Setting up tabs

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • IamSumitI Offline
      IamSumitI Offline
      IamSumit
      wrote on last edited by
      #2

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

      Be Cute

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

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

          Be Cute

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            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.

            Greetz, Jeroen

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                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

                Greetz, Jeroen

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Ok, fair enough, thanks for the explanation :)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nicky j
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        nicky j
                        wrote on last edited by
                        #11

                        SGaist thanks for the reply!

                        Where/how do I initialize tabContent to something valid?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved