新手问题 qt 应用程序崩溃

myy1994 2016-03-03 04:29:16
json.h 头文件
#ifndef JSON_H
#define JSON_H
#include <QJsonParseError>
#include <QJsonObject>
#include <QStringList>

class Json
{
public:
QStringList Json1(QJsonDocument _jsonDocument);
QStringList Json2();
QStringList objlist1;
private:
QJsonObject obj;
QStringList obj2_ports;
QJsonObject obj2;
QStringList objlist;
QJsonValue obj2_hn;
QString hn;
QJsonValue obj2_port;
double port_1;
QString port;
};
#endif // JSON_H



mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QStandardItemModel>
#include <QStandardItem>
#include <QMainWindow>
#include <QtNetwork>
#include "json.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QStandardItemModel *model1;
QStringList obj2_key;
QStringList obj2_hostnames;
QStringList obj2_ports;
QString hn;
QString port;
QStringList hnl;
QAbstractItemModel *mod;
Json b;

};

#endif // MAINWINDOW_H



main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Json b;
return a.exec();
}


json.cpp
#include "json.h"
QStringList Json::Json1(QJsonDocument _jsonDocument)
{
if (_jsonDocument.isObject())
{
obj = _jsonDocument.object();//Qt5新类
objlist=obj.keys();
for(int i=0;i<objlist.count();i++)
{
obj2=obj.value(objlist.at(i)).toObject();
obj2_hn = obj2.take("hostname");
hn = obj2_hn.toString();
objlist1<<hn;
obj2_port = obj2.take("port");
port_1 = obj2_port.toDouble();
port = QString::number(port_1,'f',0);
obj2_ports<<QString(port);
}
}
return objlist;
}
QStringList Json::Json2()
{
return objlist1;
}



mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringListModel>
#include <QStandardItem>
#include "json.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString json("{\"urn:uuid:99e68065-39b6-cd88-b8ea-001314151697\": {\"hostname\": \"128.0.111.167\",\"port\": 80,\"path\": \"/onvif/device_service\",\"events\": {}}}");
model1 = new QStandardItemModel(ui->treeView);
model1->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("hostname")<<"port");//添加表头
QJsonParseError error;
QJsonDocument jsonDocument = QJsonDocument::fromJson(json.toLatin1(), &error);
if (error.error == QJsonParseError::NoError)
{
Json b; // 就是这里出错
QStringList objlist = b.Json1(jsonDocument);
QStringList hnl=b.Json2();
for(int a=0;a<objlist.count();a++)
{
qDebug()<<hnl.at(a);
QStandardItem* itemProject = new QStandardItem(hnl.at(a));
model1->appendRow(itemProject);
}
ui->treeView->setModel(model1);
}
else {
qFatal(error.errorString().toUtf8().constData());
exit(1);
}

}
MainWindow::~MainWindow()
{
delete ui;
}


各位大神啊 ,代码如上,编译通过,但运行会崩溃。出错的地方是自定义的类Json.应该怎么改啊?本人新手 啊。
...全文
848 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你还是给下code,不然让人看得真头疼。
gongzf111 2017-06-19
  • 打赏
  • 举报
回复
引用 1 楼 foruok 的回复:
调试,debug版本调试一下就知道问题在哪里了。
大神 我的程序在debug模式运行时 跑一段时间就奔溃 指在return a.exec();这里。。 请问这是什么原因造成的??内存泄漏么???
yh2711524 2016-05-04
  • 打赏
  • 举报
回复
好像编译运行也没问题啊
yh2711524 2016-05-04
  • 打赏
  • 举报
回复
你构造函数在哪
foruok 2016-03-04
  • 打赏
  • 举报
回复
调试,debug版本调试一下就知道问题在哪里了。

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧