PDA

View Full Version : QMessageBox exec makes the the solt getting called multiple times



jinuthomas
10th February 2014, 10:26
Hi,
I have defined a slot for the slider controls valuechanged signal. In that slot I am using a messagebox exec() to display an error message. When i click on slider the error message window opens , but the solt is ggeting called multiple times and the message window pops up many times. How to handle this situation. Code is pasted below

void Vehicle::Speed(int speed)
{
qWarning()<<"VehicleSpeed is :"<<speed;


QMessageBox msgBox;
msgBox.setText("Connection Error.");
int ret = msgBox.exec();

}

Slot defined was Vehicle::Speed. Logs printed are

VehicleSpeed is : 5
VehicleSpeed is : 10
VehicleSpeed is : 15
VehicleSpeed is : 20
VehicleSpeed is : 25
VehicleSpeed is : 30
VehicleSpeed is : 35

Thanks,
Jinu

Lesiok
10th February 2014, 13:44
QDialog::exec() don't stop application. Events are normally handled. Show us real code especially how they are combined signals and slots.
And please use CODE tag as below

void Vehicle::Speed(int speed)
{
qWarning()<<"VehicleSpeed is :"<<speed;


QMessageBox msgBox;
msgBox.setText("Connection Error.");
int ret = msgBox.exec();

}

jinuthomas
10th February 2014, 14:24
Hi,
Signal and slot was associated using the QtUi. The generated code for Vehicle data is pasted below.

ehicleSpeedDisplay = new QSlider(SpeedDisplaygroupBox);
VehicleSpeedDisplay->setObjectName(QStringLiteral("VehicleSpeedDisplay"));
VehicleSpeedDisplay->setGeometry(QRect(10, 90, 161, 31));
VehicleSpeedDisplay->setInputMethodHints(Qt::ImhNone);
VehicleSpeedDisplay->setMaximum(250);
VehicleSpeedDisplay->setSingleStep(5);
VehicleSpeedDisplay->setPageStep(5);
VehicleSpeedDisplay->setValue(60);
VehicleSpeedDisplay->setTracking(true);
VehicleSpeedDisplay->setOrientation(Qt::Horizontal);
VehicleSpeedDisplay->setTickPosition(QSlider::TicksBelow);
VehicleSpeedDisplay->setTickInterval(25);

QObject::connect(VehicleSpeedDisplay, SIGNAL(valueChanged(int)), VehicleData, SLOT(VehicleSpeed(int)))

The slot implementationand the logs were already pasted in the first post.

If I am commented the message display box , only
"VehicleSpeed is : 5 " log will be generated, means the slot is getting called only once. But with the exec() call slot is getting called multiple times

Thanks,
Jinu

ChrisW67
10th February 2014, 20:11
The message box has nothing to do with it. The slot will be called every time the value of the slider changes. If you grab the handle of the slider and move it up and down then the slot will be called a lot of times.

jinuthomas
11th February 2014, 06:37
Hi,
Thanks for the reply. But I am not moving the slider up or down. I just did a click on the middle of the slider. In this case the the slot gets called many times if the message box is displayed and just once if the message box part was commented out. It looks weird for me. If anybody had faced similar issues or could just try out this scenario, please help me out.

Thanks,
Jinu

stampede
11th February 2014, 07:42
Clicking in the middle of the slider can cause multiple value changes. I don't know why this bothers you - what is your real problem ? Displaying a message box on every slider value change makes little sense to me for a real life application, maybe for debugging - but then you should be using qDebug() rather than message box.

jinuthomas
11th February 2014, 08:20
Message box is used to inform the user about the hardware is not connected. Since the valuechanged is getting called many times this messagebox windows also pops up multiple times. I could bolock the multiple pop up programatically. But the slider values are getting are keep on changing multiple times. If the hardware is connected , no need to display the warning, hence irrespective of the position clicked on the slider, valuechanged gets called only once. I could handle the situation programatically, but i would like to understand the reason behind that.

Instead of exec() if i use show(), then the behaviour is proper, means valuechanged is called only once.

Thanks,
Jinu

stampede
11th February 2014, 09:06
In that case you can show separate custom widget only once and then only update the displayed value, something like


void Class::onValueChanged(int value){
if (this->_infoWidget->isVisible() == false){
this->_infoWidget->show();
}
this->_infoWidget->displayValue(value);
}

I think you get the idea.
Another way is to have a separate part of the main layout designated for the information notifications.

Btw. maybe you could use separate slots for "connected / disconnected" and "value changed" notifications ? From what I understand, there is no way to change the slider value when the hardware is disconnected, is that right ?

anda_skoa
11th February 2014, 10:01
Or disable the slider when there is no point in changing the value.

Cheers,
_