Results 1 to 4 of 4

Thread: QT application crashes.

  1. #1
    Join Date
    Jul 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QT application crashes.

    Hello:
    I have the following code that generates the following User Interface rrow.png
    Basically its a QDIalog encapsualting 2 QPushButtons and a class derived from QTableWidget. The ADDROW button adds rows. Each row is comprised of 4 fielsd, a checkbox, a combobox and text for the remainder of the tqo. The corresponding code is spread out in the following 4 files.
    Qt Code:
    1. # ifndef __MTBL__
    2. # define __MTBL__
    3.  
    4. # include <QTableWidget>
    5. # include <QPushButton>
    6. # include <QComboBox>
    7. # include <QDialog>
    8. # include <QCheckBox>
    9.  
    10. class myTable : public QTableWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. myTable();
    15. public slots:
    16. void addCustomRow();
    17. void removeCustomRow();
    18. };
    19. # endif
    To copy to clipboard, switch view to plain text mode 

    This is the corresponding source file. myTable.cpp

    Qt Code:
    1. # include "myTable.h"
    2. # include <QMessageBox>
    3.  
    4. myTable::myTable()
    5. {
    6. this->setRowCount(0);
    7. this->setColumnCount(4);
    8. };
    9.  
    10. void myTable::addCustomRow()
    11. {
    12. int row = this->rowCount();
    13. this->insertRow(row);
    14. q->setText("here");
    15. q->show();
    16. QComboBox *mBox = new QComboBox();
    17. mBox->show();
    18. mBox->addItem("Hello");
    19. mBox->addItem("World");
    20. mBox->show();
    21. QCheckBox *mCheck = new QCheckBox();
    22. mCheck->setChecked(false);
    23. mCheck->show();
    24. this->setCellWidget(row,0,mCheck);
    25. this->setCellWidget(row,1,mBox);
    26. this->item(row,2)->setText("Delta " + QString(row));
    27. this->item(row,3)->setText("Theta" + QString(row));
    28. };
    29.  
    30. void myTable::removeCustomRow()
    31. {
    32. int numRows = this->rowCount();
    33.  
    34. for ( int i = 0 ; i < numRows ; i++)
    35. {
    36.  
    37. QCheckBox* mBox = static_cast<QCheckBox*> (this->cellWidget(i,0));
    38. QMessageBox::information(this,"removeCustomRow","code works till here");
    39. if (mBox->isChecked())
    40. {
    41. this->removeRow(i);
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    This is the dialog file which encapsulates myTable and the two buttons.

    Qt Code:
    1. # ifndef __MDLG__
    2. # define __MDLG__
    3. # include <QTableWidget>
    4. # include <QPushButton>
    5. # include <QComboBox>
    6. # include <QDialog>
    7. # include "myTable.h"
    8.  
    9. class myDialog : public QDialog
    10. {
    11. Q_OBJECT
    12. public:
    13. myTable * mTable;
    14. QPushButton * mAddButton;
    15. QPushButton * mRemoveButton;
    16.  
    17. myDialog();
    18. };
    19. # endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. # include "myDialog.h"
    2. # include <QVBoxLayout>
    3.  
    4. myDialog::myDialog()
    5. {
    6. this->mTable = new myTable();
    7. this->mTable->show();
    8. this->mAddButton = new QPushButton("Add Row");
    9. this->mAddButton->show();
    10. this->mRemoveButton = new QPushButton("Remove Row");
    11. this->mRemoveButton->show();
    12.  
    13. QVBoxLayout *mLay = new QVBoxLayout();
    14.  
    15. mLay->addWidget(this->mAddButton);
    16. mLay->addWidget(this->mRemoveButton);
    17. mLay->addWidget(this->mTable);
    18. this->connect(this->mAddButton,SIGNAL(clicked()),this->mTable,SLOT(addCustomRow()));
    19. this->connect(this->mRemoveButton,SIGNAL(clicked()),this->mTable,SLOT(removeCustomRow()));
    20. this->setLayout(mLay);
    21. this->show();
    22. };
    To copy to clipboard, switch view to plain text mode 


    The problem is with the removeCustomRow () slot in myTable.cpp. Whenever I select a checkbox in the UI above and hit "Remove Row" button the application crashes. I put in debugging code and found that the loop in the removeCUstomRow () function tends to remove all the rows (even if they are not checked) and then crashes.
    The above code is standalone so you can copy and paste it in you ide and see its behavior.

    Just include the files and instantiate myDIalog between QApplication a(argc,argv) and app.exe and you will be able to replicate the problem at your end.
    Any help would be appreciated.

  2. #2
    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: QT application crashes.

    You don't check the value of returned pointer:
    Qt Code:
    1. QCheckBox* mBox = static_cast<QCheckBox*> (this->cellWidget(i,0));
    2. QMessageBox::information(this,"removeCustomRow","code works till here");
    3. if (mBox->isChecked())
    To copy to clipboard, switch view to plain text mode 
    what happens if mBox is NULL ? A crash.
    btw. I'd rather use qobject_cast for casting between QObject-based pointers.

  3. #3
    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: QT application crashes.

    Check the logic of your loop. You cache the row count (n) and then proceed to remove rows 0 through n-1. As soon as you remove row 0 the last row is no longer n-1 and the old row 1 becomes the new row 0. You then step on leaving row 0 and delete row 1. At about half way you will try to address a widget and remove a row that no longer exists.

  4. #4
    Join Date
    Jul 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT application crashes.

    chris:

    Thanks. I made the relevant changes and now the code works.

Similar Threads

  1. If GPS is unavailable, application crashes!!!
    By wizarda in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2011, 10:58
  2. Application crashes when it has a particular name
    By hunsrus in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 20:50
  3. Application crashes
    By waynew in forum Newbie
    Replies: 1
    Last Post: 2nd November 2009, 10:31
  4. QtStyles and Application Crashes
    By vladozar in forum Qt Programming
    Replies: 0
    Last Post: 19th September 2009, 14:59
  5. My application crashes
    By sophister in forum Qt Programming
    Replies: 13
    Last Post: 27th April 2009, 07:39

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.