Results 1 to 10 of 10

Thread: Access violation -- qobject.cpp

  1. #1
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Access violation -- qobject.cpp

    Hi,

    I'm new to using Qt and I'm having a small problem, when I try to execute my program I'm getting this error message

    Unhandled exception at 0x671e26f1 (QtCored4.dll) in practice.exe: 0xC0000005: Access violation reading location 0x00000004.

    and it's telling me it is in qobject.cpp (which obviously can't be right).

    Qt Code:
    1. #include <QtGui>
    2. #include "practice.h"
    3.  
    4. practice::practice(QWidget *parent, Qt::WFlags flags)
    5. : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8. connect(pushButton, SIGNAL(clicked()), this, SLOT(addItem()));
    9. connect(pushButton_2, SIGNAL(clicked()), this, SLOT(removeItem()));
    10. connect(pushButton_3, SIGNAL(clicked()), this, SLOT(quit()));
    11. }
    12. void practice::addItem()
    13. {
    14. if( lineEdit->text().length() > 0 )
    15. {
    16. listWidget->insertItem(1, lineEdit->text() );
    17. lineEdit->clear();
    18. }
    19.  
    20. lineEdit->setFocus();
    21. }
    22.  
    23. void practice::removeItem()
    24. {
    25. delete listWidget->takeItem(1);
    26. }
    27.  
    28.  
    29. void practice::init()
    30. {
    31. listWidget->clear();
    32. lineEdit->setFocus();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas from anyone? Also while I'm here, can anyone explain to me the correct way to access mimeData()? I was hoping to do a check for an empty list widget with it and then only if the list wasn't empty could you perform the remove, but I can't quite figure out the syntax. Thanks.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access violation -- qobject.cpp

    You have a ui object (dedduced from: ui.setupUi(this); ) and then you access some buttons and list and the rest of the widgets that are not from the ui object (and are not initialized)

    Isn't your code supposed to look something like this:
    Qt Code:
    1. practice::practice(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(addItem())); //access the ui. button or list or other widgets
    6. connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(removeItem()));
    7. connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(quit()));
    8. }
    9. //....
    To copy to clipboard, switch view to plain text mode 
    Post the header file so we can see what you are doing wrong.

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

    willief (14th February 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access violation -- qobject.cpp

    oh erh, I didn't know I was supposed to access them like that

    Qt Code:
    1. #ifndef PRACTICE_H
    2. #define PRACTICE_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_practice.h"
    6.  
    7. class practice : public QMainWindow, private Ui::practiceClass
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. practice(QWidget *parent = 0, Qt::WFlags flags = 0);
    13.  
    14. public slots:
    15. void addItem();
    16. void removeItem();
    17.  
    18. private:
    19. Ui::practiceClass ui;
    20.  
    21. private slots:
    22.  
    23. void init();
    24. };
    25.  
    26. #endif // PRACTICE_H
    To copy to clipboard, switch view to plain text mode 

    there is my header file.

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access violation -- qobject.cpp

    If you didn't declare the pushButton_2 like: QPushButton* pushButton_2; that code shouldn't compile, so you have removed that pointers declaration form the header file?

    Are you accessing the UI components through ui object ( using ui.WidgetName)?
    It should work then.

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

    willief (14th February 2011)

  7. #5
    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: Access violation -- qobject.cpp

    The indexes in QListWidget start at 0 not 1 which may also cause you confusion.

  8. The following user says thank you to ChrisW67 for this useful post:

    willief (14th February 2011)

  9. #6
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access violation -- qobject.cpp

    Qt Code:
    1. #ifndef UI_PRACTICE_H
    2. #define UI_PRACTICE_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QGridLayout>
    9. #include <QtGui/QGroupBox>
    10. #include <QtGui/QHeaderView>
    11. #include <QtGui/QLineEdit>
    12. #include <QtGui/QListWidget>
    13. #include <QtGui/QMainWindow>
    14. #include <QtGui/QMenuBar>
    15. #include <QtGui/QPushButton>
    16. #include <QtGui/QSpacerItem>
    17. #include <QtGui/QStatusBar>
    18. #include <QtGui/QToolBar>
    19. #include <QtGui/QVBoxLayout>
    20. #include <QtGui/QWidget>
    21.  
    22. QT_BEGIN_NAMESPACE
    23.  
    24. class Ui_practiceClass
    25. {
    26. public:
    27. QWidget *centralWidget;
    28. QGroupBox *groupBox;
    29. QGridLayout *gridLayout;
    30. QListWidget *listWidget;
    31. QVBoxLayout *verticalLayout;
    32. QLineEdit *lineEdit;
    33. QPushButton *pushButton;
    34. QPushButton *pushButton_2;
    35. QSpacerItem *verticalSpacer;
    36. QPushButton *pushButton_3;
    37. QMenuBar *menuBar;
    38. QToolBar *mainToolBar;
    39. QStatusBar *statusBar;
    40.  
    41. void setupUi(QMainWindow *practiceClass)
    42. {
    43. if (practiceClass->objectName().isEmpty())
    44. practiceClass->setObjectName(QString::fromUtf8("practiceClass"));
    45. practiceClass->resize(663, 638);
    46. centralWidget = new QWidget(practiceClass);
    47. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    48. groupBox = new QGroupBox(centralWidget);
    49. groupBox->setObjectName(QString::fromUtf8("groupBox"));
    50. groupBox->setGeometry(QRect(9, 9, 641, 531));
    51. gridLayout = new QGridLayout(groupBox);
    52. gridLayout->setSpacing(6);
    53. gridLayout->setContentsMargins(11, 11, 11, 11);
    54. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    55. listWidget = new QListWidget(groupBox);
    56. listWidget->setObjectName(QString::fromUtf8("listWidget"));
    57.  
    58. gridLayout->addWidget(listWidget, 0, 0, 2, 1);
    59.  
    60. verticalLayout = new QVBoxLayout();
    61. verticalLayout->setSpacing(6);
    62. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    63. lineEdit = new QLineEdit(groupBox);
    64. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    65.  
    66. verticalLayout->addWidget(lineEdit);
    67.  
    68. pushButton = new QPushButton(groupBox);
    69. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    70.  
    71. verticalLayout->addWidget(pushButton);
    72.  
    73. pushButton_2 = new QPushButton(groupBox);
    74. pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    75.  
    76. verticalLayout->addWidget(pushButton_2);
    77.  
    78.  
    79. gridLayout->addLayout(verticalLayout, 0, 1, 1, 1);
    80.  
    81. verticalSpacer = new QSpacerItem(20, 409, QSizePolicy::Minimum, QSizePolicy::Expanding);
    82.  
    83. gridLayout->addItem(verticalSpacer, 1, 1, 1, 1);
    84.  
    85. pushButton_3 = new QPushButton(centralWidget);
    86. pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
    87. pushButton_3->setGeometry(QRect(300, 550, 75, 23));
    88. pushButton_3->setMaximumSize(QSize(75, 16777215));
    89. practiceClass->setCentralWidget(centralWidget);
    90. menuBar = new QMenuBar(practiceClass);
    91. menuBar->setObjectName(QString::fromUtf8("menuBar"));
    92. menuBar->setGeometry(QRect(0, 0, 663, 21));
    93. practiceClass->setMenuBar(menuBar);
    94. mainToolBar = new QToolBar(practiceClass);
    95. mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
    96. practiceClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
    97. statusBar = new QStatusBar(practiceClass);
    98. statusBar->setObjectName(QString::fromUtf8("statusBar"));
    99. practiceClass->setStatusBar(statusBar);
    100.  
    101. retranslateUi(practiceClass);
    102.  
    103. QMetaObject::connectSlotsByName(practiceClass);
    104. } // setupUi
    105.  
    106. void retranslateUi(QMainWindow *practiceClass)
    107. {
    108. practiceClass->setWindowTitle(QApplication::translate("practiceClass", "practice", 0, QApplication::UnicodeUTF8));
    109. groupBox->setTitle(QApplication::translate("practiceClass", "GroupBox", 0, QApplication::UnicodeUTF8));
    110. pushButton->setText(QApplication::translate("practiceClass", "Add", 0, QApplication::UnicodeUTF8));
    111. pushButton_2->setText(QApplication::translate("practiceClass", "Remove", 0, QApplication::UnicodeUTF8));
    112. pushButton_3->setText(QApplication::translate("practiceClass", "OK", 0, QApplication::UnicodeUTF8));
    113. } // retranslateUi
    114.  
    115. };
    116.  
    117. namespace Ui {
    118. class practiceClass: public Ui_practiceClass {};
    119. } // namespace Ui
    120.  
    121. QT_END_NAMESPACE
    122.  
    123. #endif // UI_PRACTICE_H
    To copy to clipboard, switch view to plain text mode 

    they were declared in the ui_practice file and I assumed through inheritance it should work. (and it did) after fixing the way I was accessing them, I was able to build and run the program but then when trying to add/remove it crashes. Thanks for all of your help everyone.

  10. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access violation -- qobject.cpp

    Chris already give you a hint, try it like this:
    Qt Code:
    1. ui.listWidget->insertItem(0, ui.lineEdit->text() );
    To copy to clipboard, switch view to plain text mode 
    To insert in the first position, because in the beginning the list is empty and you try to insert at an invalid row.
    Alternative way is to use addItem:
    Qt Code:
    1. ui.list->addItem(ui.lineEdit->text() );
    To copy to clipboard, switch view to plain text mode 
    And for removal you can use delete ui.list->currentItem(); to remove the selected item from the list

  11. The following user says thank you to Zlatomir for this useful post:

    willief (14th February 2011)

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

    Default Re: Access violation -- qobject.cpp

    Maybe I ask why you are using both multiple inheritance and accessing the ui namespace in your header file? This is undoubtedly causing you great confusion.

    eg:

    Qt Code:
    1. class practice : public QMainWindow, private Ui::practiceClass
    To copy to clipboard, switch view to plain text mode 

    should be:

    Qt Code:
    1. class practice : public QMainWindow
    To copy to clipboard, switch view to plain text mode 

    Otherwise all the objects and created both in your class, and via the ui pointer, but only one is being initialised (and thus accessing the other crashes your program)

    If you wish, you can use the former, but please stick to just one method, and the second is more OO friendly and encapsulated.

  13. The following 2 users say thank you to squidge for this useful post:

    willief (14th February 2011), Zlatomir (14th February 2011)

  14. #9
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access violation -- qobject.cpp

    Yes! Success you guys have taught me so much. My remove function doesn't work, but the program doesn't crash and I think I can get it from here. Thank youuuu

  15. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access violation -- qobject.cpp

    @squidge: thanks how did i miss that? that was the reason i request the code in the .h file, and i didn't see the fault

    @willief: this is a documentation page with explanation of the different methods you can use to integrate .ui file into your C++ code, might help you in the future.

Similar Threads

  1. Access Violation - Crashing Application
    By LIRiot in forum Qt Programming
    Replies: 9
    Last Post: 5th December 2010, 23:10
  2. Access Violation with VS2008
    By Takatschio in forum Qt Programming
    Replies: 3
    Last Post: 19th August 2010, 09:16
  3. QModelIndexList access reading violation
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 08:32
  4. OpenCV causes access violation only during timerEvent
    By derekkingston in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2010, 08:56
  5. Access Violation on Signal Emit
    By khagzan in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2007, 22:51

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.