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

Larry Shaffer larrys at dakotacarto.com
Fri Feb 28 13:16:33 PST 2020


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200228/ef097f99/attachment.html>


More information about the QGIS-Developer mailing list