I want to build one menu and one action with QContextMenuEvent,but it doesn't build.
Hello, I want to build one menu and one action with QContextMenuEvent,but this code doesn't build.please help me.thank you.
Code:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
{public:
widget();
void createmenu
(QMenu*filemenu
);
void createaction();
};
#endif // WIDGET_H
Code:
#include <QContextMenuEvent>
#include <QtWidgets>
#include "widget.h"
widget::widget()
{
resize(250,250);
}
{Q_UNUSED(e);
createmenu(&filemenu);
}
void widget
:: createmenu(QMenu*file) {
file =menuBar()->addMenu("File");
createaction();
}
void widget:: createaction()
{
newact.setShortcut(key);
}
Code:
#include "widget.h"
#include <QtWidgets>
int main(int argv,char*argc[])
{
widget window;
window.show();
return app.exec();
}
Re: I want to build one menu and one action with QContextMenuEvent,but it doesn't bui
There is certainly no event() method that takes a QContextMenuEvent, however that's not the reason why your code doesn't build. If you want to know why your code doesn't build then look at the error the compiler reports to you.
Re: I want to build one menu and one action with QContextMenuEvent,but it doesn't bui
If you want a context menu with one action, just use QWidget::addAction() and context menu policy Qt::ActionsContextMenu
Cheers,
_
Re: I want to build one menu and one action with QContextMenuEvent,but it doesn't bui
Quote:
If you want to know why your code doesn't build then look at the error the compiler reports to you.
And if you do get it to build, you then need to investigate why it doesn't run or simply crashes. Almost every line in the code you have posted has a bug in it. Here are some hints: What is the scope of all those variables you are declaring in your different methods? Why doesn't the assignment to "file" work in createmenu()? What about Q_OBJECT?
Again, no offense intended, but you really need to learn something about C++ and programming before you try developing with Qt. If you don't understand what is wrong with your code, how to understand compiler output, or why programs don't run, then you need to learn some basic concepts first.