[QGIS-Developer] Testing a ComboBox with a QCompleter using QTest

Jo winfixit at gmail.com
Sat Feb 29 01:08:14 PST 2020


This is the code as I have it at the moment:

        cb = self.form.dockwidget.widget['sc_510_002']
        QTest.mouseClick(cb.lineEdit(), Qt.LeftButton)
        QTest.keyClicks(cb.lineEdit(), '9000')
        QTest.qWait(1000)
        completer_popup = cb.completer().popup()
        indx = completer_popup.model().index(0, 0)
        print(indx.data())
        indx_center = completer_popup.visualRect(indx).center()
        QTest.mouseMove(completer_popup, pos=indx_center, delay=250)

I had to change the completer to  cb.completer()

Jo


On Sat, Feb 29, 2020 at 9:58 AM Jo <winfixit at gmail.com> wrote:

> Hi Larry,
>
> Thank you so much for that! It all works as you describe, except that my
> QGIS doesn't show that completer where one would expect it (under the
> combobox), but rather at position 0,0 of the widget it is in. So that's
> odd. The mouse pointer is moved to the middle of the displaced completer
> widget, so your code works fine.
>
> The other thing that doesn't work as expected, is that the mouse click
> doesn't transfer the entry to the combobox.
>
> When I don't let the script do the mouseclick and I do it myself, it also
> doesn't work. (not executing the last mouseclick of your script and then
> doing it manually)
>
> When I do arrow up or page up and then enter, it also doesn't work.
>
> When I start the whole test manually, the popup appears where one would
> expect it and it's possible to type in the combobox's lineEdit and then
> mouseclick to select the value from the completer.
>
> Normal:
>
> [image: image.png]
>
> This is what happens when run from the script:
>
> [image: image.png]
> So that's weird, as if it didn't know what combobox it belongs to. The
> mousepointer is at the correct position though. Clicking it manually has no
> effect either.
>
> Jo
>
> On Fri, Feb 28, 2020 at 10:17 PM Larry Shaffer <larrys at dakotacarto.com>
> wrote:
>
>> Hello Jo,
>>
>> Try this:
>>
>> QTest.mouseClick(cb.lineEdit(), Qt.LeftButton)
>> QTest.keyClicks(cb.lineEdit(), '9000')
>> QTest.qWait(1000)
>> lv = completer.popup()
>> indx = lv.model().index(0, 0)
>> print(indx.data())
>> indx_center = lv.visualRect(indx).center()
>> QTest.mouseMove(
>>     lv,
>>     pos=indx_center,
>>     delay=250)
>> QTest.mouseClick(
>>     lv.viewport(),
>>     Qt.LeftButton,
>>     pos=indx_center,
>>     delay=250)
>>
>> Summary:
>>
>> - QTest.mouseClick is sent to the combobox's QLineEdit, to set focus
>> - QTest.keyClicks should trigger your completer's list view
>> - QTest.qWait waits for the completer to show up (if long list, etc.)
>> - Instead of searching for QListView of QComboBox, ask for the
>> completer's popup()
>> - Get the first index of the list view's model
>> - Print the index's data (text) to Python Console to verify it is the
>> right index for test
>> - Find index's visual center, in global coordinates
>> - Move the mouse there, so you can visually verify where the click is
>> going to occur
>> - Finally, click to auto-fill line edit with item from completer's list
>>
>> Tested in QGIS 3.10.3 on macOS 10.15.3 under Python 3.8 and Qt 5.12.5.
>>
>> Regards,
>>
>> Larry Shaffer
>> Dakota Cartography
>> Black Hills, South Dakota
>>
>> On Fri, Feb 28, 2020 at 1:51 AM Alessandro Pasotti <apasotti at gmail.com>
>> wrote:
>>
>>> I don't see anything related to QGIS in your issue, you should
>>> probably ask on a Qt/PyQt mailing list.
>>>
>>> On Fri, Feb 28, 2020 at 9:45 AM Jo <winfixit at gmail.com> wrote:
>>> >
>>> > Hi,
>>> >
>>> > I'm really stuck on this.
>>> >
>>> > I'm trying to test a combobox with a completer:
>>> >
>>> > When I do these manually, they work:
>>> >
>>> >         cb = self.form.dockwidget.widget['sc_510_002']
>>> >         QTest.keyClicks(cb, '9000')
>>> >         QTest.qWait(1000)
>>> >         QTest.keyClick(cb, Qt.Key_PageUp, Qt.NoModifier, 500)
>>> >         QTest.keyClick(cb, Qt.Key_Enter, Qt.NoModifier, 500)
>>> >
>>> > When I want to simulate them with the above commands, all I see is the
>>> popup.
>>> >
>>> > I also tried the following:
>>> >
>>> >         cb = self.form.dockwidget.widget['sc_510_002']
>>> >         QTest.keyClicks(cb, '9000')
>>> >         lv = cb.findChild(QListView)
>>> >         QTest.qWait(1000)
>>> >         QTest.mouseClick(lv.viewport(), Qt.LeftButton, 0,
>>> lv.visualRect).center())
>>> >
>>> > Based on what I found here:
>>> >
>>> >
>>> https://gist.github.com/peteristhegreat/cbd8eaa0e565d0b82dbfb5c7fdc61c8d
>>> >
>>> > and here:
>>> >
>>> > https://vicrucann.github.io/tutorials/qttest-signals-qtreewidget/
>>> >
>>> > I was glad I managed to translate it from C, but no joy.
>>> >
>>> > Jo
>>> >
>>> > On Sat, Feb 22, 2020 at 10:55 AM Jo <winfixit at gmail.com> wrote:
>>> >>
>>> >> Hi,
>>> >>
>>> >> I would love to add some testing code to my ever more complex
>>> application.
>>> >>
>>> >> Somehow I'm not succeeding though, these are my attempts:
>>> >>
>>> >> #form.dockwidget.widget['sc_510_002'].setCurrentIndex(5) # , doing it
>>> directly doesn't work
>>> >> QTest.keyClicks(form.dockwidget.widget['sc_510_002'], '9000 Gent',
>>> Qt.NoModifier, 50) # this works, a popup appears
>>> >> QTest.keyClick(form.dockwidget.widget['sc_510_002'], Qt.Key_Enter,
>>> Qt.NoModifier, 500)
>>> >> QTest.keyClick(form.dockwidget.widget['sc_510_002'], Qt.Key_Tab,
>>> Qt.NoModifier, 500)
>>> >> #QTest.keyClick(form.dockwidget.widget['sc_510_002'].completer,
>>> Qt.Key_Down, Qt.NoModifier, 500)
>>> >> #print(dir(form.dockwidget.widget['sc_510_002'].completer().widget()))
>>> >> #form.dockwidget.widget['sc_510_002'].completer().setCurrentRow(0)
>>> >>
>>> #QTest.keyClick(form.dockwidget.widget['sc_510_002'].completer().widget().lineEdit(),
>>> Qt.Key_Enter, Qt.NoModifier, 500)
>>> >> #form.dockwidget.widget['sc_510_002'].setEditText('1000 Brussel')
>>> >>
>>> >> The popup is the completer, I suppose, so I also tried to send
>>> keystrokes to that, but no luck.
>>> >>
>>> >> This is the method I use to add the completer to the QComboBox:
>>> >>
>>> >> def add_completer(self, items):
>>> >>     self.setEditable(True)
>>> >>     self.setInsertPolicy(0)
>>> >>     completer = QCompleter(items, self)
>>> >>     completer.setFilterMode(Qt.MatchContains)
>>> >>     completer.setCaseSensitivity(False)
>>> >>     self.setCompleter(completer)
>>> >>     self.setStyleSheet('QScrollBar: vertical {width: 16px;}')
>>> >
>>> > _______________________________________________
>>> > QGIS-Developer mailing list
>>> > QGIS-Developer at lists.osgeo.org
>>> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>>
>>>
>>> --
>>> Alessandro Pasotti
>>> w3:   www.itopen.it
>>> _______________________________________________
>>> QGIS-Developer mailing list
>>> QGIS-Developer at lists.osgeo.org
>>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>> _______________________________________________
>> QGIS-Developer mailing list
>> QGIS-Developer at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200229/64d50987/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 1755 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200229/64d50987/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 3527 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200229/64d50987/attachment-0001.png>


More information about the QGIS-Developer mailing list