Results 1 to 3 of 3

Thread: automate QCompleter selection

  1. #1
    Join Date
    Dec 2010
    Location
    Ottawa, On, Canada
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default automate QCompleter selection

    Good afternoon,

    We have implemented a test-mode for the application we have developed. Essentially, when the test mode is selected, a separate thread is launched and we are using QMetaObject::invokeMethod( ...) on the different controls to simulate user interaction, entries, and so on. The SW also uses QStateMachine framework.

    A recent change includes the use of QCompleter on a QLineEdit, and QState transitions when the QLineEdit is modified and a selection is made is based on QSignalTransition as shown here

    Qt Code:
    1. class AutoCompleterTransition : public QSignalTransition
    2. {
    3. public:
    4. AutoCompleterTransition( AcquisitionWindow* acquisitionWindow, QCompleter* completer)
    5. : QSignalTransition( completer, SIGNAL( activated( const QModelIndex&))),
    6. m_acquisitionWindow( acquisitionWindow),
    7. m_completer( completer)
    8. {
    9. }
    10.  
    11. virtual bool eventTest( QEvent *e)
    12. {
    13. if (!QSignalTransition::eventTest( e))
    14. return false;
    15. QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
    16. QModelIndex modelIndex = se->arguments().at( 0).value<QModelIndex>();
    17. return( modelIndex.isValid());
    18. }
    19.  
    20. virtual void onTransition( QEvent *e)
    21. {
    22. QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
    23. QModelIndex modelIndex = se->arguments().at( 0).value<QModelIndex>();
    24. m_acquisitionWindow->insertCompletion( modelIndex);
    25. qDebug( "%s: model index: row: %d, col: %d",
    26. __FUNC_NAME__, modelIndex.row(), modelIndex.column());
    27. }
    28.  
    29. private:
    30. AcquisitionWindow* m_acquisitionWindow;
    31. QCompleter* m_complete
    32. };
    To copy to clipboard, switch view to plain text mode 

    Where the completer is instantiated and initialized as follow:
    Qt Code:
    1. selectCompleter = new QCompleter( this);
    2. model = new QStandardItemModel( selectCompleter);
    3.  
    4. selectCompleter->setCompletionMode( QCompleter::PopupCompletion);
    5. selectCompleter->setModel( model);
    6. selectCompleter->setModelSorting( QCompleter::CaseInsensitivelySortedModel);
    7. selectCompleter->setCaseSensitivity( Qt::CaseInsensitive);
    8. selectCompleter->setWrapAround( true);
    9.  
    10. QTreeView* popupView = new QTreeView;
    11. selectCompleter->setPopup( popupView);
    12.  
    13. popupView->setSelectionMode( QAbstractItemView::SingleSelection);
    14. popupView->setSelectionBehavior( QAbstractItemView::SelectRows);
    15. popupView->setEditTriggers( QAbstractItemView::NoEditTriggers);
    16. popupView->setAlternatingRowColors( true);
    17.  
    18. popupView->setRootIsDecorated(false);
    19. popupView->header()->hide();
    20. popupView->header()->setStretchLastSection( false);
    To copy to clipboard, switch view to plain text mode 
    This QState transition works well when running in 'normal' mode, but in 'test mode', I cannot trigger the transition. I have tried different approaches; changing the setCompletionMode to QCompleter::InlineCompletion; using QCompleter::setCurrentIndex( 0) to select the first entry in the popup for instance without success. Note that after call QCompleter::setCurrentIndex( 0), QModelIndex modelIndex = completer->currentIndex() reports the correct selection.

    How can I then simulate the selection in the popup?

    Thanks in advance
    Daniel

  2. #2
    Join Date
    Dec 2010
    Location
    Ottawa, On, Canada
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: automate QCompleter selection

    Hi again,

    I guess another way to look at this problem is 'how to programmatically make a selection in a QTreeView (that is the pop-up view used in this example) using QMetaObject::invokeMethod( ...)?"

    My investigation continues and I will report back if I find a solution. Meanwhile, your help is much appreciated.

    Daniel

  3. #3
    Join Date
    Dec 2010
    Location
    Ottawa, On, Canada
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Solved: automate QCompleter selection

    Here is the code used to automate the selection from the QCompleter popup (using QTreeView)

    Qt Code:
    1. QCompleter* completer = patientInputWidget->txtLastName->completer();
    2. if (completer) {
    3. // The QCompleter has been set, force the selection of the first entry
    4. completer->setCurrentRow( 0);
    5. QModelIndex modelIndex = completer->currentIndex();
    6. QMetaObject::invokeMethod( completer, "activated", Qt::BlockingQueuedConnection, Q_ARG( QModelIndex, modelIndex));
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    This code is executed on a different thread than the GUI thread.

Similar Threads

  1. Automate Mousse click event
    By kuppan-j in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2013, 08:29
  2. QCompleter Help
    By shinegun in forum Qt Programming
    Replies: 0
    Last Post: 2nd September 2010, 13:26
  3. Automate Response To Questions During Configure
    By NoYouDidnt in forum Installation and Deployment
    Replies: 0
    Last Post: 27th July 2010, 16:03
  4. Replies: 1
    Last Post: 12th October 2008, 08:21
  5. PostgreSQL and QT4 automate id problem!
    By nnidza in forum Qt Programming
    Replies: 9
    Last Post: 30th December 2006, 23:16

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.