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. Creating events instead of signals/slots
QtWS25 Last Chance

Creating events instead of signals/slots

Scheduled Pinned Locked Moved General and Desktop
30 Posts 4 Posters 14.8k Views
  • 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.
  • R Offline
    R Offline
    Revu
    wrote on last edited by
    #1

    I have multiple windows(screens) in my application.I need to activate windows with the respective events that I receive from the controller code.Now,I want to close the previous window while activating the other window.I had used signals and slots mechanism to close a particular window but I want it to be done using events.
    How to do this using events?
    Am using QtCreator with qt4.7 version to design my windows(screens).I read the documents regarding events in Qt Documentation.

    Thanks in Advance.

    Every fall is to raise higher than Before.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      is there a specific reason to use events further than signal/slot?
      But events are also easy:

      Create a QEventSubclass and give it a unique type (>QEvent::User).

      @
      QApplication::postEvent(target, new MyEvent);
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

        Could you explain in a bit more detail why you want to use events instead of signal/slot?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Revu
          wrote on last edited by
          #4

          Yes.I have to wait for an event to activate my window requested by the controller code (at the back end).By using close() when I run my application continuously the previous window are not closed.So the same is done using signal slot mechanism.All my windows are frameless, so I want to pass a event to close my current window and raise my next window when I get the event from the controller.

          Do I have an option to use Signal and slot connection by passing event to a particular window?

          Every fall is to raise higher than Before.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Hi,

            I think, close should do it. If close does not work, perhaps there is a different problem. Can you please provide some code example, where close does not work? Do you do something in the closeEvent of that widget?

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Revu
              wrote on last edited by
              #6

              @Geroif:Hi..Yes close does its job.The problem is that am using static to raise my other window.I want to activate my next window before I close my current window.Always the next window will be event dependent to change to the next screen.
              like I want to navigate between the screens initially from Idle screen to Dial screen.After I dial the number the number and press dial button I should show Progress Screen and finally Connect Screen after the call has been established.

              Every fall is to raise higher than Before.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Revu
                wrote on last edited by
                #7

                Whether I could pass the events between two widgets/windows?Can you tell me how to do it?There is no mush details regarding QCloseEvent in the documentation.

                Every fall is to raise higher than Before.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  In his first reply, Gerolf already told you how to use custom events. -It doesn't matter much if you want to post your own event or a Qt event.- A QCloseEvent is just a subclass of QEvent that does not add any special members or methods.

                  I am not sure if posting your own QCloseEvent is going to work, since the spontaneous flag would be false. AFAIK, there is no public API to manipulate that flag, and I don't know if it would affect the handling of the event.

                  Edit: it turns out it does matter what kind of event you post.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    [quote author="Andre" date="1301564768"]In his first reply, Gerolf already told you how to use custom events. It doesn't matter much if you want to post your own event or a Qt event. A QCloseEvent is just a subclass of QEvent that does not add any special members or methods.
                    [/quote]
                    I thinnk it will not. QCloseEvent is also a reaction on the close() slot (if close is no reaction to a system close event, there is an if in the code for that) :-).

                    Sending a QCloseEvent will NOT close the widget!

                    @
                    ... QWidget::event()
                    {
                    ...
                    case QEvent::Close:
                    closeEvent((QCloseEvent *)event);
                    break;
                    ...
                    }

                    void QWidget::closeEvent(QCloseEvent *event)
                    {
                    event->accept();
                    }
                    @

                    [quote author="Andre" date="1301564768"]
                    I am not sure if posting your own QCloseEvent is going to work, since the spontaneous flag would be false. AFAIK, there is no public API to manipulate that flag, and I don't know if it would affect the handling of the event.
                    [/quote]

                    It's totaly internal, I was trying to use it once... Did not work. Needed it for a virtual key board.

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Revu
                      wrote on last edited by
                      #10

                      Do I have a solution to close a widget from another widget of different classes using events?How could I do that?

                      Every fall is to raise higher than Before.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        As you need the widget pointer for sending events, you can just call close() method also. And it does the job. So why bother about sending events? Dou you need it asynchronous? Use QmetaObject::invokeMethod(...) with the queued invokation.

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Revu
                          wrote on last edited by
                          #12

                          It should be synchronous with the close of the current widget and raise of other widget.

                          Every fall is to raise higher than Before.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            Then call

                            @
                            widget2()->show();
                            widget2()->raise();
                            widget1()->close();
                            @

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              Revu
                              wrote on last edited by
                              #14

                              I had already used it in my program and its working.My question is that can't we do the same by passing events between those widgets.in examples given in qt4,in mainwindows there is a sample program named application in which the QCloseEvent has been used but am not able take that and implement to my requirement.It throws a compile error as
                              @/usr/include/qt4/QtGui/qwidget.h:84: error: forward declaration of ‘struct QCloseEvent’
                              @

                              Help me.

                              Every fall is to raise higher than Before.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on last edited by
                                #15

                                you should add the include for the QCloseEvent, it's only forward declared!

                                @
                                #include <QtGui/QCloseEvent>
                                @

                                It's staed in the error message...

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  Revu
                                  wrote on last edited by
                                  #16

                                  Thank You.That is clear now.How could I pass the event between two different widgets.Suppose if I had implementted the QCloseEvent in MainWindow then how to call that from my SubWindow?

                                  Every fall is to raise higher than Before.

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    giesbert
                                    wrote on last edited by
                                    #17

                                    to forward events to a parent, you can just use event->ignore() which should then send the take care that it is also send to the parent (don't know if that also happens to a CloseEvent).

                                    Nokia Certified Qt Specialist.
                                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      Revu
                                      wrote on last edited by
                                      #18

                                      Well!I have multiple screens.I need to display a particular screen based on the event I pass from my first screen.With signal and slot,I need to write those lines of code in all the files.Suppose if I have some 20 screens then by passing an event from my first screen I should display 10th(or 15th whatever) or close that particular window by having all my control in my first screen.

                                      Every fall is to raise higher than Before.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goetz
                                        wrote on last edited by
                                        #19

                                        Then connect your signals to a slot/some slots in the first screen.

                                        http://www.catb.org/~esr/faqs/smart-questions.html

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          Revu
                                          wrote on last edited by
                                          #20

                                          @Volker:I had done that earlier.How well can I organise these so as to have the control to close my other windows?Just like as we have goto in C.

                                          Every fall is to raise higher than Before.

                                          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