Qt Signal Slot Architecture Unwanted Endless Loop

I have a problem with qt signal slot system.

First, I created a class called System in Singleton pattern, so I can access it whenever I want. The system has a SelectionChanged signal.

I have a list widget and I connect its itemSelectionChanged to my custom slot called onSelectionChanged. In the onSelectionChanged slot, I emit a System SelectionChanged signal. There are no problems so far.

In my software, object selection can be used by many GUI widgets or custom classes, and the System SelectionChanged signal can be emitted by widgets other than the list widget.

So, I create a slot called OnSystemSelectionChanged in the list widgets, and then connect it to the System SelectionChanged signal. OnSystemSelectionChangedSlot is as follows.

void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
    if (sender == this) return;
    // Then I want to get a list of selected objects and set them as selection of this widget like this:
    this->SetSelection(System::Instance()->GetSelectedObjects());
}

But the problem is that when I start setting the selected elements of the list widget, it will generate an itemSelectionChanged signal and my onSelectionChanged slot will be called. The slot then emits a System SelectionChanged signal, and then OnSystemSelectionChanged is called. It will stop by the sender parameter, but there is no way to immediately select the selected list items.

How can I solve this problem.

Hope I explained my problem well. Thanks in advance.

Edit: Fixed spelling and grammar errors.

+4
source share
4

Qt .

  • . , . QDataWidgetMapper, " " . , -. - .

  • DisplayRole, EditRole. (, EditRole), , (, DisplayRole). dataChanged , setData . .

  • , QAbstractItemView s, : , - /. , QAbstractButton, : toggled(bool) , clicked() - . .

    , . , . , dialog/window , , .

Hackish Let's-Hope-They-Won't-Become Idioms

  • ( ).

  • / ( ).

  • QObject::blockSignals().

+7

, :

  • , :

 

void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
    if (sender == this || inhibitSelectionChanged)
        return;

    this->inhibitSelectionChanged = true;
    this->SetSelection(System::Instance()->GetSelectedObjects());
    this->inhibitSelectionChanged = false;
}

 

void MyListWidget::OnSystemSelectionChanged(QObject *sender)
{
    if (sender == this)
        return;

    this->disconnect(SIGNAL(SelectionChanged()));

    this->SetSelection(System::Instance()->GetSelectedObjects());

    this->connect(
        this, SIGNAL(SelectionChanged()), 
        this, SLOT(OnSystemSelectionChanged(QObject*)));
}
+2

QObject:: blockSignals(). , .

, BartoszKP. .

+2

: . singleton.

, , .

:

  • No singleton signal.
  • Each object has its own signal and slot for the corresponding event (for example, a change of choice).
  • An application or an object of a higher level (which created widgets / objects) makes a connection to the channel. If these widgets are listed, it’s very simple.
+1
source

All Articles