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. Getting mouse events on all GLWidgets
Forum Updated to NodeBB v4.3 + New Features

Getting mouse events on all GLWidgets

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.3k 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.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    Hi All,
    I have a mainwindow, and I attached 4 GLWidgets, and I want to listen for mousepress for all these wigets at MainWindow, so that I can add new widgets at runtime. Is that possible?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      Hi, ~AndreAhmed!

      Yes, it's possible. You just need to emit signal from widgets you use and connect it with your slot.

      myglwidget.cpp
      @
      void MyGLWidget::mousePressEvent(QMouseEvent *) {
      emit hadMousePress();
      }
      @

      mainwindow.cpp
      @
      MainWindow::MainWindow(QWidget* parent) :
      //...
      {
      connect(ui->widget1, SIGNAL(hadMouseEvent()), this, SLOT(mySlot()));
      connect(ui->widget2, SIGNAL(hadMouseEvent()), this, SLOT(mySlot()));
      connect(ui->widget3, SIGNAL(hadMouseEvent()), this, SLOT(mySlot()));
      connect(ui->widget4, SIGNAL(hadMouseEvent()), this, SLOT(mySlot()));
      }

      void MainWindow::mySlot()
      {
      qDebug() << "mouse on widget pressed";
      }
      @

      Try to understand what we are doing here. We listen mouse press event and when it is - we send signal which we handle at MainWindow in mySlot()

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AndreAhmed
        wrote on last edited by
        #3

        Thanks, I have used a different way, I added an event filter to all GLWidgets at main window, because all the 4 GLWidgets are of the same object...

        Now I would like on press to "show" the selected widget onto another widget or a horizontal layout...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tucnak
          wrote on last edited by
          #4

          bq. Thanks, I have used a different way, I added an event filter to all GLWidgets at main window, because all the 4 GLWidgets are of the same object…

          I think it will be better to create your own class, such as QAbstractFilteredGLWidget which inherit from QGLWidget and inherit it from all widgets you use. And in this abstract class you should to reimplement mousePressEvent(QMouseEvent*) and add signal to emit from it.

          bq. Now I would like on press to “show” the selected widget onto another widget or a horizontal layout…

          It should be another question. If you don't know how to display widgets on form or in layout, you should read manuals.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndreAhmed
            wrote on last edited by
            #5

            I have posted another topic, would love to see your reply.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tucnak
              wrote on last edited by
              #6

              [quote author="AndreAhmed" date="1341306224"]I have posted another topic, would love to see your reply.[/quote]

              Just now I am busy, but in 20 minutes I will free and will try to answer;)

              1 Reply Last reply
              0

              • Login

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