PDA

View Full Version : my program crash when I use external slot



SIFE
13th March 2011, 10:54
I am using external slot to change line edit value, after connecting this slot and run my application it crash.
I am runing in this issuse from the last 4 days, I try many thinks but it doesn't work to me.
I have tow classes, one for GUI, and one for changing widget values and other things:
getdata class:


#ifndef GETDATA_H
#define GETDATA_H

#include <QObject>

#include "store.h"

class QString;

class GetData : public QObject, private Ui::Store
{
Q_OBJECT
public:
GetData();

private slots:
void setItemCountity();
};

#endif // GETDATA_H[/QTCLASS]





#include <QString>

#include "getdata.h"

GetData::GetData() : Ui::Store()
{

}

void GetData::setItemCountity()
{
addressWholesaleAdd->setText("dddddddd");
}

store class:


#ifndef STORE_H
#define STORE_H

#include <QMainWindow>

#include "ui_store.h"

class Store : public QMainWindow
{
Q_OBJECT
public:
Store(QWidget *parent = 0);

private:
Ui::Store ui;

private slots:
};

#endif // STORE_H



#include "store.h"
#include "getdata.h"

Store::Store(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);

.
.
.

GetData *dd = new GetData;

connect(ui.addWholesaleAdd, SIGNAL(clicked()), dd, SLOT(setItemCountity()));
}

wysota
13th March 2011, 10:57
Please provide backtrace from the crash.

unit
13th March 2011, 11:13
if addressWholesaleAdd is QPushButton try this code


void GetData::setItemCountity()
{
QPushButton *mybut = qobject_cast<QPushButton *>(sender());
mybut->setText("dddddddd");
}


But if you want to manipulate ui object of store class you should find another way

SIFE
13th March 2011, 11:24
Please provide backtrace from the crash.
How to do that, because I see only don't send message.

if addressWholesaleAdd is QPushButton try this code


void GetData::setItemCountity()
{
QPushButton *mybut = qobject_cast<QPushButton *>(sender());
mybut->setText("dddddddd");
}


But if you want to manipulate ui object of store class you should find another way
its QLineEdit, and it doesn't work to after I changed to QLineEdit.

unit
13th March 2011, 11:32
In your code you try call function of object but don't have pointer to it. So if you want manipulate ui of store class, you should have pointer to it at GetData class. I'm try your code in this way:


class GetData : public QObject, private Ui::Store
{
Q_OBJECT
public:
GetData(Ui::Store*);

private slots:
void setItemCountity();

private:
Ui::Store *ui;
};


GetData::GetData(Ui::Store *mainui)
{
ui=mainui;
}


GetData *dd = new GetData(&ui);

wysota
13th March 2011, 11:48
How to do that, because I see only don't send message.
Run the application under a debugger and when it crashes make it print the backtrace and post it here.

SIFE
14th March 2011, 12:32
Thread 3 (Thread 2404.0x540):
#0 0x7c91eb94 in ntdll!LdrAccessResource () from E:\WINDOWS\system32\ntdll.dll
#1 0x7c91d85c in ntdll!ZwDelayExecution () from E:\WINDOWS\system32\ntdll.dll
#2 0x7c8023ed in SleepEx () from E:\WINDOWS\system32\kernel32.dll
#3 0x00000000 in ?? ()

Thread 2 (Thread 2404.0xfb8):
#0 0x7c91eb94 in ntdll!LdrAccessResource () from E:\WINDOWS\system32\ntdll.dll
#1 0x7c91e399 in ntdll!ZwReplyWaitReceivePortEx () from E:\WINDOWS\system32\ntdll.dll
#2 0x77e56703 in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#3 0x77e56c22 in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#4 0x77e56a3b in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#5 0x77e56c0a in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#6 0x7c80b50b in KERNEL32!GetModuleFileNameA () from E:\WINDOWS\system32\kernel32.dll
#7 0x00000000 in ?? ()

Thread 1 (Thread 2404.0x828):
#0 0x00d28bee in QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data (this=0x4) at ../../include/QtCore/../../src/corelib/tools/qscopedpointer.h:135
#1 0x0093da9e in qGetPtrHelper<QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> > > (p=...) at ../../include/QtCore/../../src/corelib/global/qglobal.h:2328
#2 0x00d1ebe0 in QLineEdit::d_func (this=0x0) at ../../include/QtGui/../../src/gui/widgets/qlineedit.h:275
#3 0x0098f01f in QLineEdit::setText (this=0x0, text=...) at widgets\qlineedit.cpp:382
#4 0x00401d85 in GetData::setItemCountity (this=0x9a301f0) at getdata.cpp:12
#5 0x00401f72 in GetData::qt_metacall (this=0x9a301f0, _c=QMetaObject::InvokeMetaMethod, _id=0, _a=0x22cc68) at debug\moc_getdata.cpp:72
#6 0x6a20445c in QMetaObject::metacall (object=0x9a301f0, cl=QMetaObject::InvokeMetaMethod, idx=4, argv=0x22cc68) at kernel\qmetaobject.cpp:237
#7 0x6a214bbe in QMetaObject::activate (sender=0x9a0f6e8, m=0xe6ece0, local_signal_index=2, argv=0x22cc68) at kernel\qobject.cpp:3272
#8 0x00bc2e3c in QAbstractButton::clicked (this=0x9a0f6e8, _t1=false) at tmp\moc\debug_shared\moc_qabstractbutton.cpp:206
#9 0x0093243b in QAbstractButtonPrivate::emitClicked (this=0x9a0f718) at widgets\qabstractbutton.cpp:546
#10 0x009323b9 in QAbstractButtonPrivate::click (this=0x9a0f718) at widgets\qabstractbutton.cpp:539
#11 0x00933865 in QAbstractButton::mouseReleaseEvent (this=0x9a0f6e8, e=0x22d3cc) at widgets\qabstractbutton.cpp:1121
#12 0x006041a0 in QWidget::event (this=0x9a0f6e8, event=0x22d3cc) at kernel\qwidget.cpp:8187
#13 0x009336f0 in QAbstractButton::event (this=0x9a0f6e8, e=0x22d3cc) at widgets\qabstractbutton.cpp:1080
#14 0x009d235e in QPushButton::event (this=0x9a0f6e8, e=0x22d3cc) at widgets\qpushbutton.cpp:683
#15 0x005b9706 in QApplicationPrivate::notify_helper (this=0x3d5500, receiver=0x9a0f6e8, e=0x22d3cc) at kernel\qapplication.cpp:4396
#16 0x005b7909 in QApplication::notify (this=0x22fe98, receiver=0x9a0f6e8, e=0x22d3cc) at kernel\qapplication.cpp:3959
#17 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x22fe98, receiver=0x9a0f6e8, event=0x22d3cc) at kernel\qcoreapplication.cpp:732
#18 0x00c2d53e in QCoreApplication::sendSpontaneousEvent (receiver=0x9a0f6e8, event=0x22d3cc) at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
#19 0x005b6118 in QApplicationPrivate::sendMouseEvent (receiver=0x9a0f6e8, event=0x22d3cc, alienWidget=0x9a0f6e8, nativeWidget=0x22fb24, buttonDown=0x105941c, lastMouseReceiver=..., spontaneous=true) at kernel\qapplication.cpp:3056
#20 0x0061fc63 in QETWidget::translateMouseEvent (this=0x22fb24, msg=...) at kernel\qapplication_win.cpp:3317
#21 0x0061aaa6 in QtWndProc@16 (hwnd=0x50238, message=514, wParam=0, lParam=15663318) at kernel\qapplication_win.cpp:1657
#22 0x77d18709 in USER32!GetDC () from E:\WINDOWS\system32\user32.dll
#23 0x00000000 in ?? ()
That what I get from debugger, its look like segmentation fault.

wysota
14th March 2011, 13:08
#3 0x0098f01f in QLineEdit::setText (this=0x0, text=...) at widgets\qlineedit.cpp:382
#4 0x00401d85 in GetData::setItemCountity (this=0x9a301f0) at getdata.cpp:12
You pass a null pointer at getdata.cpp:12

It seems you did not initialize the addressWholesaleAdd variable.

SIFE
14th March 2011, 13:27
I add this in GetData class:

private:
Ui::Store form;
And setItemcountity slot become like this:

void GetData::setItemCountity()
{
form.addressWholesaleAdd->setText("sssssssssss");
}
Now I run in some problem, and the backtrace show this:


Thread 3 (Thread 1436.0xb24):
#0 0x7c91eb94 in ntdll!LdrAccessResource () from E:\WINDOWS\system32\ntdll.dll
#1 0x7c91d85c in ntdll!ZwDelayExecution () from E:\WINDOWS\system32\ntdll.dll
#2 0x7c8023ed in SleepEx () from E:\WINDOWS\system32\kernel32.dll
#3 0x00000000 in ?? ()

Thread 2 (Thread 1436.0x6a4):
#0 0x7c91eb94 in ntdll!LdrAccessResource () from E:\WINDOWS\system32\ntdll.dll
#1 0x7c91e399 in ntdll!ZwReplyWaitReceivePortEx () from E:\WINDOWS\system32\ntdll.dll
#2 0x77e56703 in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#3 0x77e56c22 in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#4 0x77e56a3b in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#5 0x77e56c0a in RPCRT4!I_RpcBindingCopy () from E:\WINDOWS\system32\rpcrt4.dll
#6 0x7c80b50b in KERNEL32!GetModuleFileNameA () from E:\WINDOWS\system32\kernel32.dll
#7 0x00000000 in ?? ()

Thread 1 (Thread 1436.0x278):
#0 0x00d28bee in QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data (this=0xbaadf011) at ../../include/QtCore/../../src/corelib/tools/qscopedpointer.h:135
#1 0x0093da9e in qGetPtrHelper<QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> > > (p=...) at ../../include/QtCore/../../src/corelib/global/qglobal.h:2328
#2 0x00d1ebe0 in QLineEdit::d_func (this=0xbaadf00d) at ../../include/QtGui/../../src/gui/widgets/qlineedit.h:275
#3 0x0098f01f in QLineEdit::setText (this=0xbaadf00d, text=...) at widgets\qlineedit.cpp:382
#4 0x00401d85 in GetData::setItemCountity (this=0x9a301f0) at getdata.cpp:12
#5 0x00401f72 in GetData::qt_metacall (this=0x9a301f0, _c=QMetaObject::InvokeMetaMethod, _id=0, _a=0x22cc68) at debug\moc_getdata.cpp:72
#6 0x6a20445c in QMetaObject::metacall (object=0x9a301f0, cl=QMetaObject::InvokeMetaMethod, idx=4, argv=0x22cc68) at kernel\qmetaobject.cpp:237
#7 0x6a214bbe in QMetaObject::activate (sender=0x9a0f6e8, m=0xe6ece0, local_signal_index=2, argv=0x22cc68) at kernel\qobject.cpp:3272
#8 0x00bc2e3c in QAbstractButton::clicked (this=0x9a0f6e8, _t1=false) at tmp\moc\debug_shared\moc_qabstractbutton.cpp:206
#9 0x0093243b in QAbstractButtonPrivate::emitClicked (this=0x9a0f718) at widgets\qabstractbutton.cpp:546
#10 0x009323b9 in QAbstractButtonPrivate::click (this=0x9a0f718) at widgets\qabstractbutton.cpp:539
#11 0x00933865 in QAbstractButton::mouseReleaseEvent (this=0x9a0f6e8, e=0x22d3cc) at widgets\qabstractbutton.cpp:1121
#12 0x006041a0 in QWidget::event (this=0x9a0f6e8, event=0x22d3cc) at kernel\qwidget.cpp:8187
#13 0x009336f0 in QAbstractButton::event (this=0x9a0f6e8, e=0x22d3cc) at widgets\qabstractbutton.cpp:1080
#14 0x009d235e in QPushButton::event (this=0x9a0f6e8, e=0x22d3cc) at widgets\qpushbutton.cpp:683
#15 0x005b9706 in QApplicationPrivate::notify_helper (this=0x3d5500, receiver=0x9a0f6e8, e=0x22d3cc) at kernel\qapplication.cpp:4396
#16 0x005b7909 in QApplication::notify (this=0x22fe98, receiver=0x9a0f6e8, e=0x22d3cc) at kernel\qapplication.cpp:3959
#17 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x22fe98, receiver=0x9a0f6e8, event=0x22d3cc) at kernel\qcoreapplication.cpp:732
#18 0x00c2d53e in QCoreApplication::sendSpontaneousEvent (receiver=0x9a0f6e8, event=0x22d3cc) at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
#19 0x005b6118 in QApplicationPrivate::sendMouseEvent (receiver=0x9a0f6e8, event=0x22d3cc, alienWidget=0x9a0f6e8, nativeWidget=0x22fb24, buttonDown=0x105941c, lastMouseReceiver=..., spontaneous=true) at kernel\qapplication.cpp:3056
#20 0x0061fc63 in QETWidget::translateMouseEvent (this=0x22fb24, msg=...) at kernel\qapplication_win.cpp:3317
#21 0x0061aaa6 in QtWndProc@16 (hwnd=0x40566, message=514, wParam=0, lParam=16384221) at kernel\qapplication_win.cpp:1657
#22 0x77d18709 in USER32!GetDC () from E:\WINDOWS\system32\user32.dll
#23 0x00000000 in ?? ()
However if I change the call instance in Store class to this:

GetData dd;
connect(ui.addWholesaleAdd, SIGNAL(clicked()), &dd, SLOT(setItemCountity()));
My program doesn't crash but setItemCountity slot doesn't take its effect too!

unit
14th March 2011, 13:35
Why you don't use pointer to object? Look my code more attentively.

In first code you try use pointer to object that does not exist.

Now you create new object, not object that use store.

You should learn c++ basics, IMHO

wysota
14th March 2011, 13:40
Why aren't you calling setupUi() anywhere?

SIFE
14th March 2011, 13:48
You should learn c++ basics, IMHO
I am totally with you, I was PHP, C programmer.

Why aren't you calling setupUi() anywhere?
I thought I don't need it in other classes as they will have only slots to deal with database.
Now I get this error after your suggestions Mr unit:

In file included from ..\Store\/getdata.h:7,

from ..\Store\getdata.cpp:3:

..\Store\/store.h:17: error: ISO C++ forbids declaration of 'GetData' with no type

..\Store\/store.h:17: error: expected ';' before '*' token

..\Store\getdata.cpp: In constructor 'GetData::GetData(Ui::Store*)':

..\Store\getdata.cpp:7: error: 'ui' was not declared in this scope


#ifndef STORE_H
#define STORE_H

#include <QMainWindow>

#include "ui_store.h"
#include "getdata.h"

class Store : public QMainWindow
{
Q_OBJECT
public:
Store(QWidget *parent = 0);

private:
Ui::Store ui;
GetData* dd;

private slots:
};

#endif // STORE_H

unit
14th March 2011, 13:55
Show getdata.h and getdata.cpp

SIFE
14th March 2011, 14:00
#ifndef GETDATA_H
#define GETDATA_H


#include <QObject>

#include "store.h"

class QString;

class GetData : public QObject, private Ui::Store
{
Q_OBJECT
public:
GetData(Ui::Store *);
private:
Ui::Store form;

private slots:
void setItemCountity();
};

#endif // GETDATA_H



#include <QString>
#include <QLineEdit>

#include "getdata.h"

GetData::GetData(Ui::Store *mainui)
{
ui=mainui;
}

void GetData::setItemCountity()
{
form.addressWholesaleAdd->setText("sssssssssss");
}

wysota
14th March 2011, 14:05
I suggest you read about object oriented programming before you start using it. It seems your PHP skills don't include using classes too. You can't blindly pass arguments to methods just because some other class or method accepted them. Stop your copy&paste approach and start a learn&experiment approach. It is obvious you have no idea what you are doing and even if unit writes your program for you, you'll immediately come back with another similar thing. Can't you see you are both inheriting Ui::Store and having an instance of Ui::Store as a member variable? You're initializing one but using the other hence you get a crash, simple as that.

SIFE
14th March 2011, 14:12
Stop your copy&paste approach and start a learn&experiment approach.
Because I am lost now, I spent last five days reading other source codes and comments with out to figure out to fix my problem.

you'll immediately come back with another similar thing.
I don't think so, at least in similar problem because my whole project base just in this question as I know :), I think am trying to learn C++ OOP with Qt.

unit
14th March 2011, 14:20
You should known some basics of c++, such as class (object) and pointer. I'm write simple code for you and add some comment.

SIFE
14th March 2011, 14:26
I think now I understand what I miss before, and for this error:

..\Store\/store.h:17: error: ISO C++ forbids declaration of 'GetData' with no type
The solution was to declare class in the header and to include it in .cpp file as what it posted here (http://www.qtcentre.org/threads/38152-error-ISO-C-forbids-declaration-of-obj-with-no-type).

unit
14th March 2011, 14:26
Because I am lost now, I spent last five days reading other source codes and comments with out to figure out to fix my problem.

Before reading sources, you need known basic grammar and syntax of c++, or it like as read book without knowning of alphabet