Results 1 to 5 of 5

Thread: QComboBox crash

  1. #1
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QComboBox crash

    Hi!

    When I try to call QComboBox::addItem from another function in my class than the constructor, my application crashes.
    I've made a short application to reproduce the crash, here's the code (.zip included for those who want).

    ComboBoxCrash.pro
    Qt Code:
    1. HEADERS += GUI.h
    2. SOURCES += GUI.cpp \
    3. main.cpp
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtGui>
    3. #include "GUI.h"
    4.  
    5.  
    6. int main(int argc, char *argv[]){
    7. QApplication a(argc, argv);
    8.  
    9. ComboBoxCrash *MainWindow = new ComboBoxCrash;
    10. MainWindow->show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    GUI.h
    Qt Code:
    1. #ifndef GUI_H
    2. #define GUI_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QComboBox;
    7.  
    8. class ComboBoxCrash : public QWidget{
    9. Q_OBJECT
    10.  
    11. public:
    12. ComboBoxCrash(QWidget *parent=0);
    13. public slots:
    14. void function();
    15. private:
    16. QComboBox *comboBox;
    17. QPushButton *crashButton;
    18. };
    19.  
    20. #endif // GUI_H
    To copy to clipboard, switch view to plain text mode 

    GUI.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "GUI.h"
    3.  
    4. ComboBoxCrash::ComboBoxCrash(QWidget *parent) : QWidget(parent){
    5.  
    6. QComboBox *comboBox = new QComboBox;
    7. QPushButton *crashButton = new QPushButton("Crash");
    8.  
    9. QGridLayout *mainLayout = new QGridLayout;
    10. mainLayout->addWidget(comboBox,0,0);
    11. mainLayout->addWidget(crashButton,0,1);
    12. setLayout(mainLayout);
    13. setWindowTitle("ComboBox Crash");
    14.  
    15. comboBox->addItem("Item1");
    16. comboBox->addItem("Item2");
    17. comboBox->addItem("Item3");
    18.  
    19. connect(crashButton, SIGNAL(clicked()), this, SLOT(function()));
    20. }
    21.  
    22. void ComboBoxCrash::function(){
    23. comboBox->addItem("Item4");
    24. }
    To copy to clipboard, switch view to plain text mode 

    When the function which calls addItem is called, the application crashes.
    I'm using Qt 4.5.2 and Qt Creator 1.2.0 under Windows XP SP2.

    If anyone know why this happens, or even better, have a solution, I would be very happy.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QComboBox crash

    Oh, that's easy. You have two "comboBox" variables - one is a member variable and the other is a local variable created (and destroyed) in the constructor. You are assigning an object to the local one and try to dereference the member which obviously results in a crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    sfilez (8th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox crash

    Thanks for the fast reply wysota!

    What a silly mistake, got it fixed now.

  5. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox crash

    Replace

    Qt Code:
    1. QComboBox *comboBox = new QComboBox;
    2. QPushButton *crashButton = new QPushButton("Crash");
    To copy to clipboard, switch view to plain text mode 
    replace that with
    Qt Code:
    1. comboBox = new QComboBox;
    2. crashButton = new QPushButton("Crash");
    To copy to clipboard, switch view to plain text mode 
    :-)
    We can't solve problems by using the same kind of thinking we used when we created them

  6. The following user says thank you to sunil.thaha for this useful post:

    sfilez (8th July 2009)

  7. #5
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox crash

    Already fixed it, but thanks for replying anyways sunil.thaha

Similar Threads

  1. Crash: Heap corruption due to selectedRows()
    By Ankitha Varsha in forum Qt Programming
    Replies: 16
    Last Post: 1st October 2010, 01:55
  2. QString comparison gives wierd crash
    By supergillis in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2009, 23:09
  3. QComboBox problem
    By SteveH in forum Newbie
    Replies: 2
    Last Post: 2nd March 2009, 22:20
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 20:25
  5. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 17:14

Tags for this Thread

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.