PDA

View Full Version : Unexplained Crash



jbkc85
12th March 2009, 19:54
Hi all, I am fairly new at QT and have downloaded the new SDK and have the latest QT libraries (which i believe is 4.5?). I wanted to make a quick widget to allow me to run a test program, showing simple LCD and displays, however whenever I run it the QT program crashes on me, with no error codes. Anyway, here is the code...

************************************************** **************

//#include <QtCore/QCoreApplication>

#include <QApplication>
#include <QLCDNumber>
#include <QPushButton>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>

#include "digitalclock.h"
#include <stdlib.h>
#include <stdio.h>

#define userMinTemp 15

char display[10];
int newTemp = 0;

/* Widget Class Creator */
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);

public slots:
void MyWidget::newTempMain();
};
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 12, QFont::Bold));

QLCDNumber *lcd = new QLCDNumber(newTemp);
lcd->setSegmentStyle(QLCDNumber::Filled);

QPushButton *devDisplay = new QPushButton(display);
devDisplay->setFont(QFont("Times",18,QFont::Bold));

//DigitalClock clock;

// Quit when clicked Quits...
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
// devDisplay when clicked creates a new temp and says whether or not ac/heat
connect(devDisplay, SIGNAL(clicked()), this, SLOT(newTemp()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(devDisplay);
layout->addWidget(lcd);
layout->addWidget(quit);
//layout->addWidget(clock);
setLayout(layout);

}

void MyWidget::newTempMain()
{
newTemp = (rand()%101);

if( newTemp <= userMinTemp )
{
strncpy("Heat", display, sizeof(display));
//display = "Heat";
}
else
{
strncpy("AC", display, sizeof(display));
//display = "AC";
}
}

void newTempMain()
{
newTemp = (rand()%101);

if( newTemp <= userMinTemp )
{
strncpy("Heat", display, sizeof(display));
//display = "Heat";
}
else
{
strncpy("AC", display, sizeof(display));
//display = "AC";
}
}

int main(int argc, char *argv[])
{
newTempMain();

QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

************************************************** ***************

If there are any suggestions or anything that anyone sees out of the ordinary or that causes potential crashing, please let me know...its buggin the living hell outta me.

Thanks!

spirit
12th March 2009, 20:06
first of all you need to add Q_OBJECT marco right here


class MyWidget : public QWidget
{
Q_OBJECT
...

if you don't do this then signal/slot mechanism will not work.

then there is no needed to use strncpy, QString already has all what you need for working with strings.

are these variables quit, lcd & devDisplay declared as class member, ?
if not then this can cause a crash, because in ctor you have initialized local variables.

PS. use tag code please.