What is the right way to suppress Qt signals when values are explicitely set - qt

I got a from QWidget derived class that holds three QSpinBoxes (e.g. coordinates). The valueChanged() signal is connected and is emitted in at least these three cases:
up/down button
manually entered number
setValue()
However, when using setValue(), I want to suppress the signal(s), since I don't want to have (three) signals. In my understanding, there are two ways to handle this:
QObject::blockSignals()
using a flag that indicates whether the values were explicitely set
Both variants work, but I think they are not straight-forward at all: For the first one, I generally block all signals AND I need to set blockSignals(true) for all underyling widgets (blockSignals doesn't block children QObjects in my application). For the second one, I need to query the flag in every update method AND the signals are raised although I don't need them.
Are there any general designing patterns that prevent such behavior? If not, what variant would you prefer?

The third option would be to subclass QSpinBox, implement desired functionality there, and used derived class instead of QSpinBox - this hides all associated complexity in derived class and allows you use it just like QSpinBox.
For example, following class
myQSpinBox.h
#ifndef MYQSPINBOX_H
#define MYQSPINBOX_H
#include <QSpinBox>
class myQSpinBox : public QSpinBox
{
Q_OBJECT
public:
myQSpinBox(QWidget * parent = 0 );
protected:
bool valueBeingSet;
public slots:
void setValue (int val);
private slots:
void On_valueChanged(int val);
signals:
void valueChangedNotBySet(int val);
};
#endif // MYQSPINBOX_H
myQSpinBox.cpp
#include "myQSpinBox.h"
myQSpinBox::myQSpinBox(QWidget * parent)
: QSpinBox(parent)
, valueBeingSet(false)
{
connect(this,SIGNAL(valueChanged(int)),this,SLOT(On_valueChanged(int)));
}
void myQSpinBox::setValue ( int val )
{
valueBeingSet = true;
QSpinBox::setValue(val);
valueBeingSet = false;
}
void myQSpinBox::On_valueChanged(int val)
{
if(!valueBeingSet)
emit valueChangedNotBySet(val);
}
will emit valueChangedNotBySet(int); in cases 1. and 2., but not in case 3., keeping all QSpinBox functionality intact

Related

If I call a setValue() or similar function of a Qt widget, when is its valueChanged() slot guaranteed to be executed?

Let's say I have multiple input widgets to set up the same parameter. For example, there is a QSlider and a QSpinBox which need to show the same value. In the valueChanged() slot of one of them I call the setValue() of the other one.
Obviously, this would result in an endless loop of them calling each other.
A similar problem arises when this input widget controls some external resource or device. If the user changes the value, it will send the new value to the external device. But if the external device changes the value (or it is read from a settings file, etc) then I have to update the widget, which in turn will send the value, which in turn will update the widget, and so on.
A third scenario is when I save the values into a file or database, but I have to initialize the widgets to some value at the beginning, possibly before I got all the values from the database. But by initializing the widgets at the beginning of my program, they will write that dummy value into the database, overwriting the real values.
The obvious solution for these problems is to just have a bool which allows or forbids the side effects of the valueChanged() functions.
For example, if I want to change the value of my slider, I use
editing = true;
slider.setValue(value);
editing = false;
While I have if (editing) return; at the beginning of my valueChanged() function.
Assuming I didn't fiddle with setting up the signals and slots manually, but they were done by QtCreator, is there a danger of the slot being called later, for example after the editing flag is set to false again? I tried it, and it works, but I am unsure how guaranteed it is.
If you use direct connection (the default for objects in the same thread), the slot is called as soon as the signal is emitted, that is before the setValue method returns.
If you use Qt::QueuedConnection, the slot is invoked when control returns to the event loop of the receiver's thread.
See Qt::ConnectionType
The way I would go about solving this problem is by having another QObject that will be your data model. Your data will be centralized in your model, and will gotten/set via the model. This way your widgets wouldn't need to know about one another and can be created in separate places in your code as long as they can access your model.
Your model will have a method setValue and a signal valueChanged, so it will look some thing like this:
class Model : public QObject {
Q_OBJECT
public:
void setValue(const QVariant& value) {
if (_value != value) {
_value = value;
emit valueChanged(_value);
}
}
const QVariant& getValue() const {
return _value;
}
public signals:
void valueChanged(QVariant& value);
private:
QVariant _value;
}
Then your widgets can take the same instance of Model as a dependency and listen to its valueChanged signal and update themselves. The widgets will also listen to user input, and when the user changes the value then they will change the value in the model. That way the other widgets will get notified about the change.
Your widgets will look like this:
class MySlider : public QSlider {
Q_OBJECT
public:
explicit MySlider(QSharedPointer<Model> model, QWidget *parent=nullptr)
: QSlider(parent), _model(model) {
connect(this, &QSlider::valueChanged, this, [this](int value){
_model->setValue(value);
});
connect(_model.data(), &Model::valueChanged, this, &MySlider::onValueChanged);
//this is to update the widget with the latest value upon creation
onValueChanged(_model->getValue());
}
private slots:
void onValueChanged(const QVariant& value) {
if (value.toInt() != value()) {
//this is calling QSlider::setValue
setValue(value.toInt());
}
}
}
Before you create all your widgets you can create your model with the default value, so let's assume it's in main:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
//note that your model doesn't have a parent, it's a shared pointer
auto model = QSharedPointer<Model>::create();
auto mySlider1 = new MySlider(model, &w);
auto mySlider2 = new MySlider(model, &w);
return a.exec();
}
P.S. You can also look into QDataWidgetMapper and see if it can accomplish what you're looking for.

How to achieve exclusive (e.g. Radio Button) type behavior with QGraphicObject?

I'm working with Qt (v 5.3) again after sometime away. As a learning exercise I am prototyping a module using QGraphicView in order to understand better how to use it.
The functionality is simple: I have several QGraphicObjects which function like buttons with states and behaviors:
OFF - image panel hidden; default button art
ON - image panel
displayed; highlight button art
At startup all buttons are in an OFF state
A clicked button will toggle it's state (non-Exclusive)
If a different button is already in an ON state, it must be turned off (Exclusive)
Everything is data driven and created dynamically at runtime.
So that is my little learning exercise. What I am trying to sort out is an efficient messaging mechanism to handle the "radio button sometimes" exclusive behavior and sending a message to a group of objects without strongly coupling them.
I've looked at signals and slots but that gets tedious if there are many connections to make.
QSignalMapper which seems somewhat better
QEvent is probably the most solid approach but I've had trouble finding good learning examples.
I am making this more complicated than need be but as I say, it's a learning exercise to get used to Qt again.
So my question (finally): is there an approach I am overlooking or one of these mentioned which would be better (e.g most flexible, maintainable, scalable). Not looking for code per se, but an understanding of how to go about coding something like this using the Qt framework.
Here's an image of the thing:
Signal and slots is the method I would use here. However, rather than connect each button to every other button, you can create an intermediate, Controller class.
The Controller class can either aggregate, or be a parent to all of the buttons.
When a button is created, it connects signals to the controller to inform it of being pressed. The receiving slot in the Controller class is then responsible for turning off any other buttons.
Skeleton code: -
class ButtonController : public QGraphicsObject
{
public slots:
void ButtonPressed();
private:
QList<MyButton*> m_buttonList;
};
void ButtonController::ButtonPressed()
{
foreach(MyButton* pButton, m_buttonList)
{
if(pButton != sender())
{
pButton->Off();
}
}
}
In the constructor of MyButton, assuming the controller is the parent: -
MyButton::MyButton(ButtonController* parent)
: QGraphicsObject(parent);
{
connect(this, &MyButton::pressed(), parent, &ButtonController::ButtonPressed());
...
}
Alternatively, the controller may just hold a list of the buttons, in which case you can do this: -
MyButton::MyButton(QObject* parent, ButtonController* pButtonController)
: QGraphicsObject(parent);
{
connect(this, &MyButton::pressed(), pButtonController, &ButtonController::ButtonPressed());
...
}
In the case of not being able to change the constructor of the buttons, the controller may simply have an AddButton function, in which it creates the connection when the button is added under its control.
Subclass QGraphicsScene
Reimplement mousePressEvent and check is left mouse button pressed.
If so, then get position of mouse by pos() method and get current graphics object under arrow by itemAt() method.
Try to cast it into your needed object, if it was successfully, then change background and other.
Store your graphics objects in vector, in this case you can check every object and decide which object should be turned off.
Fully working example:
header:
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QPoint>
#include <QMouseEvent>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
public slots:
private:
QVector<QGraphicsEllipseItem * > vec;
};
#endif // GRAPHICSSCENE_H
cpp:
#include "graphicsscene.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
//set circles in different positions.
vec.push_back(addEllipse(0,0,50,50,QPen(Qt::red),QBrush(Qt::blue)));
vec.push_back(addEllipse(0+100,0+100,50,50,QPen(Qt::red),QBrush(Qt::blue)));
vec.push_back(addEllipse(0+150,0+150,50,50,QPen(Qt::red),QBrush(Qt::blue)));
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//qDebug() << "in";
if (mouseEvent->button() == Qt::LeftButton)
{
QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
QGraphicsEllipseItem *ell = qgraphicsitem_cast<QGraphicsEllipseItem *>(item);
if(ell)
{
for(int i = 0; i < vec.size(); i++)
{
if(vec.at(i) != ell)
{
vec.at(i)->setBrush(QBrush(Qt::blue));//disable all other bettons
}
}
ell->setBrush(QBrush(Qt::black));//to chosen circle
qDebug() << "good";
}
else
qDebug() << "not ell" << mouseEvent->scenePos();
}
}
Usage:
GraphicsScene *scene = new GraphicsScene(this);
ui->graphicsView->setScene(scene);
With this case, your circles will be similar to radioButtons. If you want save state of all buttons you can use:
for(int i = 0; i < vec.size(); i++)
vec.at(i)->setData(0,false);
And get state with
if(!ell->data(0).toBool())
//do

Is there a way to generate a signal on QSlider value increased

I'm trying to trigger a distinct signal from a QSlider for when it increases or decreases. Unfortunately due to restrictions in the program i'm creating outside of my control I cannot track the value of the slider and just do a comparison.
Is there a way to do this?
If I understand you correctly, you can't use valueChanged(int) to decide if the value increased or decreased, since you can't track the value. However, you need to do this, since QSlider doesn't have distinct signals for an increased / decreased value.
Use this helper class to keep track of the current value. It remaps the signal valueChanged(int) of the provided target object to the new signals valueIncreased(int) and valueDecreased(int) telling the (absolute) difference.
class ValueChangeTracker : public QObject
{
Q_OBJECT
int v;
public:
ValueChangeTracker(QObject *target) :
QObject(target)
{
connect(target, SIGNAL(valueChanged(int)), SLOT(changed(int)));
v = target->property("value"); // or ->value() if you specifically use QSlider* instead of QObject* as the target type
}
signals:
void valueIncreased(int);
void valueDecreased(int);
private slots:
void changed(int newValue) {
int diff = newValue - v;
v = newValue;
if(diff > 0) emit valueIncreased(diff);
if(diff < 0) emit valueDecreased(-diff);
}
};
To use it, simply create a new instance of this class and connect to its signals. In the QObject tree, it becomes a child of the target object, so you don't have to keep the pointer to an instance.
QSlider *mySlider = ...
ValueChangeTracker *tracker = new ValueChangeTracker(mySlider);
connect(tracker, SIGNAL(valueIncreased(int)), ...);
connect(tracker, SIGNAL(valueDecreased(int)), ...);
QSlider emits the signal valueChanged, just connect it to a slot:
mySlider.valueChanged.connect(self.on_mySlider_valueChanged)
and to your tests there and emit a custom signal if you like.
I don't fully understand the question, maybe an example would help. But if you look at the documentation for QSlider, it emits signal valueChanged(int) when the value of the slider changes.

In Qt how to sort the immediate child indexes of a QModelIndex

I'm writing a C++ application that uses Qt classes to work with certain data models. For that purpose I inherited from QAbstractItemModel:
// the following is a class that represents the actual data used in my application
class EventFragment
{
....
private:
qint32 address;
QString memo;
QDateTime dateCreated;
QVector<EventFragment*> _children;
....
};
// the following is the model representation that used by my application to display the actual details to the user
class EventModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit EventModel (QObject *parent = 0);
....
private:
// the following is the root item within the model - I use a tree-like presentation to show my data
EventFragment* _rootFragment;
};
At some point I needed a sort/filter option in my application so I also created a class that inherits from QSortFilterProxyModel
class EventProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit EventProxyModel (QObject *parent = 0);
...
public:
// I had to add my custom implementation in the 'lessThan' method to achieve a
// more complex sort logic (not just comparing the actual values but using
// additional conditions to compare the two indexes)
virtual bool lessThan ( const QModelIndex & left, const QModelIndex & right ) const;
...
};
To achieve sorting, I used the default QSortFilterProxyModel::sort() method (I haven't reimplemented it in my proxy model class) and for a time it seemed to work.
At some point though, I noticed that the actual QSortFilterProxyModel::sort() method sorts the entire model and what I need is to sort only the immediate children of a certain index.
I tried to reimplement the sort() method of the EventModel class, but after a while I realized that QSortFilterProxyModel::sort() is not referring to it at all. On the other hand, I'm not sure how to rearrange the indexes in a safe way so that the view which displays the model does not crash.
I think there must be a way to sort only the immediate children of a certain QModelIndex, but I haven't found it yet.
Is there any tutorial/example that demonstrates a possible solution to my case, or some guidelines on how to do it?
Regards
If you want an optimized solution that doesn't do comparisons at all for the indexes you don't want to sort, I think you'd have to reimeplement your own QAbstractProxyModel, which is a non-trivial task. However, if you're fine with a non-optimized solution, I'd try this:
bool EventProxyModel::lessThan( const QModelIndex & left, const QModelIndex & right ) const {
if ( left.parent() == isTheOneToSortChildrenFor ) {
...apply custom comparison
} else {
return left.row() < right.row();
}
}
Comparing the rows in the source should leave everything other then indexes with that specific parent as they are.

Qt: How to track Release or Change event for Phonon::SeekSlider?

I'm using Phonon::SeekSlider, it's a cool thing and I don't need to bother about synchronization between slider and MediaObject but now I need to track the moment when user releases the slider after moving it or when it's value is changed or when the current time of MediaObject is changed. I cannot find any public signals, I can see them in the slider's code but they are private. How could I inherit/implement/track whatever to track this event?
Thanks
You can create a class inherits SeekSlider
in the .h file :
#include <Phonon/SeekSlider>
using namespace Phonon;
class MySeekSlider : public SeekSlider
{
Q_OBJECT
public:
MySeekSlider(QWidget *parent = 0);
signals:
void release(qint64 pos);
protected:
virtual void mouseReleaseEvent(QMouseEvent *);
};
and in the.cpp
void MySeekSlider::mouseReleaseEvent(QMouseEvent *)
{
emit release(this->pos());
}
or use the tick signal of your MediaObject
void tick ( qint64 time )
You may connect to private signals same way as to any others.
QStaticMetaObject ignores visibility level of signals|slots. The only difference of private signals from normal (which has protected visibility level) - you cannot emit them when subclassing your Phonon::SeekSlider.
I suppose there is no way to do it but to create a new class inherited from QSlider, and add the whole functionality about MediaObject inside.

Resources