PDA

View Full Version : Is it possible to move main menubar down?



gfunk
29th August 2006, 01:58
I want to place some graphic/banner above the main QMenuBar provided by QMainWindow. Does anyone know a way to do this? I tried setting the x,y geometry in QMenubar and QMainWindow, but it didn't change a thing - it seems the menubar is stuck in the top left-hand corner of the window.

Has anyone ever tried subclassing QMenuBar to do this? Or is there a different way I should go about this... :confused:

wysota
29th August 2006, 02:03
The menu bar is probably positioned using a layout. You can peek at the sources of QMainWindow class to look for the layout and how it is used and then gain access to the layout (for example by calling it by name, if necessary) and insert an additional widget in a place you want it in.

gfunk
29th August 2006, 20:43
Interesting... I tried accessing the QMainWindowLayout object and tried adding buttons to it, but it would just crash. Ick... I wonder if anyone has actually delved into this. the QMainWindowLayout class looks like a monster, 2500 lines... all that docking widget support code...

Alternatively, maybe it would be easier to just re-implement the menubar into a dialog so that it can be embedded elsewhere on the main window. I wonder if QToolButtons with just text inside, placed on a QToolBar, will look sufficiently similar to the menubar...

wysota
29th August 2006, 21:00
Not a very good idea. I would stick to the layout thing. Just try to correct your code, I'm sure it'll work.

gfunk
29th August 2006, 21:10
Well, here's what I quickly tried to come up with: (there's a lot more code that I had to expose in order to get into QMainWindowPrivate - included below)



QMainWindowPrivate* p = reinterpret_cast<QMainWindowPrivate*>(d_ptr);
QLayoutItem *li0 = p->layout->takeAt(0);
QLayoutItem *li1 = p->layout->takeAt(0);
QLayoutItem *li2 = p->layout->takeAt(0);
p->layout->addWidget(new QPushButton);
p->layout->addWidget(li0->widget());
p->layout->addWidget(li1->widget());
p->layout->addWidget(li2->widget());


Naturally it doesn't crash right away; probably when it's drawing. Wondering if this is the right way to go. Seems ugly to have to remove the previous items and then re-add them after my pushbutton (but I suppose it must be done that way since no insertWidget method exists). Just wondering if this is the right way to go...

(ugliness included to get this to compile):


#include "C:/Qt/4.1.4/src/gui/widgets/qmainwindowlayout_p.h"
#include "C:/Qt/4.1.4/src/gui/kernel/qwidget_p.h"
class QMainWindowPrivate : public QWidgetPrivate
{
Q_DECLARE_PUBLIC(QMainWindow)
public:
inline QMainWindowPrivate()
: layout(0), toolButtonStyle(Qt::ToolButtonIconOnly)
{ }
QMainWindowLayout *layout;
QSize iconSize;
bool explicitIconSize;
Qt::ToolButtonStyle toolButtonStyle;
void init();
};

wysota
29th August 2006, 22:23
I would try to reach proper objects by calling them by name. They are all QObject and as such they are accessible through QObject::findChildren().

gfunk
29th August 2006, 23:35
Ah, you're right, I can get the layout much easier... even calling layout() works. But I still can't seem to add anything to that QMainWindowLayout.

wysota
30th August 2006, 00:37
Did you try simply using QMainWindow::setMenuWidget() and passing a complex widget composed of your custom data and a QMenuBar laid out vertically?


+-------------------+
|QWidget w. v-layout|
| +---------------+ |
| | CUSTOM WIDGET | |
| +---------------+ |
| | QMenuBar | |
| +---------------+ |
+-------------------+

Edit: Note that it's available since Qt4.2. In earlier versions you have to stick with mangling with the layout directly.

gfunk
30th August 2006, 01:03
Oh wow! I was using 4.1 and had no idea they added that to 4.2. Thanks, I will have to check that out.

jpn
30th August 2006, 21:35
This should do the trick in Qt 4.1:


menuBar()->setParent(statusBar());

I can't assure it won't break something but it's worth try.. ;)

gfunk
13th September 2006, 00:40
This should do the trick in Qt 4.1:


menuBar()->setParent(statusBar());



Interesting. I tried it and noticed that it could make the menubar take up the window space of any widget that I called setParent() on. Unfortunately, the menubar QAction's can't pop up any dialogs, ending up in crash. Ah well, was definitely worth the try. :cool: