为什么我觉得Qt在windows下各种问题呢?

在写一个很简单的实验程序,我想实现的无非是,当我点击next button时,中央的label里面的内容发生变化,不知道为什么编译能通过,但是无法实现,当我点击next button时还报Exception at 0xxxxxxxx, code: 0xc0000005: read access violation at: 0x0, flags=0x0

图片描述

下面贴上相关代码:


/*experiment.h*/

#define initial_state 0

#define start_state 1

#define detect_state 2

#define pause_state 3

#define final_state 4

#pragma comment(lib, "Qt5Widgets.lib")

class Experiment:public QDialog{

Q_OBJECT

public:

Experiment(QWidget *parent=0);

protected:

void closeEvent(QCloseEvent *event);

private slots:

void ButtonFunction();

private:

//Thread cv;

//Thread mind;

QLabel *direction;

QPushButton *next;

int state;

};

#endif // EXPERIMENT_H


/*experiment.cpp*/

#include "experiment.h"

Experiment::Experiment(QWidget *parent): QDialog(parent){

state=initial_state;

QFont font("Courier",48,75,0);

direction=new QLabel(tr("This is a experiment"),this);

direction->setGeometry(450,400,760,73);

direction->setFont(font);

next=new QPushButton(tr("Next"),this);

next->setGeometry(1500,850,71,21);

next->setDefault(true);

connect(next, SIGNAL(clicked()), this, SLOT(ButtonFunction()));

}

void Experiment::ButtonFunction(){

switch(state){

case initial_state:

//TODO:

next->setText(tr("please do something change"));//完全不起作用

direction->setGeometry(450,400,1400,73);**

break;

case start_state:

//TODO:

break;

case detect_state:

//TODO:

break;

case pause_state:

//TODO:

break;

case final_state:

//TODO:

break;

}

}

回答:

用Qt Creator建立一个Qt空项目 把你这个源码拷进去根本编译不过啊

单看代码的问题是

1.没有include对应的头文件,估计是截取一部分所致。还有一个莫名其妙的#endif

2.没有main函数,同上

3.不知道#pragma的意义,因为要链接的lib用pro文件可以配置,没必要显示写出来

4.next->setText(tr("please do something change"));//完全不起作用 应该是direction->setText(tr("please do something change"));

稍微改了一下,使用Qt Creator,在Qt 4.8.5下编译运行没问题

/*experiment.h*/

#pragma once

#include <QDialog>

#include <QPushButton>

#include <QLabel>

#define initial_state 0

#define start_state 1

#define detect_state 2

#define pause_state 3

#define final_state 4

class Experiment:public QDialog{

Q_OBJECT

public:

Experiment(QWidget *parent=0);

private slots:

void ButtonFunction();

private:

//Thread cv;

//Thread mind;

QLabel *direction;

QPushButton *next;

int state;

};


/*experiment.cpp*/

#include "experiment.h"

Experiment::Experiment(QWidget *parent): QDialog(parent){

state=initial_state;

QFont font("Courier",48,75,0);

direction=new QLabel(tr("This is a experiment"),this);

direction->setGeometry(450,400,760,73);

direction->setFont(font);

next=new QPushButton(tr("Next"),this);

next->setGeometry(1500,850,71,21);

next->setDefault(true);

connect(next, SIGNAL(clicked()), this, SLOT(ButtonFunction()));

}

void Experiment::ButtonFunction(){

switch(state){

case initial_state:

//TODO:

direction->setText(tr("please do something change"));//完全不起作用

direction->setGeometry(450,400,1400,73);

break;

case start_state:

//TODO:

break;

case detect_state:

//TODO:

break;

case pause_state:

//TODO:

break;

case final_state:

//TODO:

break;

}

}


#include "experiment.h"

#include <QApplication>

Q_DECL_EXPORT int main(int argc, char *argv[])

{

QApplication app(argc, argv);

Experiment viewer;

viewer.show();

return app.exec();

}

以上是 为什么我觉得Qt在windows下各种问题呢? 的全部内容, 来源链接: utcz.com/p/191471.html

回到顶部