[SOLVED]myWindow.exe exited with code -1073741819
-
Hi all,
I am trying to use a QListwidget so that when I click on it ,the text in the items changes accordingly.The header code is as follows
@#include <QWidget>
#include <QListWidget>
#include <QPushButton>
#include <QVBoxLayout>
class MyWindow:public QWidget
{
Q_OBJECT
public:
MyWindow();public slots:
void processItems();private:
QListWidget *mWdgt;
};
@and the implementation code is as follows:
@MyWindow::MyWindow()
{QListWidget *mWdgt=new QListWidget(this);
mWdgt->addItem("inka");
mWdgt->addItem("intama");
mWdgt->addItem("ihene");
mWdgt->addItem("inka");QPushButton *button1=new QPushButton("Click Me",this);
QVBoxLayout *myVLayout=new QVBoxLayout;
myVLayout->addWidget(mWdgt);
myVLayout->addWidget(button1);setLayout(myVLayout);
QObject::connect(button1, SIGNAL(clicked()),
this,SLOT(processItems()));}
void MyWindow::processItems()
{
mWdgt->currentItem()->setText("amahoro");}@
The code compiles nice but when i run it he following message pops up after a windows error message."exe exited with code -1073741819"
Any help would be appreciated since this has been bugging me for some time.When I start from the Gui project
it works.I just want to be able to do it from raw code.Thanks.
-
mWdgt you create in the constructor (line 4) is a local variable, in no way connected to private mWdgt declared in the header and modified in the ::processItems() slot.
Add this to your constructor. I suggest rethinking the rest of the design a bit, too.
@
this->mWdgt = mWdgt;
@Just to be clear: the reason your app crashes is that currently the mWdgt pointer in the processItems() is a dangling pointer (not initialised).
-
Thanks sierdzio ,I did as you said and it worked.I am learning c++ and exploring the concepts of classes using Qt.Thanks again!
-
Sure thing, have fun and ask more if something is not clear :)