PDA

View Full Version : My software crashes



Higgs
9th January 2014, 15:44
Hi again.
I was writing simple software,which have one big window. 2 Qpushbutton in this window. Parent is main window.
when mouse cursor is approaching one of the button,button is moved at the other randomly got coordinate,and so on.

I get .exe I get all neccessary .DLL files. in folder where exe is placed. Then I create folder: "platforms". There are "qwindows.dll".
Everything seems all right but when I launch .exe file,suddenly it crashes.

Then I created simple program, just big window. Did the same thing and then launched without any crashes.
I think in code I have bug,which causes Segmentation fault.
But I dont have any idea where are my mistake:

.pro

QT += widgets

HEADERS += \
dialog.h

SOURCES += \
dialog.cpp \
main.cpp


Dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include <QPushButton>
#include <QPoint>

class Dialog : public QWidget
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);

signals:
void PositionChanged();

public slots:
void getNewPosition();

private:
QPushButton *button1;
QPushButton *button2;
QPoint point1; //button1 position
QPoint point2; //button2 position
bool SecondButton;
bool FirstButton;

protected:
void mouseMoveEvent(QMouseEvent *);

};

#endif // DIALOG_H


Dialog.cpp

#include "dialog.h"
#include <QPushButton>
#include <QWidgetItem>
#include <QPoint>
#include <QKeyEvent>
Dialog::Dialog(QWidget *parent) :
QWidget(parent)
{
//main window size
setFixedSize(650,440);

//button1 related things
button1 = new QPushButton(this);
button1->setGeometry(QRect(200,200,75,23));
button1->setText("one");

//button2 related things
button2 = new QPushButton(this);
button2->setGeometry(QRect(300,200,75,23));
button2->setText("two");
point1 = button1->pos();
point2 = button2->pos();

connect(this,SIGNAL(PositionChanged()),this,SLOT(g etNewPosition()));

setMouseTracking(true);
SecondButton = true;
FirstButton = true;
}
void Dialog::getNewPosition()
{
point1 = button1->pos();
point2 = button2->pos();
}
void Dialog::mouseMoveEvent(QMouseEvent *event)
{
if(FirstButton && (( event->x() > point1.x() - 30 && event->x() < point1.x() + 105 )
&& (event->y() > point1.y() - 30 && event->y() < point1.y() + 54)))
{
//get random (x,y) coordinate
int x = rand() % 550;
int y = rand() % 400;
button1->setGeometry(QRect(x,y,75,23));
emit PositionChanged();
SecondButton = false;
}
else if(SecondButton && ((event->y() > point2.y()-30 && event->y() < point2.y()+54)
&& ( event->x() > point2.x()-30 && event->x() < point2.x() + 105)))
{
//get random (x,y) coordinate
int x = rand() % 550;
int y = rand() % 400;
button2->setGeometry(QRect(x,y,75,23));
emit PositionChanged();
FirstButton = false;
}
}


main.cpp

#include <QApplication>
#include "dialog.h"

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


Dialog dg;
dg.show();

return app.exec();
}



Any Ideas? :)

P.S sorry for my English. It isn't my native language

Santosh Reddy
9th January 2014, 16:31
Everything seems all right but when I launch .exe file,suddenly it crashes.
What is the error shown?

Higgs
9th January 2014, 18:12
What is the error shown?

9918

Then,when I look at event log:

9919

I'm working on the code and i think problem is this:

button1 = new QPushButton(this);

When I remove "this" parent. Everything works just fine :(
but without "this" I can't add buttons to mainWindow.

Added after 1 21 minutes:

I'have just commented this line:


button1->setText("one");


and it works! It doesn't crash
But Can't set text on a button :(

Lesiok
9th January 2014, 18:50
For me (Microsoft Visual C++ 2008 32 bit, Qt 4.8.5) all is working without problems. What is your compiler ?

Higgs
9th January 2014, 19:42
For me (Microsoft Visual C++ 2008 32 bit, Qt 4.8.5) all is working without problems. What is your compiler ?

Microsoft Visual C++ 2012 32 Bit.
Qt 5.1.1

P.S I take .exe from Release folder.

Santosh Reddy
10th January 2014, 06:34
Do you have multiple versions of Qt on your machine?

Higgs
10th January 2014, 16:34
Do you have multiple versions of Qt on your machine?

I think no. But At first I had many versions installed on my computer. First 4.x then 5.1 an then also 5.1(uninstaled and then installed again)
now,I have Qt 5.1.1. Seems I haven't other version of Qt.