Results 1 to 4 of 4

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

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default 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.

    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QtWidgets>
    4. class widget:public QMainWindow
    5. {public:
    6. widget();
    7. void event(QContextMenuEvent*e);
    8. void createmenu(QMenu*filemenu);
    9. void createaction();
    10. };
    11. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QContextMenuEvent>
    2. #include <QtWidgets>
    3. #include "widget.h"
    4. widget::widget()
    5. {
    6. resize(250,250);
    7. }
    8. void widget::event(QContextMenuEvent*e)
    9. {Q_UNUSED(e);
    10. QMenu filemenu(this);
    11. createmenu(&filemenu);
    12. }
    13. void widget:: createmenu(QMenu*file)
    14. {
    15. file =menuBar()->addMenu("File");
    16. createaction();
    17. }
    18. void widget:: createaction()
    19. {
    20. QAction newact("Open",this);
    21. QKeySequence key(tr("Ctrl+O"));
    22. newact.setShortcut(key);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "widget.h"
    2. #include <QtWidgets>
    3. int main(int argv,char*argc[])
    4. {
    5. QApplication app(argv,argc);
    6. widget window;
    7. window.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I want to build one menu and one action with QContextMenuEvent,but it doesn't bui

    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.

Similar Threads

  1. Replies: 0
    Last Post: 13th December 2013, 20:39
  2. Replies: 4
    Last Post: 6th March 2012, 01:27
  3. Replies: 2
    Last Post: 4th October 2011, 00:04
  4. Replies: 0
    Last Post: 14th July 2011, 08:42
  5. Replies: 2
    Last Post: 20th April 2010, 22:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.