Important: Please read the Qt Code of Conduct -

QToolbar in QMainWidnow problem

  • Hello my dear friends!

    I want to have QToolbar on main window. He should not be dockable, should be movable and after app start should be in postion 0.0.

    If i add toolbar like this:
    addToolBar(m_pToolBar );

    On start app this is docked but shouldnt

    I try this one:
    m_pToolBar->setAllowedAreas( Qt::NoToolBarArea );
    addToolBar( Qt::ToolBarArea::NoToolBarArea,m_pToolBar );

    Now toolbar disapear. It isnt drawing.

    I force draw by setVisible but it make another window ...

    Any idea?

  • The same problem with QDockWidget

  • Lifetime Qt Champion


    By default the QToolBar is floatable so you should be able to move it around already.

    As for the current code, you are explicitly telling Qt that your m_pToolBar is not allowed to dock

  • QToolBar::QToolBar ( const QString & label, QMainWindow * mainWindow, QWidget * parent, bool newLine = FALSE, const char * name = 0, WFlags f = 0 )

    Constructs an empty horizontal toolbar.
    The toolbar is called name and is a child of parent and is managed by mainWindow. The label and newLine parameters are passed straight to QMainWindow::addDockWindow(). name and the widget flags f are passed on to the QDockWindow constructor.

    Use this constructor if you want to create torn-off (undocked, floating) toolbars or toolbars in the status bar.

    Does above help?

  • Alex:

    This constructor was in QT 3.x. I use QT 5.3.1 and there is not present.

    I looking in source cpp and:
    addToolBar( Qt::ToolBarArea::NoToolBarArea,m_pToolBar );
    addToolBar function doesnt work because of NoToolBarArea parameter. I should use here one of 4 sides... When toolbar is docked and i undock him everything is perfect but i want to default this toolbar will be undock and here is problem.

    If you know some function that will move toolbar from dock to position x,y ( move(x,y) doesnt work ) or i dont know ...

  • Sorry working with multiple qt versions and was tired.

    I am not sure about Qt 5, but on Qt 4
    undocking is equivalent of changing window flags.

    Example below works on my computer.
    you will have to test it on QT 5:

    @ ui.toolBarFiles->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);

    doing this after toolbar is created will show it in undocked state.

  • Wow this is magic... I set this falg in constructor and all was bad... Today I set this flag after add toolabr to MainWindow and work perfect...

    Thanks Alex ;)

  • @poonury I have tried this and its working fine , but when the main window is resized the floating toolbar position is not getting adjusted to new size

Log in to reply