Skip to content

Block recording commands using a "BlockRecordCommands" dynamic property

Steven Pehrson requested to merge sbp/qttesting:record-blocker into master

Adds the ability to block/prevent unwanted events.

The motivation for this is to avoid recording commands that are triggered programmatically. Many translators connect to signals that are fired both when a user interacts with the widget and when methods are called by the programmer. For example the pqAbstractItemViewEventTranslatorBase connects to the currentChanged signal.

Recording programmatic events is problematic because the previous command in the script has already triggered the recorded programmatic changes. This unneessarily complicates the script and also causes the programmatic commands to be executed twice. I also encountered a case where programmatic events were recorded before the user event which created an invalid script.

To use, set the dynamic property "BlockRecordCommands" to a QRegExp that contains the commands you wish to block on the object you wish to block from. To resume recording the commands set it to an invalid QVariant().

ex.

struct RecordCommandsBlocker
{
  static constexpr char kBlockRecordCommands[] = "BlockRecordCommands";

  RecordCommandsBlocker(
    QObject* object,
    QRegExp blockCommandsMatching = QRegExp( "*", Qt::CaseInsensitive, QRegExp::Wildcard ) )
  : mObject( object )
  {
    mObject->setProperty( kBlockRecordCommands, QVariant::fromValue( blockCommandsMatching ) );
  }
  ~RecordCommandsBlocker()
  {
    mObject->setProperty( kBlockRecordCommands, QVariant() );
  }

  QObject* mObject;
};

void Foo::restoreTreeSelectionState( const qModelIndex& index )
{
   const RecordCommandsBlocker raii( mTreeView );
    // Select index
    mTreeView->selectionModel()->setCurrentIndex( index, QItemSelectionModel::ClearAndSelect );

    // Scroll tree to index
    mTreeView->scrollTo( index );

    // Force expanding of the selected index
    mTreeView->expand( index );
}

In the example above restoreTreeSelectionState is called programmatically when restoring the selection state from a save file. Thus we do not want to record any of the events that are triggered in the script because those events will already be triggered when the save file is loaded.

Edited by Steven Pehrson

Merge request reports

Loading