Qt:崩溃 - 它是代码还是我需要插入一些其他命令?

时间:2016-06-16 06:40:44

标签: xdebug

当我运行以下代码时,它会抛出错误

The inferior stopped because it received a signal from the operating system.

Signal name: SIGSEGV

Signal meaning: Segmentation fault

这是我的代码,我想知道我哪里出错了? 我想在窗口上显示一些随机块,但它失败了 mainwindow.cpp

#include "mainwindow.h"
#include <ctime>
#include <QPixmap>
#include <QPainter>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setFixedSize(win_width,win_height);
    this->setWindowIcon(QIcon(":/Image/icon.png"));
    this->setWindowTitle("Qt POPONG");

    startpage = new StartPage(this);

    for (int i=0; i<80; i++){
        block_[i] = new block(this);
    }

    gameRedy();
    startpage->show();
}
void MainWindow::paintEvent(QPaintEvent *)      
{
    QPainter painter(this);
    QPixmap bgImg;
    bgImg.load(":/Image/bg.png");

    painter.drawPixmap(0,0,win_width,win_height,bgImg);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{

}
void MainWindow::keyPressEvent(QKeyEvent *event)
{

    if (event->key()==Qt::Key_S) {
        gameStart();
        startpage->close();
    }
}

void MainWindow::createBlocks()
{
    int startx = 0, starty = 0;
    int WW;     
    int HH;     
    qsrand(time(NULL));

    for (int i=0; i<blockcount; i++){
        while(1){
            WW = qrand()%10;
            HH = qrand()%12;
            if (blockShowW[WW]==0 && blockShowH[HH]==0){
                block_[i]->move(startx+WW*50, starty+HH*50);
                blockShowW[WW]=1;
                blockShowH[HH]=1;
                break;
            }
        }
    }

    blockTimer = new QTimer(this);
    //connect(blockTimer,SIGNAL(timeout()),this,SLOT(blockAction()));
    blockTValue = 8;

}
/*void MainWindow::blockAction()
{
    int startx = 0, starty = 0;
    int WW;     
    int HH;     
    for (int i=0; i<blockcount; i++){
        while(1){
            WW = qrand()%10;
            HH = qrand()%12;
            if (blockShowW[WW]!=0 && blockShowH[HH]!=0){
                block_[i]->move(startx+WW*50, starty+HH*50);
                blockShowW[WW]=1;
                blockShowH[HH]=1;
                break;
            }
        }
    }
}*/

void MainWindow::gameRedy()
{
    gamemod=redy;
    createBlocks();
}
void MainWindow::gameLose()
{
    gamemod=lose;
}
void MainWindow::gameStart()
{
    gamemod=start;
    //blockTimer->start(blockTValue);
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QApplication>
#include <QMouseEvent>
#include <QTimer>

#include "startpage.h"
#include "plate.h";
#include "block.h";

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    int win_width=500;     
    int win_height=700;     

protected:
    void paintEvent(QPaintEvent *);    
    void mousePressEvent(QMouseEvent *event);   
    void keyPressEvent(QKeyEvent *event);   

private slots:
    //void blockAction();

private:
    void gameLose();            
    void gameStart();            
    void gameRedy();             

    void createBlocks();
    enum{lose=0,start=1,redy=2};  
    int gamemod;        
    StartPage *startpage; 

    enum{blockcount=10};
    block *block_[blockcount];
    QTimer *blockTimer;
    int blockTValue;
    int blockShowW[11]={0};     
    int blockShowH[13]={0};     
};

#endif // MAINWINDOW_H

block.cpp

#include "block.h"

block::block(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(W,H);
    this->color = (qrand()%7)+1;
}
void block::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap object;

    switch(this->color){
    case 1:
        object.load(":/Image/blocks/1.png");
        break;
    case 2:
        object.load(":/Image/blocks/2.png");
        break;
    case 3:
        object.load(":/Image/blocks/3.png");
        break;
    case 4:
        object.load(":/Image/blocks/4.png");
        break;
    case 5:
        object.load(":/Image/blocks/5.png");
        break;
    case 6:
        object.load(":/Image/blocks/6.png");
        break;
    default:
        object.load(":/Image/blocks/7.png");
        break;
        }
    painter.drawPixmap(0,0,W,H,object);
}

block.h

#ifndef BLOCK_H
#define BLOCK_H

#include <QWidget>
#include <QPainter>
#include <QPixmap>

class block : public QWidget
{
    Q_OBJECT
public:
    explicit block(QWidget *parent = 0);
    int W = 50;
    int H = 50;

private:
    int color;

protected:
    void paintEvent(QPaintEvent *);
};

#endif // BLOCK_H

0 个答案:

没有答案