I have some problem to locate my QMenu at runtime.
menu->pos()
is supposed to send his position back, but it sends QPoint(0,0)
. To find its real position I have to open that menu and hover a QAction.
Is there a way to initialize correct menu position without opening manually that menu?
The idea is showing the user where (s)he can find an option, and using exec
or popup
without correct position is not helping…
mainwindow.cpp
(QMAKE_CXXFLAGS += -std=c++11
)
#include "mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QMenuBar * menuBar = new QMenuBar(this);
QMenu * menu = new QMenu("File", this);
menu->addAction("Action0");
menuBar->addMenu(menu);
qDebug() << menu->pos();
connect(menu, &QMenu::hovered, this, [menu] {
qDebug() << menu->pos();
});
setMenuBar(menuBar);
}
MainWindow::~MainWindow()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}