Results 1 to 10 of 10

Thread: form and its objects communication

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default form and its objects communication

    Hi all,

    This may or may not be a qt question but I have no idea where else I could place this. I apologise if this offended some of you.

    form items:
    I have 1 form (call f1).
    f1 contains a custom graphicsScene (with modified mouse press event) call scene for simplicity.
    f1 also contain 2 labels (x, y) and a list box;

    part1:
    I want only to enable mouse press event (of scene) only when an item in the list box is selected.

    part2:
    I wanted to do is when I clicked on the scene, I want to return the x and y position of the mouse (of scene) to the 2 labels in f1.

    How can I do both of these things?

  2. #2
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: form and its objects communication

    Hi

    Go thru event filter& sending events, u can do both these things using that

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: form and its objects communication

    I wouldn't "disable" mouse events in the scene. I would just gave a flag set in the scene according to the state of the list box. Then if the flag is set (something is selected) I would do the main thing the event is supposed to do, otherwise I would just emit a signal with mouse click position so that you can connect to it and set up the labels.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: form and its objects communication

    Quote Originally Posted by wysota View Post
    I wouldn't "disable" mouse events in the scene. I would just gave a flag set in the scene according to the state of the list box. Then if the flag is set (something is selected) I would do the main thing the event is supposed to do, otherwise I would just emit a signal with mouse click position so that you can connect to it and set up the labels.
    I would also do it that way but I have no idea how to work it out in qt. My problem is I don't know how to communication between the 2 widgets. I am still new to qt.

    below are codes I did but got stuck halfway.

    Mainwindow.h
    Qt Code:
    1. ....
    2. private:
    3. QLabel *posX;
    4. QLabel *posY;
    5. QListWidget *area;
    6. mapscene *scene;
    To copy to clipboard, switch view to plain text mode 

    Mainwindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. posX = new QLabel;
    5. posX->setMinimumWidth(50);
    6. posX->setFrameShape(QFrame::Box);
    7.  
    8. posY = new QLabel;
    9. posY->setMinimumWidth(50);
    10. posY->setFrameShape(QFrame::Box);
    11.  
    12. area = new QListWidget;
    13. area->setMaximumWidth(180);
    14. area->setMinimumHeight(300);
    15. //assume all items added
    16.  
    17. scene = new mapscene;
    18. scene->setSceneRect(0, 0, 600, 600);
    19. }
    To copy to clipboard, switch view to plain text mode 

    mapscene.h
    Qt Code:
    1. class mapscene : public QGraphicsScene
    2. {
    3. public:
    4. mapscene();
    5.  
    6. protected:
    7. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    8. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    9.  
    10. private:
    11. };
    To copy to clipboard, switch view to plain text mode 

    mapscene.cpp
    Qt Code:
    1. const int ellsize = 5;
    2.  
    3. mapscene::mapscene()
    4. {
    5. item->setBrush(Qt::red);
    6. addItem(item);
    7. }
    8.  
    9. void mapscene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    10. {
    11. //check status of listbox. How to check?
    12.  
    13. removeItem(item);
    14.  
    15. item->setRect(mouseEvent->scenePos().x(),mouseEvent->scenePos().y(),ellsize, ellsize);
    16. addItem(item);
    17.  
    18. //update posX and posY (Qlabel of Mainwindow). How to update?
    19.  
    20. QGraphicsScene::mousePressEvent(mouseEvent);
    21. }
    22.  
    23. void mapscene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    24. {
    25. QGraphicsScene::mouseReleaseEvent(mouseEvent);
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: form and its objects communication

    Quote Originally Posted by vishwajeet.dusane View Post
    Hi

    Go thru event filter& sending events, u can do both these things using that
    I looked through the events & events filter help but no sure how to use it in my case. there is part of my implementation in my previous post. Maybe someone can guide me through there.

  6. #6
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: form and its objects communication

    Hi I manage to update the label box but I still wasn't able to get the status of the list box.

    current code.

    Qt Code:
    1. class mapscene : public QGraphicsScene
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. mapscene();
    7.  
    8. signals:
    9. void map_plot(QPointF pt);
    10.  
    11. protected:
    12. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    13. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    14.  
    15. private:
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. mapscene::mapscene()
    2. {
    3. item->setBrush(Qt::red);
    4. addItem(item);
    5. }
    6.  
    7. void mapscene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    8. {
    9. //check status of list box. How to do it?
    10.  
    11. removeItem(item);
    12. item->setRect(mouseEvent->scenePos().x(),mouseEvent->scenePos().y(),ellsize, ellsize);
    13. addItem(item);
    14.  
    15. QGraphicsScene::mousePressEvent(mouseEvent);
    16. emit map_plot(mouseEvent->scenePos());
    17. }
    18.  
    19. void mapscene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    20. {
    21. QGraphicsScene::mouseReleaseEvent(mouseEvent);
    22. }
    To copy to clipboard, switch view to plain text mode 

    My mainwindow still stays the same as the above except I added a function to update the labels and connect it to the signal emitted by the mousepressevent.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: form and its objects communication

    Don't check the status of the listbox in mouse events of another widget. Use signals and slots to connect to changes in the listbox's state (i.e. signals emitted by its QItemSelectionModel) to set a flag in your scene that the listbox has an item selected or not. Then just check this flag in the event. Same goes for updating the status bar - emit a signal from your scene with the new coordinates and connect it to a slot in your widget where you will update the status bar.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: form and its objects communication

    I get it that I can use signals and slots to do the trick. The problem here is disp can see the mapscene (custom graphicsScene) object and therefore it can use the signals produce by that object. I have use this to update the position of the mouse.

    However, since no reference of disp in mapscene, I can't see the signals emited by disp and hence wasn't able to reference to the signal from disp to notify mapscene that an item in the listbox is selected. This is the problem I am facing now.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: form and its objects communication

    What is "disp"? If that's an object of some kind then the answer is that you need to connect signals and slots from an object that sees both the source and target objects (which is most often the first common ancestor object of the two objects in question). If there is no such object then you can always relay (propagate) signals to the environment until a situation I described occurs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: form and its objects communication

    I manage to get it through by using pointers. Thanks for your help.

Similar Threads

  1. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 18:19
  2. Replies: 3
    Last Post: 9th January 2010, 15:47
  3. Help me to load one form over another form PushButton
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2008, 16:11
  4. Hiding a form and opening another form
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 20th February 2008, 15:04
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.