0
votes

I'm writing a plugin for a Qt gui application. I know only the toplevel window QWidget pointer, and I have no particular knowledge of the window's layout(s).

Is there an easy way to add a widget to this window, for exampe occupying the bottom side of the window, like a toolbar?

Perhaps moving all the window content to a new QWidget (a), and create another QWidget (b) with a vbox layout to contain (a) and my new toolbar QWidget. Doesn't sound too easy... perhaps there is a simpler solution?

EDIT: is there a tool like Spy++ which can reveal the Qt widgets layout at runtime?

1
Is the widget you wish to modify/update always a top level widget? - G.M.
@G.M. it is the main window of the application. The application can return the QWidget* of it. - fferri
about Spy++: see GammaRay - CapelliC

1 Answers

0
votes

if you can get a QWidget* you have access to it's .layout() You can add that layout to the top of a QVBoxLayout, and your widget at the bottom.

Something like:

void addWidget() {
  QWidget* mainwindow = getMainWindow();
  auto* newLayout = new QVBoxLayout(mainwindow);
  auto* oldLayout = mainwindow.layout();
  auto* toolbar = getToolbar(mainwindow);

  mainwindow.setLayout(newLayout);

  int stretchMainWindow=1;
  int stretchToolbar=0;
  newLayout->addLayout(oldLayout, stretchMainWindow);
  newLayout->addWidget(toolbar, stretchToolbar);
}

Disclaimer: this is untested