Results 1 to 9 of 9

Thread: QMessageBox exec makes the the solt getting called multiple times

  1. #1
    Join Date
    Feb 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default QMessageBox exec makes the the solt getting called multiple times

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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
    Qt Code:
    1. void Vehicle::Speed(int speed)
    2. {
    3. qWarning()<<"VehicleSpeed is :"<<speed;
    4.  
    5.  
    6. QMessageBox msgBox;
    7. msgBox.setText("Connection Error.");
    8. int ret = msgBox.exec();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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.

  5. #5
    Join Date
    Feb 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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.

  7. #7
    Join Date
    Feb 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    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

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    In that case you can show separate custom widget only once and then only update the displayed value, something like
    Qt Code:
    1. void Class::onValueChanged(int value){
    2. if (this->_infoWidget->isVisible() == false){
    3. this->_infoWidget->show();
    4. }
    5. this->_infoWidget->displayValue(value);
    6. }
    To copy to clipboard, switch view to plain text mode 
    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 ?

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox exec makes the the solt getting called multiple times

    Or disable the slider when there is no point in changing the value.

    Cheers,
    _

Similar Threads

  1. exec QMessageBox
    By saman_artorious in forum Newbie
    Replies: 8
    Last Post: 8th May 2013, 08:15
  2. Replies: 12
    Last Post: 16th September 2011, 23:48
  3. Qt bug? Signal emitted once, slot called multiple times
    By MattPhillips in forum Qt Programming
    Replies: 22
    Last Post: 1st December 2010, 22:32
  4. filterAcceptRows() is being called many times for same souceRow.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2009, 03:49
  5. Replies: 0
    Last Post: 17th May 2008, 18:06

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.