Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25423 > unrolled thread

PyQt QCalendarWidget events question

Started bytinnews@isbd.co.uk
First post2012-07-16 16:51 +0100
Last post2012-07-18 03:41 +1000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  PyQt QCalendarWidget events question tinnews@isbd.co.uk - 2012-07-16 16:51 +0100
    Re: PyQt QCalendarWidget events question tinnews@isbd.co.uk - 2012-07-16 17:28 +0100
      Re: Re: PyQt QCalendarWidget events question John Posner <jjposner@optimum.net> - 2012-07-16 15:58 -0400
        Re: PyQt QCalendarWidget events question tinnews@isbd.co.uk - 2012-07-17 10:34 +0100
          Re: PyQt QCalendarWidget events question Chris Angelico <rosuav@gmail.com> - 2012-07-18 03:41 +1000

#25423 — PyQt QCalendarWidget events question

Fromtinnews@isbd.co.uk
Date2012-07-16 16:51 +0100
SubjectPyQt QCalendarWidget events question
Message-ID<bspdd9-22s.ln1@chris.zbmc.eu>
I am trying to use the PyQt4 calendar widget to perform some different
actions on specific dates.  There are three events available:-

    selectionChanged()
    activated(QDate)
    clicked(QDate)

On trying all these out it would appear that the event handlers get
called as follows:-

    The clicked(QDate) event gets called if you click on an already
    selected date.

    The selectionChanged() and then the clicked(QDate) events are
    called when you click on a new date.

    The selectionChanged(), then the clicked(QDate) and then the
    activated(QDate) events are called if you double-click on a new date.

    The clicked(QDate) and then the activated(QDate) events are called
    if you double-click on an already selected date.


How can I get a single-click on a date to get 'Action1' and double-click
on a date to get 'Action2'?

-- 
Chris Green

[toc] | [next] | [standalone]


#25424

Fromtinnews@isbd.co.uk
Date2012-07-16 17:28 +0100
Message-ID<m2sdd9-s2t.ln1@chris.zbmc.eu>
In reply to#25423
tinnews@isbd.co.uk wrote:
> I am trying to use the PyQt4 calendar widget to perform some different
> actions on specific dates.  There are three events available:-
> 
>     selectionChanged()
>     activated(QDate)
>     clicked(QDate)
> 
> On trying all these out it would appear that the event handlers get
> called as follows:-
> 
>     The clicked(QDate) event gets called if you click on an already
>     selected date.
> 
>     The selectionChanged() and then the clicked(QDate) events are
>     called when you click on a new date.
> 
>     The selectionChanged(), then the clicked(QDate) and then the
>     activated(QDate) events are called if you double-click on a new date.
> 
>     The clicked(QDate) and then the activated(QDate) events are called
>     if you double-click on an already selected date.
> 
> 
> How can I get a single-click on a date to get 'Action1' and double-click
> on a date to get 'Action2'?

I'm sorry, this got sent a bit before I'd completed it.  The trouble
is that I want to run Action1 if I single-click on a date whether or
not it's a changed date and I want to run Action2 if I double-click on
a date whether or not it's a changed date.  However I don't see how I
can do this because of the order in which the event handlers are
called.

Is there any way to manipulate this so I can get the result I want? 
At the moment the only way I can see to do it is to wait a while after
a click and then look at what actions occurred but this seems a real
bodge.

-- 
Chris Green

[toc] | [prev] | [next] | [standalone]


#25436

FromJohn Posner <jjposner@optimum.net>
Date2012-07-16 15:58 -0400
Message-ID<mailman.2186.1342470497.4697.python-list@python.org>
In reply to#25424
On 7/16/2012 12:28 PM, tinnews@isbd.co.uk wrote:
> tinnews@isbd.co.uk wrote:
>> I am trying to use the PyQt4 calendar widget to perform some different
>> actions on specific dates.  There are three events available:-
>>
>>     selectionChanged()
>>     activated(QDate)
>>     clicked(QDate)
>>
>> On trying all these out it would appear that the event handlers get
>> called as follows:-
>>
>>     The clicked(QDate) event gets called if you click on an already
>>     selected date.
>>
>>     The selectionChanged() and then the clicked(QDate) events are
>>     called when you click on a new date.
>>
>>     The selectionChanged(), then the clicked(QDate) and then the
>>     activated(QDate) events are called if you double-click on a new date.
>>
>>     The clicked(QDate) and then the activated(QDate) events are called
>>     if you double-click on an already selected date.
>>
>>
>> How can I get a single-click on a date to get 'Action1' and double-click
>> on a date to get 'Action2'?
> I'm sorry, this got sent a bit before I'd completed it.  The trouble
> is that I want to run Action1 if I single-click on a date whether or
> not it's a changed date and I want to run Action2 if I double-click on
> a date whether or not it's a changed date.  However I don't see how I
> can do this because of the order in which the event handlers are
> called.
>
> Is there any way to manipulate this so I can get the result I want? 
> At the moment the only way I can see to do it is to wait a while after
> a click and then look at what actions occurred but this seems a real
> bodge.

I suspect that the consensus would be "don't do that" -- having
single-click and double click perform unrelated actions. But here's an
implementation based on the advice at
http://www.qtcentre.org/threads/7858-Double-Click-Capturing

(use Ctrl-Break to break out of the event loop)

import PyQt4.QtCore as C
import PyQt4.QtGui as G

class Button(G.QPushButton):
    def __init__(self, text):
        G.QPushButton.__init__(self, text)

        # flag to suppress second mouseReleaseEvent
        # in this double-click event sequence:
        # 1. mousePressEvent
        # 2. mouseReleaseEvent
        # 3. mouseDoubleClickEvent
        # 4. mouseReleaseEvent
        self.double_clicked = False

    def mouseReleaseEvent(self, evt):
        # executed for first mouseReleaseEvent
        if not self.double_clicked:
            self.first_click_timer = C.QTimer()
            self.first_click_timer.setSingleShot(True)
            # double-click must occur within 1/4 second of first-click
release
            self.first_click_timer.setInterval(250)
            self.first_click_timer.timeout.connect(self.single_click_action)
            self.first_click_timer.start()
        # executed for second mouseReleaseEvent
        else:
            # reset the flag
            self.double_clicked = False

    def single_click_action(self):
        print "Performing single-click action"

    def mouseDoubleClickEvent(self, evt):
        # suppress the single-click action; perform double-click action
instead
        self.first_click_timer.stop()
        print "Performing double-click action"

        # prepare for second mouseReleaseEvent
        self.double_clicked = True

# main program

app = G.QApplication([])
button = Button("Click or double-click me")
button.show()
app.exec_()


HTH,
John

[toc] | [prev] | [next] | [standalone]


#25478

Fromtinnews@isbd.co.uk
Date2012-07-17 10:34 +0100
Message-ID<q5ofd9-id7.ln1@chris.zbmc.eu>
In reply to#25436
John Posner <jjposner@optimum.net> wrote:
> On 7/16/2012 12:28 PM, tinnews@isbd.co.uk wrote:
> > tinnews@isbd.co.uk wrote:
> >> I am trying to use the PyQt4 calendar widget to perform some different
> >> actions on specific dates.  There are three events available:-
> >>
> >>     selectionChanged()
> >>     activated(QDate)
> >>     clicked(QDate)
> >>
> >> On trying all these out it would appear that the event handlers get
> >> called as follows:-
> >>
> >>     The clicked(QDate) event gets called if you click on an already
> >>     selected date.
> >>
> >>     The selectionChanged() and then the clicked(QDate) events are
> >>     called when you click on a new date.
> >>
> >>     The selectionChanged(), then the clicked(QDate) and then the
> >>     activated(QDate) events are called if you double-click on a new date.
> >>
> >>     The clicked(QDate) and then the activated(QDate) events are called
> >>     if you double-click on an already selected date.
> >>
> >>
> >> How can I get a single-click on a date to get 'Action1' and double-click
> >> on a date to get 'Action2'?
> > I'm sorry, this got sent a bit before I'd completed it.  The trouble
> > is that I want to run Action1 if I single-click on a date whether or
> > not it's a changed date and I want to run Action2 if I double-click on
> > a date whether or not it's a changed date.  However I don't see how I
> > can do this because of the order in which the event handlers are
> > called.
> >
> > Is there any way to manipulate this so I can get the result I want? 
> > At the moment the only way I can see to do it is to wait a while after
> > a click and then look at what actions occurred but this seems a real
> > bodge.
> 
> I suspect that the consensus would be "don't do that" -- having
> single-click and double click perform unrelated actions.

They're not actually unrelated, one will edit an existing entry in a
file for the given date, the other will add a new entry in the file
for that date.  I.e. single-click means open file at specified date,
double-click means open file at specified date and insert a new entry
for that date.


>                                                            But here's an
> implementation based on the advice at
> http://www.qtcentre.org/threads/7858-Double-Click-Capturing
> 
> (use Ctrl-Break to break out of the event loop)
> 
> import PyQt4.QtCore as C
> import PyQt4.QtGui as G
> 
> class Button(G.QPushButton):
>     def __init__(self, text):
>         G.QPushButton.__init__(self, text)
> 
>         # flag to suppress second mouseReleaseEvent
>         # in this double-click event sequence:
>         # 1. mousePressEvent
>         # 2. mouseReleaseEvent
>         # 3. mouseDoubleClickEvent
>         # 4. mouseReleaseEvent
>         self.double_clicked = False
> 
>     def mouseReleaseEvent(self, evt):
>         # executed for first mouseReleaseEvent
>         if not self.double_clicked:
>             self.first_click_timer = C.QTimer()
>             self.first_click_timer.setSingleShot(True)
>             # double-click must occur within 1/4 second of first-click
> release
>             self.first_click_timer.setInterval(250)
>             self.first_click_timer.timeout.connect(self.single_click_action)
>             self.first_click_timer.start()
>         # executed for second mouseReleaseEvent
>         else:
>             # reset the flag
>             self.double_clicked = False
> 
>     def single_click_action(self):
>         print "Performing single-click action"
> 
>     def mouseDoubleClickEvent(self, evt):
>         # suppress the single-click action; perform double-click action
> instead
>         self.first_click_timer.stop()
>         print "Performing double-click action"
> 
>         # prepare for second mouseReleaseEvent
>         self.double_clicked = True
> 
> # main program
> 
> app = G.QApplication([])
> button = Button("Click or double-click me")
> button.show()
> app.exec_()
> 
> 
Yes, thanks, though it is basically the bodge using timing that I was
trying to avoid.  It's so fundamental to most GUIs that single-click
and double-click allow one to do different things with the same object
I'm surprised that pyqt makes it so difficult to implement.

-- 
Chris Green

[toc] | [prev] | [next] | [standalone]


#25519

FromChris Angelico <rosuav@gmail.com>
Date2012-07-18 03:41 +1000
Message-ID<mailman.2232.1342546899.4697.python-list@python.org>
In reply to#25478
On Tue, Jul 17, 2012 at 7:34 PM,  <tinnews@isbd.co.uk> wrote:
> It's so fundamental to most GUIs that single-click
> and double-click allow one to do different things with the same object

Kinda yes, kinda no. Most GUIs and GUI recommendations would either
enforce or strongly suggest that the double-click action incorporate
the click action; for instance, click to select, double-click to open.

The series of events usually goes:

Mouse down
Mouse up
Click
Mouse down
Mouse up
Double-click

They're fired as things happen and in that order. (Some systems insert
another Click just before the Double-click, but that's generally
unhelpful.) Please code your application so that this isn't a problem
to it, because anything else causes untold confusion to the users.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web