Qt – decide where a Qaction is added to a Qmenubar

qt

I need to add a QAction directly into a QMenuBar (not an QAction inside a QMenu but a QAction directly in the QMenuBar) I am able to do this with the following command.

ui->menuBar->addAction("VFTP",this, SLOT(VFTPmenuTrigger()) );

My only problem is that when I add it, it is appended to the end of the menu bar that I have built in Qt designer. I would like to be able to put it somewhere in the middle. Seems the only way I could do this is if I generate the menu bar by coding it only. Is there a way to build most of my menuBar in Qt Designer and then add that Qaction in the menu bar where I want? I hope I am clear and I added the QAction programmatically because it is not possible to do so with Qt (I know it sounds countintuitive to put a QAction directly in the menu bar but this is what the customer wants)

Best Answer

If you look at QMenuBar code you'll that it does this:

QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
{
    QAction *ret = new QAction(text, this);
    QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
    addAction(ret);
    return ret;
}

which basically uses QWidget::addAction(). So it stands to reason that you can use QWidget::insertAction() yourself to do the same thing. insertAction() lets you specify before what QAction you want to add.

Related Topic