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. C++ Qt Widget app: catch mouse signals from a QPixmap?
Forum Updated to NodeBB v4.3 + New Features

C++ Qt Widget app: catch mouse signals from a QPixmap?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 232 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by Tom asso
    #1

    I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?

    I tried this approach:

    MainWindow.h:
    
    protected:
      QPixmap pixmap_;
    
    private slots:
      void mySlot(QMouseEvent *event);
    
    MainWindow.cpp:
    
    pixmap_ = 
       new QPixmap(width, height);
    
    // Connect QPixmap mouse press signal 
    // to MainWindow slot
      connect(pixmap_, 
              some-signal-here?
              this,
              &MainWindow::mySlot);
    
    

    What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?

    Thanks!

    C Pl45m4P 2 Replies Last reply
    0
    • Tom assoT Tom asso

      I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?

      I tried this approach:

      MainWindow.h:
      
      protected:
        QPixmap pixmap_;
      
      private slots:
        void mySlot(QMouseEvent *event);
      
      MainWindow.cpp:
      
      pixmap_ = 
         new QPixmap(width, height);
      
      // Connect QPixmap mouse press signal 
      // to MainWindow slot
        connect(pixmap_, 
                some-signal-here?
                this,
                &MainWindow::mySlot);
      
      

      What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?

      Thanks!

      Pl45m4P Online
      Pl45m4P Online
      Pl45m4
      wrote on last edited by Pl45m4
      #3

      @Tom-asso

      Search for "Clickable Label" or something.

      There is even an entry on Qt Wiki for that:

      • https://wiki.qt.io/Clickable_QLabel

      Then you just have to use QLabel::setPixmap(yourPixmap) to assign the image to it.

      Edit:

      Usually there is no need to "new" a QPixmap. Using a stack variable is sufficient most of the time.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      4
      • Tom assoT Tom asso

        I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?

        I tried this approach:

        MainWindow.h:
        
        protected:
          QPixmap pixmap_;
        
        private slots:
          void mySlot(QMouseEvent *event);
        
        MainWindow.cpp:
        
        pixmap_ = 
           new QPixmap(width, height);
        
        // Connect QPixmap mouse press signal 
        // to MainWindow slot
          connect(pixmap_, 
                  some-signal-here?
                  this,
                  &MainWindow::mySlot);
        
        

        What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?

        Thanks!

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        @Tom-asso A QPixmap is an in-memory structure holding an image, not something that a user can click on. If you want a user to click on a displayed version of this image then you need to put it on a widget that a user can interact with (e.g. QLabel). The widget can handle mouse click events, map that to a point in the image, and emit a signal carrying that information.

        1 Reply Last reply
        4
        • Tom assoT Tom asso

          I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?

          I tried this approach:

          MainWindow.h:
          
          protected:
            QPixmap pixmap_;
          
          private slots:
            void mySlot(QMouseEvent *event);
          
          MainWindow.cpp:
          
          pixmap_ = 
             new QPixmap(width, height);
          
          // Connect QPixmap mouse press signal 
          // to MainWindow slot
            connect(pixmap_, 
                    some-signal-here?
                    this,
                    &MainWindow::mySlot);
          
          

          What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?

          Thanks!

          Pl45m4P Online
          Pl45m4P Online
          Pl45m4
          wrote on last edited by Pl45m4
          #3

          @Tom-asso

          Search for "Clickable Label" or something.

          There is even an entry on Qt Wiki for that:

          • https://wiki.qt.io/Clickable_QLabel

          Then you just have to use QLabel::setPixmap(yourPixmap) to assign the image to it.

          Edit:

          Usually there is no need to "new" a QPixmap. Using a stack variable is sufficient most of the time.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          4
          • Tom assoT Tom asso has marked this topic as solved on

          • Login

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