Can not control the position of a QToolbar within a QTabWidget

  • Hi guys,

    I have a, maybe for most of you really stupid, question, but I'm thinking about that for the last five hours, including a lot of asking Dr. Google, but I was not able to find a solution... :-(

    I try to place a QToolbar within a QTabWidget (this is because within the tabs different kinds of widgets will run, both 3D and 2D representation of models, which will need different edit and filtering functions, which I want to place as near as possible to the graphical representation of the data). Basicaly it works, but the QToolbar ist located on the bottom of the widget, and I want it on the top...

    Following the relevant parts of my code:

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        // Setup GUI with application name and version
        QString windowTitle = applicationName + " " + version;
        this->setWindowTitle (windowTitle);
        // Create new toolbar for Cloud Viewer tab widget
        QToolBar *cloudViewerToolbar = new QToolBar;
        // Create actions for the Cloud Viewer toolbar
        QAction *actionFitToScreen = new QAction(QIcon(":/view/fit"),tr("Fit to screen"), this);
        // Add actions to Cloud Viewer toolbar
        // Add toolbar to Cloud Viewer tab widget
        // Connect actions for Cloud Viewer toolbar with slots
        connect(actionFitToScreen, SIGNAL(triggered()), this, SLOT(on_actionFit_to_screen_triggered()));
        // Set up the QVTK Cloud Viewer widget
        viewer.reset (new pcl::visualization::PCLVisualizer ("viewer", false));
        ui->qvtkWidget->SetRenderWindow (viewer->getRenderWindow ());
        viewer->setupInteractor (ui->qvtkWidget->GetInteractor (), ui->qvtkWidget->GetRenderWindow ());
        ui->qvtkWidget->update ();

    As mentioned, this code works, my only problem ist the positioning of the QToolbar on the bottom of the tab widget...

    Can someone tell me, what I do not understand and point me to the right direction?

    Thanks in advance,

  • @cheops
    To be clear: You wish to add a single QToolbar onto a QTabWidget as a whole, not on each page. You will presumably be putting tab-pages on the QTabWidget, but your code does not do that yet. You have added the toolbar and can tell it will come at the bottom of the tab-pages, but you want above the tabs. Is that correct?

  • @JNBarchan


    thanks for your reply! I hope, I inderstand what you mean... no, I want to add a different QToolbar on each page of the tab widget! This seems to be no problem, the only thing I'm fighting with is, that the toolbar appears on the bottom of the tab page...

    In my example I have two tabs, one for 3D-models and one for pictures. I need different filters and editing functions for this two tabs, that's why I want two show differene toolbars in the tabs...


  • @cheops

    I try to place a QToolbar within a QTabWidget

    // Add toolbar to Cloud Viewer tab widget

    I take these statements to indicate that ui->tabCloudViewer is the QTabWidget. Are you saying it is a page-widget on the QTabWidget? What type is it?

  • Lifetime Qt Champion


    Except that you are currently adding that toolbar under your QTabWidget and not at the bottom of the widget inside the tab.

    Since you want different tabs to have different tool bars, you should consider making a base "tab widget" class that contains what you need and that you can configure.

  • @JNBarchan
    No, ui->tabCloudViewer is a page on a QTabWidget with the name tabWidget...

  • Lifetime Qt Champion

    OK, thanks for the clarification.

    In that case, you can use insertWidget so you can prepend the toolbar.

  • @SGaist

    my problem ist, that I'm not able to configure the tab widget in a way, that the toolbar appears at the top of the tab page... So to create a new class wouldnt change anything, because I would also in such a case not be able to configure the tab page in the way I want...

    Or am I completely wrong?

  • @cheops
    @SGaist is telling you that you can have the toolbar at the top:

    // Add toolbar to Cloud Viewer tab widget

    Use insertWidget instead of addWidget. addWidget always adds at end.

  • Lifetime Qt Champion

    The widget you should have as tab should basically be:

    • A QWidget with
    • A QVBoxLayout and
    • Your VTK widget inside that layout.

    And in that case you can insert the QToolBar as proposed.

    Or you can make a custom widget with the same content plus a method that would handle adding/setting a QToolBar in that tab.

  • @JNBarchan Class QLayout has no member insertWidget?

  • Lifetime Qt Champion

    QBoxLayout::insertWidget. QBoxLayout is the base class of QVBoxLayout and QHBoxLayout.

  • @SGaist
    I tried to create a new widget class based on MainWindow, where I can configure all things I need very easy in QtDesigner, and I can easily insert this widget with

    cloudViewer *cv = new cloudViewer;
    ui->tabWidget->addTab(cv, "3D Viewer");

    ... I think, this is the simplest way for my needs, or is there somthing wrong with this solution in you opinion?

  • Lifetime Qt Champion

    I don't see anything particularly wrong here.

Log in to reply

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