Event handling : Accessing another widget's property [SOLVED]



  • OK, this should be easy.

    I tried to handle drop event onto a QGraphicsView widget. Incoming data dragged from a QTreeView widget. For that, I re-implemented these methods:

    @void QGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
    {
    event.accept();
    }

    void QGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
    {
    event.accept();
    }

    void QGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
    {
    event.accept();
    }

    void QGraphicsView::dropEvent(QDropEvent *event)
    {
    QPixmap pixmap(event->mimedata()->urls()[0].toString().remove(0,8));
    this.scene()->addPixmap(pixmap);
    }@

    This works fine; but how can I change another graphicsview scene within this widget's drop event? That is:

    @void QGraphicsView::dropEvent(QDropEvent *event)
    {
    QPixmap pixmap(event->mimedata()->urls()[0].toString().remove(0,8));
    // I cannot access ui; and cannot access other widgets...:
    ui->anotherview->scene()->addPixmap(pixmap);
    }@


  • Moderators

    Well, does that not work? Are you getting any errors?



  • Access to Mainwindow widgets from QGraphicsView class is forbidden.

    ui->anotherview doesn't work.

    Should I create a subclass of QGraphicsView (that inherits it), then promote my graphicsview widgets to that class and create signals there? Then I can connect to mainwindow slots. That was the only way I could think of.


  • Moderators

    Wait so you have modified source code of Qt itself right now? I don't get your setup :)

    In general, since you are reimplementing events you should subclass. Then there are numerous ways to get to your main window and update the other scene.

    You can change mainWindow into a Singleton and then include it in your subclass (perfectly legal, although I find it ugly).

    You can use meta object system to get to your parent (which probably is your MainWindow), then find the other view using findChildren<>() and set the property using QObject::setProperty().

    Or you can send the signal from your subclass notifying your MainWindow (or anotherView directly) that a change needs to be made.



  • I actually rewrote drag drop events of qgraphics and qscene :) I get "inconsistent dll" warning but it worked fine. I will subclass them right now. I will try the meta object method as the signalling method (which was the only thing i could think of) requires more effort.



  • meta method worked fine man; thanks! btw, how do you accept an answer in this forum ? :)


  • Moderators

    Hey, that's nice to hear! Happy coding!

    In general we just add "[Solved]" to the subject (edit your initial post, then add [Solved] to the title). I'll tag it for you.


Log in to reply
 

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