1

编译成功,但是当我运行它时,终端给了我“分段错误(核心转储)”消息。我使用的编译器是 Ubuntu 上的 g++。

代码是:

#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QWidget>

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

  QWidget window;
  QLabel *label = new QLabel;
  QLineEdit *edit = new QLineEdit;
  QObject::connect(edit, SIGNAL(textChanged(const QString&)), label, SLOT(setText(const QString&)));

  QVBoxLayout *layout;
  layout->addWidget(edit);
  layout->addWidget(label);
  window.setLayout(layout);

  window.show();


  return app.exec();
}
4

1 回答 1

2

QVBoxLayout *layout未初始化,您使用的是未初始化的指针。

正确方法:

QVBoxLayout *layout = new QVBoxLayout;
// use layout..

http://qt-project.org/doc/qt-4.8/qvboxlayout.html

于 2014-04-08T22:36:40.500 回答