
Originally Posted by
moowy
Ok. But how can I add a toolbar to a qdockwidget. QMainWindow has addToolbar method, but QDockWidget doesn't. What do you think?
Just create a new QToolBar and pass the dock widget as parent.
I meant something like this:
{
public:
protected:
private:
}
class MyDockWidget : public QDockWidget
{
public:
MyDockWidget(QWidget* parent = 0);
protected:
void resizeEvent(QResizeEvent* event);
private:
QToolBar* toolBar;
}
To copy to clipboard, switch view to plain text mode
{
// create toolbar widget as child of the dockwidget
// so it will be placed inside the dockwidget
}
{
// set toolbar's geometry so that it covers the whole are above
// the widget which has been set as dock widget's insider
r.setBottom(widget()->geometry().top());
toolBar->setGeometry(r);
}
MyDockWidget::MyDockWidget(QWidget* parent = 0) : QDockWidget(parent)
{
// create toolbar widget as child of the dockwidget
// so it will be placed inside the dockwidget
toolBar = new QToolBar(this);
}
void MyDockWidget::resizeEvent(QResizeEvent* event)
{
QDockWidget::resizeEvent(event);
// set toolbar's geometry so that it covers the whole are above
// the widget which has been set as dock widget's insider
QRect r = geometry;
r.setBottom(widget()->geometry().top());
toolBar->setGeometry(r);
}
To copy to clipboard, switch view to plain text mode
Just be aware that the toolbar widget will naturally "steal" all events which would otherwise go to the dock widget's title area. This includes all mouse events and so on, and will break for example the movement functionality of the dock widget. To avoid this, you would have to filter appropriate toolbar's events and deliver them to the dock widget instead.
Edit: I would rather draw something custom to the title bar area instead of adding another widget there. This would require overriding of QDockWidget::paintEvent(). I have already shown you in earlier threads related to this, how to draw custom things with QStyle.
Bookmarks