Results 1 to 8 of 8

Thread: MessageBox appearnig twice.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default MessageBox appearnig twice.

    Hi,

    I am new to Qt GUI programming but well aware of C++ concepts. I am developing simple application which takes data from GUI and inserts into Database. Before inserting data into DB few validation are being performed for valid data.

    Now to show validation failure I am using QMessageBox class as provided by Qt. Problem is coming when popping up message box with QMessageBox. After clicking OK button it displays the same message box again and again I have to click on OK button to close it.

    While posting the Thread I am not having source code available but will be happy to share for better understanding of issue.

    If any one has faced similar issue please let me know the solution.

    Qt Version : 4.6.0
    OS : Windows Vista 64-bit
    Compiler : MinGW g++ 4.4.0 as supplied with Qt.
    Database : Oracle 11g 64-bit

    Thanks,
    Ronak
    Last edited by ronak.vashi; 17th May 2010 at 11:08.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    Put a breakpoint and check where the call to messagebox is coming from...

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MessageBox appearnig twice.

    Message box runs its own event loop, so the chances are an event is happening in your event loop, you are creating message box and then getting another event and creating another message box, etc, as you can receive new events even if you are displaying message box.

  4. #4
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    Quote Originally Posted by aamer4yu View Post
    Put a breakpoint and check where the call to messagebox is coming from...
    Hi,
    Tried debugging and found that function validateInput is getting called twice from qobject.cpp and hence MessageBox is appearing twice.

    From thorough debugging it is found that in qobject.cpp statement @ line # 4 & line# 8 are returning two different Memory address hence two different Connection.

    I am not sure what this Connections objects and why it has two different objects.

    Also, one observation is as follow:

    • Application is having small login window asking User-name and password. Here also few validations are performed and QMessageBox is used to give appropriate error message,but here Message window is not appearing twice.



    Here is a piece of code from validateInput function.

    Function: validateInput.

    Qt Code:
    1. QString expenseHead = ui->expenseHead->text();
    2. int expenseCategory = ui->expenseClassCombo->currentIndex();
    3. int expenseSubCategory = ui->expenseSubClassCombo->currentIndex();
    4. QString amountSpent = ui->expenseAmount->text();
    5. QString expenseDate = ui->expenseDate->text();
    6. QString expenseRemarks = ui->remarks->toPlainText();
    7.  
    8. // QMessageBox::information(0,"Info",expenseHead + "|" + QString::number(expenseCategory) + "|"
    9. // + QString::number(expenseSubCategory));
    10. if(expenseHead.isEmpty()){
    11. QMessageBox::critical(0,QObject::tr("Validation Error"),"Expense Head is required. It can't be left blank.");
    12. //ui->expenseHead->setFocus();
    13. return false;
    14. }
    15.  
    16. if(expenseCategory == 0){
    17. QMessageBox::critical(0,QObject::tr("Validation Error"),"Expense Category must be selected.");
    18. ui->expenseClassCombo->setFocus();
    19. return false;
    20. }
    21.  
    22. if(expenseSubCategory == 0){
    23. QMessageBox::critical(0,QObject::tr("Validation Error"),"Expense SubCategory must be selected.");
    24. ui->expenseSubClassCombo->setFocus();
    25. return false;
    26. }
    27.  
    28. if(amountSpent.isEmpty()){
    29. QMessageBox::critical(0,QObject::tr("Validation Error"),"Amount Spent is required. It can't be left blank.");
    30. ui->expenseAmount->setFocus();
    31. return false;
    32. }
    33.  
    34. if(amountSpent.toDouble() == 0.00){
    35. QMessageBox::critical(0,QObject::tr("Wrong Input"),"Amount must be greater than zero.");
    36. ui->expenseAmount->setFocus();
    37. return false;
    38. }
    39. return true;
    To copy to clipboard, switch view to plain text mode 

    File : qobject.cpp

    Qt Code:
    1. ...
    2.  
    3. do {
    4. QObjectPrivate::Connection *c = connectionLists->at(signal_index).first;
    5. if (!c) continue;
    6. // We need to check against last here to ensure that signals added
    7. // during the signal emission are not emitted in this emission.
    8. QObjectPrivate::Connection *last = connectionLists->at(signal_index).last;
    9.  
    10. do {
    11. if (!c->receiver)
    12. continue;
    13.  
    14. QObject * const receiver = c->receiver;
    15.  
    16. // determine if this connection should be sent immediately or
    17. // put into the event queue
    18. if ((c->connectionType == Qt::AutoConnection
    19. && (currentThreadData != sender->d_func()->threadData
    20. || receiver->d_func()->threadData != sender->d_func()->threadData))
    21. || (c->connectionType == Qt::QueuedConnection)) {
    22. queued_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv);
    23. continue;
    24. } else if (c->connectionType == Qt::BlockingQueuedConnection) {
    25. blocking_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv);
    26. continue;
    27. }
    28.  
    29. const int method = c->method;
    30. QObjectPrivate::Sender currentSender;
    31. currentSender.sender = sender;
    32. currentSender.signal = signal_absolute_index;
    33. currentSender.ref = 1;
    34. QObjectPrivate::Sender *previousSender = 0;
    35. if (currentThreadData == receiver->d_func()->threadData)
    36. previousSender = QObjectPrivate::setCurrentSender(receiver, &currentSender);
    37. locker.unlock();
    38.  
    39. if (qt_signal_spy_callback_set.slot_begin_callback != 0) {
    40. qt_signal_spy_callback_set.slot_begin_callback(receiver,
    41. method,
    42. argv ? argv : empty_argv);
    43. }
    44.  
    45. #if defined(QT_NO_EXCEPTIONS)
    46. metacall(receiver, QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv);
    47. #else
    48. QT_TRY {
    49. metacall(receiver, QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv);
    50. } QT_CATCH(...) {
    51. locker.relock();
    52.  
    53. QObjectPrivate::resetCurrentSender(receiver, &currentSender, previousSender);
    54.  
    55. --connectionLists->inUse;
    56. Q_ASSERT(connectionLists->inUse >= 0);
    57. if (connectionLists->orphaned && !connectionLists->inUse)
    58. delete connectionLists;
    59. QT_RETHROW;
    60. }
    61. #endif
    62.  
    63. if (qt_signal_spy_callback_set.slot_end_callback != 0)
    64. qt_signal_spy_callback_set.slot_end_callback(receiver, method);
    65.  
    66. locker.relock();
    67.  
    68. QObjectPrivate::resetCurrentSender(receiver, &currentSender, previousSender);
    69.  
    70. if (connectionLists->orphaned)
    71. break;
    72. } while (c != last && (c = c->nextConnectionList) != 0);
    73.  
    74. if (connectionLists->orphaned)
    75. break;
    76. } while (signal_index >= 0 && (signal_index = -1)); //start over for -1 (all signal)
    77.  
    78. ...
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    Hi all,

    Any idea, how to avoid double appearance of messagebox ?
    I have got stuck due to this weird behavior.

    Please help..

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    I take it that validateInput() is a slot.
    Can you show which signals did you connect to this slot?

    It looks like you have connected several objects to this slot, and any of these signals trigger the slot, and since you are doing a lot of checking on various widgets in the slots it self, it is very well possible that multiple conditions are true, and thus trigger multiple message boxes.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    Quote Originally Posted by high_flyer View Post
    I take it that validateInput() is a slot.
    Can you show which signals did you connect to this slot?

    It looks like you have connected several objects to this slot, and any of these signals trigger the slot, and since you are doing a lot of checking on various widgets in the slots it self, it is very well possible that multiple conditions are true, and thus trigger multiple message boxes.
    Hi,
    validateInput() is not a slot but called by slot on_addButton_clicked() when "Add Button" is clicked. As shown in below code.

    Here are few thing about application, which may help to understand the issue.
    1). I have created QMainWindowGUI with various menu and submenus.
    2). On clicking Add -> Expense -> Add Single Entry I am creating SingleEntry widget with QMainWindow as parent where in all text boxes, dropdown and button stuff comes into picture.
    3). On this singleentry widget while performing validation this issue is occurring.
    4). setupSingleEntry function is called when "Add Single Entry" submenu option is clicked.

    Let me know if further input is required.

    Qt Code:
    1. void SingleEntry::setupSingleEntry(){
    2. setupSingleEntrySignalSlots();
    3. populateExpenseCategory();
    4. ui->expenseDate->setDate(QDate::currentDate());
    5. ui->expenseDate->setMaximumDate(QDate::currentDate()); /* Restrict user from selecting Future Dates */
    6. }
    7.  
    8. void SingleEntry::setupSingleEntrySignalSlots(){
    9. connect(ui->expenseClassCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(on_expenseClassCombo_currentIndexChanged()));
    10. connect(ui->adjustmentCheckBox,SIGNAL(clicked()),this,SLOT(on_adjustmentCheckBox_clicked()));
    11. connect(ui->addButton,SIGNAL(clicked()),this,SLOT(on_addButton_clicked()));
    12. }
    13.  
    14. void SingleEntry::on_addButton_clicked(){
    15. if(validateInput()){
    16. QMessageBox::information(0,"Success","Validation Successful");
    17. }
    18. else{
    19.  
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox appearnig twice.

    Hi All,

    Issue got resolved. Actual problem was in setupSingleEntrySignalSlots()function. I am connecting addButton with SLOT on_addButton_clicked() second time. First time it is already done Qt with in-built function connectSlotsByName(). So on_addButton_clicked() was getting called twice and hence appearance of messagebox .

    Thanks for your suggestions.

    Quote Originally Posted by ronak.vashi View Post
    Hi,
    validateInput() is not a slot but called by slot on_addButton_clicked() when "Add Button" is clicked. As shown in below code.

    Here are few thing about application, which may help to understand the issue.
    1). I have created QMainWindowGUI with various menu and submenus.
    2). On clicking Add -> Expense -> Add Single Entry I am creating SingleEntry widget with QMainWindow as parent where in all text boxes, dropdown and button stuff comes into picture.
    3). On this singleentry widget while performing validation this issue is occurring.
    4). setupSingleEntry function is called when "Add Single Entry" submenu option is clicked.

    Let me know if further input is required.

    Qt Code:
    1. void SingleEntry::setupSingleEntry(){
    2. setupSingleEntrySignalSlots();
    3. populateExpenseCategory();
    4. ui->expenseDate->setDate(QDate::currentDate());
    5. ui->expenseDate->setMaximumDate(QDate::currentDate()); /* Restrict user from selecting Future Dates */
    6. }
    7.  
    8. void SingleEntry::setupSingleEntrySignalSlots(){
    9. connect(ui->expenseClassCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(on_expenseClassCombo_currentIndexChanged()));
    10. connect(ui->adjustmentCheckBox,SIGNAL(clicked()),this,SLOT(on_adjustmentCheckBox_clicked()));
    11. connect(ui->addButton,SIGNAL(clicked()),this,SLOT(on_addButton_clicked()));
    12. }
    13.  
    14. void SingleEntry::on_addButton_clicked(){
    15. if(validateInput()){
    16. QMessageBox::information(0,"Success","Validation Successful");
    17. }
    18. else{
    19.  
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. set Bold to MessageBox particular Text
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 11:53
  2. MessageBox Validation
    By fortyhideout12 in forum Newbie
    Replies: 10
    Last Post: 2nd September 2009, 17:14
  3. Replies: 1
    Last Post: 21st April 2008, 22:43
  4. default on focus different item in messagebox?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 05:54
  5. QSA 1.2.1 && MessageBox
    By xk in forum Newbie
    Replies: 0
    Last Post: 20th April 2006, 17:30

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
  •  
Qt is a trademark of The Qt Company.