PDA

View Full Version : I want to build one menu and one action with QContextMenuEvent,but it doesn't build.



rezas1000
18th September 2014, 17:18
Hello, I want to build one menu and one action with QContextMenuEvent,but this code doesn't build.please help me.thank you.



#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
class widget:public QMainWindow
{public:
widget();
void event(QContextMenuEvent*e);
void createmenu(QMenu*filemenu);
void createaction();
};
#endif // WIDGET_H



#include <QContextMenuEvent>
#include <QtWidgets>
#include "widget.h"
widget::widget()
{
resize(250,250);
}
void widget::event(QContextMenuEvent*e)
{Q_UNUSED(e);
QMenu filemenu(this);
createmenu(&filemenu);
}
void widget:: createmenu(QMenu*file)
{
file =menuBar()->addMenu("File");
createaction();
}
void widget:: createaction()
{
QAction newact("Open",this);
QKeySequence key(tr("Ctrl+O"));
newact.setShortcut(key);
}


#include "widget.h"
#include <QtWidgets>
int main(int argv,char*argc[])
{
QApplication app(argv,argc);
widget window;
window.show();
return app.exec();
}

wysota
18th September 2014, 17:37
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.

anda_skoa
18th September 2014, 21:42
If you want a context menu with one action, just use QWidget::addAction() and context menu policy Qt::ActionsContextMenu

Cheers,
_

d_stranz
19th September 2014, 02:44
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.