C++ 陷入困境;分段故障“;在Qt中使用Qt creator创建一个非常简单的项目

C++ 陷入困境;分段故障“;在Qt中使用Qt creator创建一个非常简单的项目,c++,qt,qt4,C++,Qt,Qt4,以下是我的源代码,非常简单: #include<QApplication> #include<QLabel> int main(int argc,char* argv[]) { QApplication app(argc,argv); QLabel *label=new QLabel("Hello world"); label->show(); return app.exec(); } 我查了很多谷歌、stackoverflow和维基百科。

以下是我的源代码,非常简单:

#include<QApplication>
#include<QLabel>
int main(int argc,char* argv[])
{
   QApplication app(argc,argv);
   QLabel *label=new QLabel("Hello world");
   label->show();
   return app.exec();
}
我查了很多谷歌、stackoverflow和维基百科。许多人说这是由于错误使用指针造成的。但我找不出问题出在哪里。请帮忙

顺便说一下,我使用的是win7,我已经在Qt4.8.5和Qt5.4.0上试用过了。 编译器和调试器都很好(至少qt创建者没有显示错误)

我应该重新安装Qt吗

以下是调试器为我指出的错误代码(在qatomic_i386.h中):

inline bool QBasicAtomicInt::deref()
{
        unsigned char ret;
    asm volatile("lock\n"
                 "decl %0\n"
                 "setne %1"
                 : "=m" (_q_value), "=qm" (ret)
                 : "m" (_q_value)

                 : "memory");   //this is the wrong code the debugger point out for me

 return ret != 0;
}
qstring.h中的另一行:

inline QString::~QString() { if (!d->ref.deref()) free(d); }
这些都是错误的信息。谢谢

*****************代码更改***但相同******

#include<QApplication>
#include<QLabel>
#include<QDialog>

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    QDialog* dialog=new QDialog;
    QLabel* label=new QLabel(dialog);
    label->setText("Hello world");
    dialog->show();
    //label->show();    it's the same with or without this

    return app.exec();
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QDialog*dialog=新建QDialog;
QLabel*标签=新QLabel(对话框);
标签->设置文本(“你好世界”);
对话框->显示();
//label->show();有或没有这个都一样
返回app.exec();
}
***********变化更多,但不变*********

#include<QApplication>
#include<QLabel>
#include<QDialog>

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    QDialog dialog;
    QLabel label(&dialog);
    label.setText("Hello world");
    dialog.show();
    //label->show();

    return app.exec();
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QDialog对话框;
QLabel标签(&dialog);
label.setText(“Hello world”);
dialog.show();
//标签->显示();
返回app.exec();
}

请检查您的.pro文件,如果您没有尝试使用控制台应用程序,则必须使用Qwide应用程序:

#-------------------------------------------------
#
# Project created by QtCreator 2015-05-04T11:12:27
#
#-------------------------------------------------

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = testapp


TEMPLATE = app


SOURCES += main.cpp

应用程序对你的代码很有用,,,,它运行一个procces_stub.exe cmd,然后在窗口上显示标签…

我想你需要一个父主窗口来显示标签,看看文档:@Joachim Pileborg我更改了代码。请参见上文。@danielfranca我更改了代码。请参见上面的内容。@JoachimPileborg
QLabel
是一个与其他小部件类似的小部件。这样的程序做的不多,但它是完全有效的,应该运行没有任何问题(我测试过,至少在Qt4.8.4中是这样)。只显示没有窗口的QLabel应该可以工作,你确定你没有使用与编译Qt不同的编译器编译你的应用程序吗?
#-------------------------------------------------
#
# Project created by QtCreator 2015-05-04T11:12:27
#
#-------------------------------------------------

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = testapp


TEMPLATE = app


SOURCES += main.cpp