Getting mouse events on all GLWidgets



  • 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?



  • 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()



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



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



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



  • [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;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.