Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Chart and Legend
Qt 6.11 is out! See what's new in the release blog

Chart and Legend

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 277 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pixel
    wrote last edited by pixel
    #1

    Hi,

    after i finished learning path "Introduction to Qt Widgets with KDAB" im trying to create own QT Application wit QT Creator.
    Now im stuck in an issue trying to show context menu when user make right click on the chart, my goal is to show a context menu when a user has
    used mouse right click on a Chart Legend, but im not able to find a chart legend position, or how to check what is under my cursor currently,
    for this i used event filter, this part works, but how to catch mouse right click on QLegend, is this possible?

    This is how im try to connect to QLegend:

    connect(m_Chart->legend(), &QLegend::clicked,
                this, &MyChartView::onLegendClicked);
    

    Edit: i saw there is no signal "clicked", but QLegendMarker has one, if my first is not possible to use, can we use for this QLegendMarker's signal "clicked" to catch the right mouse event?

    Thanks for any help

    1 Reply Last reply
    0
    • Jacek Marcin JaworskiJ Offline
      Jacek Marcin JaworskiJ Offline
      Jacek Marcin Jaworski
      wrote last edited by Jacek Marcin Jaworski
      #2

      You have two options:

      1. Inherit QLegend and code your own virtual MyLegend::mousePressEvent(QGraphicsSceneMouseEvent *) function;
      2. Use QLegend::installEventFilter function.
        You have call it in the window constructor with this as param.
        Then in your window you have implement function (this is not compiled and not tested code):
      bool MyWindowWithLegend::eventFilter(QObject* o, QEvent* e)
      {
         if(o != myLegendObject)
            return false; // Event not recognized - propagate it to next widget.
         QMouseEvent* e2 = dynamic_cast<QMouseEvent*>(e);
         if(!e2)
            return false; // Event not recognized - propagate it to next widget.
         if(e2->button() == Qt::RightButton)
         {
            // Your popup code here
            return true; // Event recognized and used properly - do no propagate it to next widget.
         }
         return false; // Event not recognized - propagate it to next widget.
      }
      

      Jacek Marcin Jaworski, Pruszcz Gd., Pomeranian Voivodeship, POLAND🇵🇱, EU 🇪🇺;
      WWW home page (in Polish): <https://energokod.gda.pl>;
      email: <jmj@energokod.gda.pl>;
      tel.: +48-609-170-742, best at: 5:00-5:55 or 16:00-17:25.

      P 1 Reply Last reply
      2
      • Jacek Marcin JaworskiJ Jacek Marcin Jaworski

        You have two options:

        1. Inherit QLegend and code your own virtual MyLegend::mousePressEvent(QGraphicsSceneMouseEvent *) function;
        2. Use QLegend::installEventFilter function.
          You have call it in the window constructor with this as param.
          Then in your window you have implement function (this is not compiled and not tested code):
        bool MyWindowWithLegend::eventFilter(QObject* o, QEvent* e)
        {
           if(o != myLegendObject)
              return false; // Event not recognized - propagate it to next widget.
           QMouseEvent* e2 = dynamic_cast<QMouseEvent*>(e);
           if(!e2)
              return false; // Event not recognized - propagate it to next widget.
           if(e2->button() == Qt::RightButton)
           {
              // Your popup code here
              return true; // Event recognized and used properly - do no propagate it to next widget.
           }
           return false; // Event not recognized - propagate it to next widget.
        }
        
        P Offline
        P Offline
        pixel
        wrote last edited by
        #3

        @Jacek-Marcin-Jaworski
        hi, thank you for your answer, i will try to implement tomorrow.

        Regards
        pixel

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote last edited by
          #4

          The clicked() signal is usually for the left click. However, for QWidgets you can setContextMenuPolicy() to Qt::CustomContextMenu. Then you should connect to customContextMenuRequested(). This signal will even give you the position where to show the context menu.

          1 Reply Last reply
          3
          • P Offline
            P Offline
            pixel
            wrote last edited by
            #5

            Hi guys,
            i was able to solve my issue but i have to say i used help from AI,
            and now i have 2 solutions one is using eventFilter and second one is using customContextMenuRequested.

            Thanks for your suggestions
            best regards
            pixel

            1 Reply Last reply
            0
            • P pixel has marked this topic as solved

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved