tags:

views:

45

answers:

2

Hello, Qt3 used to provide QMenuBar::insertItem with QWidget* parameter. This way any custom widget could be added to menu bar - for example a clock-widget. In Qt4 there is no such overloaded method. What would be the best way to reach the same goal - adding custom widgets to a menu bar? The custom widgets should be integrated in the layout of menu bar. Does anyoune knows the background, why this overload of insertItem was removed in Qt4 API?

Best regards.

+4  A: 

there's a QMenuBar::addAction ( QAction * action ) method, to add an arbitrary QAction to the menu bar.
For example, it could be a QWidgetAction, which is a subclass of QAction with an associated QWidget instead of just an icon + text.

Javier
Thank you very much for your answer, Javier.
Valentin Heinitz
Unfortunatelly it doesn't work :-)
Valentin Heinitz
@Javier. Ok, now I've got it. It is a mistake, addAction is QWidgets' method and has not to do anything particulary with QMenuBar. Adding QWidgetAction to a QMenuBar is not specified. The action is added, but widget is ignored. However QMenu overloads addAction, and displays QWidgets as menu items. I'm affraid, I have to implement my menu bar and add it to application using setMenuBarWidget.
Valentin Heinitz
A: 

Sorry Javier for short comments. Every time I intended to break a line, the comment was submitted :-(

I tried this code in a project created with QtCreator:

class MyWidgetAction : public QWidgetAction
{
public:
    MyWidgetAction( QObject * parent ) :QWidgetAction (  parent )
    {

    }
    void releaseWidget ( QWidget * widget )
    {
        widget->deleteLater();
    }

    QWidget * requestWidget ( QWidget * parent )
    {
        QPushButton *b = new QPushButton( tr("MyWidget"), parent );
        b->show();
        return b;
    }
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);    
    QAction *a = new QAction(tr("TestAction"),this); //1
    QWidgetAction * wa = new QWidgetAction(this);    //2
    wa->setDefaultWidget(new QPushButton("Default"));
    MyWidgetAction *mwa = new MyWidgetAction(this);  //3

    ui->menuBar->addAction( a ); //1 - works. TestAction added to menu bar
    ui->menuBar->addAction( wa ); //2 - noop. nothing added to menu bar
    ui->menuBar->addAction( mwa ); //3 - noop. nothing added to menu bar
}

Only adding QAction (1) worked. Neither adding QWidgetAction with default widget not subclassing QWidgetAction gave a result. I've set breakpoints in C-Tor and both virtual functions of MyWidgetAction. Surprisingly, only C-Tor break-point was hit. I've tried on Windows with Open-Source, MinGW version of Qt4.6.3 Could it be a bug in Qt? Thank you very much in advance for any suggestions!

Regards, Valentin Heinitz

Valentin Heinitz
You should really add additional information as an edit to your original question but we learn as we go right? :) This solution should work but you should create your widget in an override of createWidget() not requestWidget(). requestWidget() is not virtual.
Arnold Spence
Thank you! Unfortanutelly it still doesn't work for MenuBar. QMenuBar simply doesn't want to accept custom widgets :-( However, it works for sub-menus, e.g. ui->menuFile->addAction( mwa ); works. My Custom button appears in File-menu.
Valentin Heinitz