PDA

View Full Version : Access violation -- qobject.cpp



willief
14th February 2011, 19:27
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).


#include <QtGui>
#include "practice.h"

practice::practice(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(pushButton, SIGNAL(clicked()), this, SLOT(addItem()));
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(removeItem()));
connect(pushButton_3, SIGNAL(clicked()), this, SLOT(quit()));
}
void practice::addItem()
{
if( lineEdit->text().length() > 0 )
{
listWidget->insertItem(1, lineEdit->text() );
lineEdit->clear();
}

lineEdit->setFocus();
}

void practice::removeItem()
{
delete listWidget->takeItem(1);
}


void practice::init()
{
listWidget->clear();
lineEdit->setFocus();
}

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.

Zlatomir
14th February 2011, 19:38
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:


practice::practice(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(addItem())); //access the ui. button or list or other widgets
connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(removeItem()));
connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(quit()));
}
//....

Post the header file so we can see what you are doing wrong.

willief
14th February 2011, 20:24
oh erh, I didn't know I was supposed to access them like that :(


#ifndef PRACTICE_H
#define PRACTICE_H

#include <QtGui/QMainWindow>
#include "ui_practice.h"

class practice : public QMainWindow, private Ui::practiceClass
{
Q_OBJECT

public:
practice(QWidget *parent = 0, Qt::WFlags flags = 0);

public slots:
void addItem();
void removeItem();

private:
Ui::practiceClass ui;

private slots:

void init();
};

#endif // PRACTICE_H


there is my header file.

Zlatomir
14th February 2011, 20:50
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.

ChrisW67
14th February 2011, 21:46
The indexes in QListWidget start at 0 not 1 which may also cause you confusion.

willief
14th February 2011, 22:07
#ifndef UI_PRACTICE_H
#define UI_PRACTICE_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QListWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QSpacerItem>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_practiceClass
{
public:
QWidget *centralWidget;
QGroupBox *groupBox;
QGridLayout *gridLayout;
QListWidget *listWidget;
QVBoxLayout *verticalLayout;
QLineEdit *lineEdit;
QPushButton *pushButton;
QPushButton *pushButton_2;
QSpacerItem *verticalSpacer;
QPushButton *pushButton_3;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;

void setupUi(QMainWindow *practiceClass)
{
if (practiceClass->objectName().isEmpty())
practiceClass->setObjectName(QString::fromUtf8("practiceClass"));
practiceClass->resize(663, 638);
centralWidget = new QWidget(practiceClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
groupBox = new QGroupBox(centralWidget);
groupBox->setObjectName(QString::fromUtf8("groupBox"));
groupBox->setGeometry(QRect(9, 9, 641, 531));
gridLayout = new QGridLayout(groupBox);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
listWidget = new QListWidget(groupBox);
listWidget->setObjectName(QString::fromUtf8("listWidget"));

gridLayout->addWidget(listWidget, 0, 0, 2, 1);

verticalLayout = new QVBoxLayout();
verticalLayout->setSpacing(6);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
lineEdit = new QLineEdit(groupBox);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

verticalLayout->addWidget(lineEdit);

pushButton = new QPushButton(groupBox);
pushButton->setObjectName(QString::fromUtf8("pushButton"));

verticalLayout->addWidget(pushButton);

pushButton_2 = new QPushButton(groupBox);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));

verticalLayout->addWidget(pushButton_2);


gridLayout->addLayout(verticalLayout, 0, 1, 1, 1);

verticalSpacer = new QSpacerItem(20, 409, QSizePolicy::Minimum, QSizePolicy::Expanding);

gridLayout->addItem(verticalSpacer, 1, 1, 1, 1);

pushButton_3 = new QPushButton(centralWidget);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
pushButton_3->setGeometry(QRect(300, 550, 75, 23));
pushButton_3->setMaximumSize(QSize(75, 16777215));
practiceClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(practiceClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 663, 21));
practiceClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(practiceClass);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
practiceClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(practiceClass);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
practiceClass->setStatusBar(statusBar);

retranslateUi(practiceClass);

QMetaObject::connectSlotsByName(practiceClass);
} // setupUi

void retranslateUi(QMainWindow *practiceClass)
{
practiceClass->setWindowTitle(QApplication::translate("practiceClass", "practice", 0, QApplication::UnicodeUTF8));
groupBox->setTitle(QApplication::translate("practiceClass", "GroupBox", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("practiceClass", "Add", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("practiceClass", "Remove", 0, QApplication::UnicodeUTF8));
pushButton_3->setText(QApplication::translate("practiceClass", "OK", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class practiceClass: public Ui_practiceClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_PRACTICE_H


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.

Zlatomir
14th February 2011, 22:20
Chris already give you a hint, try it like this:

ui.listWidget->insertItem(0, ui.lineEdit->text() );

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:

ui.list->addItem(ui.lineEdit->text() );
And for removal you can use delete ui.list->currentItem(); to remove the selected item from the list

squidge
14th February 2011, 22:35
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:



class practice : public QMainWindow, private Ui::practiceClass


should be:



class practice : public QMainWindow


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.

willief
14th February 2011, 22:40
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 :D

Zlatomir
14th February 2011, 22:55
@squidge: thanks :o how did i miss that? that was the reason i request the code in the .h file, and i didn't see the fault :o

@willief: this (http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html) 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.