- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 QtCreator 创建一个小型应用程序。它可以编译,但是从 QtCreator 执行时出现“程序意外完成”错误。
如果我尝试从控制台执行二进制文件,我会收到段错误(核心已转储)。
因为这是我第一次自己编写 Qt 代码,所以我想我遗漏了一些东西。请检查以下代码:
标题 mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow;
class QLabel;
class QLineEdit;
class QPushButton;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void createGUI();
private:
QLineEdit *mysqlUserLineEdit;
QLineEdit *mysqlPasswordLineEdit;
QLineEdit *albatrossIPLineEdit;
QLineEdit *albatrossPortLineEdit;
QPushButton *exitButton;
QPushButton *startButton;
};
#endif // MAINWINDOW_H
来源主窗口.cpp:
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
createGUI();
//connect(...)
//connect(...)
setWindowTitle(tr("Albatross MySQL simulator"));
}
MainWindow::~MainWindow()
{
}
void MainWindow::createGUI()
{
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *settingsLayout = new QHBoxLayout;
QVBoxLayout *mysqlSettingsLayout = new QVBoxLayout;
QHBoxLayout *mysqlUserLayout = new QHBoxLayout;
mysqlUserLineEdit = new QLineEdit();
QLabel *mysqlUserLabel = new QLabel(tr("&User:"));
mysqlUserLabel->setBuddy(mysqlUserLineEdit);
mysqlUserLayout->addWidget(mysqlUserLabel);
mysqlUserLayout->addWidget(mysqlUserLineEdit);
QHBoxLayout *mysqlPasswordLayout = new QHBoxLayout;
mysqlPasswordLineEdit = new QLineEdit();
QLabel *mysqlPasswordLabel = new QLabel(tr("&Password:"));
mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);
mysqlSettingsLayout->addLayout(mysqlUserLayout);
mysqlSettingsLayout->addLayout(mysqlPasswordLayout);
QVBoxLayout *networkSettingsLayout = new QVBoxLayout;
QHBoxLayout *albatrossIPLayout = new QHBoxLayout;
albatrossIPLineEdit = new QLineEdit();
QLabel *albatrossIPLabel = new QLabel(tr("&IP:"));
albatrossIPLabel->setBuddy(albatrossIPLineEdit);
albatrossIPLayout->addWidget(albatrossIPLabel);
albatrossIPLayout->addWidget(albatrossIPLineEdit);
QHBoxLayout *albatrossPortLayout = new QHBoxLayout;
albatrossPortLineEdit = new QLineEdit();
QLabel *albatrossPortLabel = new QLabel(tr("P&ort:"));
albatrossPortLabel->setBuddy(albatrossPortLineEdit);
albatrossPortLayout->addWidget(albatrossPortLabel);
albatrossPortLayout->addWidget(albatrossPortLineEdit);
networkSettingsLayout->addLayout(albatrossIPLayout);
networkSettingsLayout->addLayout(albatrossPortLayout);
settingsLayout->addLayout(mysqlSettingsLayout);
settingsLayout->addLayout(networkSettingsLayout);
QHBoxLayout *buttonsLayout = new QHBoxLayout;
exitButton = new QPushButton(tr("&Exit"));
buttonsLayout->addWidget(exitButton);
startButton = new QPushButton(tr("&Start"));
startButton->setDefault(true);
buttonsLayout->addWidget(startButton);
mainLayout->addLayout(settingsLayout);
mainLayout->addLayout(buttonsLayout);
centralWidget()->setLayout(mainLayout);
}
最后是 main.cpp,它是用 QtCreator 自动生成的:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
编辑:好的,问题是在使用 mainLayout 将其附加到 mainWindow 时,以及 mainwindow.cpp 的最后一行。这就是它抛出段错误的地方。我应该将什么设置为中央小部件?或者有没有其他方法可以将布局附加到主窗口小部件?
最佳答案
一般来说,creator 中的这种行为是由于 SEGFAULT 或缺少库造成的。
你的代码
mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);
是原因。您没有初始化导致 SEGFAULT 的 mysqlPasswordLineEdit
关于c++ - Qt 程序不工作 : The program has inexpectedly finished,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618563/
这两者有区别吗? startActivity(intent); finish(); startActivity(intent); ThisActivityName.this.finish(); 注意:
我有一个包含大量 url 链接的 arrayOfLinks。我需要从这些链接中获取图像。我正在使用以下代码来执行此操作。 - (void)getImages { NSArray *links
我想创建一条执行以下步骤的路线 读取文件 将文件分割成行 使用 BeanIO 解码行 调用(昂贵且耗时的)处理器 我想在处理完所有线路后完成这条路线。 所以我的路线的基本布局如下所示: from("d
我正在使用 Nodejs。我有一个异步的 forEach,因为我必须在 forEach 中等待结果。结果,我需要等待 forEach 完成,然后继续处理循环的结果。我找到了几种等待 forEach 的
我正在编写需要背靠背内核调用的 android renderscript 代码(有时一个内核的输出成为另一个内核的输入)。我还有一些全局指针,绑定(bind)到 Java 层的内存。每个内核更新那些全
伪代码: void draw() { Vertex* vertices = scene.GetVertexArray(); glEnableClientState(...);
当我创建一个 Android 应用程序时,我首先在其中创建了主 Activity ,然后添加了启动 Activity 以及一个正常 Activity 。 所以我的问题是每当我在应用程序中单击退出时,它
我正在尝试将一些 API 数据获取到我的应用程序,并且我有两个 Activity 。第一个是启动屏幕(就像加载应用程序时谷歌使用的那样),我想知道在哪里调用 finish() 来结束 Activity
我使用 创建了“新文件” Activity startActivityForResult(new Intent(MainActivity.this, NewFile.class),1); NewFil
我有一个以 SingleTask 模式 (Android 2.2) 运行的 Activity ,该 Activity 从其他 Activity 或服务接收 Intent 。我的 Activity 将每
我编写了以下代码来使用 TextView 和按钮显示消息框。单击按钮时, Activity 应该完成。但这有时会起作用,但并不一致。有时它会挂起。如何让它始终如一地工作?请帮忙 public clas
我正在搜索,当 onDestroy 在 android 应用程序上执行时,我发现它是在设备资源不足(RAM、CPU) 时执行的,并且当用户调用 finish() 时。 例如,当我按下后退按钮从一个 A
我正在编写一个 Android 应用程序,它显示一个带有一些元素和一些 CRUD 操作的 RecyclerView。我想做的一件事是从我的 RecyclerView 更新现有元素,为此我打开一个新 A
被测试的 Activity 是一个简单的棋盘游戏。当满足游戏结束的必要条件时,在执行几项操作以显示谁赢了等之后,将运行以下代码: // Return to main menu after 5 seco
我有一个 Activity ,它创建了我创建的名为classA的类的新实例。在classA中,我有一个方法将对象添加到位于classA中的STATICarrayList。 所以我的 Activity
我正在使用 Facebook Android SDK,并希望在用户登录并获取用户对象后关闭我的 Activity。实际上,我存储了它的一部分,但无论如何我都想关闭该 Activity 。
我以前从事c# dotnet开发,有一个Application.exit()类型的方法,可以立即关闭应用程序并释放内存。 我发现 android 的“finish()”方法应该这样做 但它没有这样做,
我有一个 Activity ,我调用了 finish() 方法,但 Activity 没有从内存中清除。 调用 finish() 后,我看到方法 onDestroy() 已成功执行(并且我清除了其中的
我想在我的MKMapView上设置一个区域,然后找到与 map 的东北角和西南角对应的坐标。 This code works just fine to do that: //Recenter and
我正在尝试使用从顶部滑动动画将一个 View 替换为另一个 View 。我有点遵循 http://gentlebytes.com/2011/09/auto-layout-in-lion/ 中发布的指南
我是一名优秀的程序员,十分优秀!