Fix bug that causes random crashes.

This commit is contained in:
Black Hat 2018-03-02 19:58:55 +08:00
parent d4c2a1ed50
commit 4347755a71
5 changed files with 29 additions and 21 deletions

View File

@ -29,10 +29,7 @@ void Controller::login(QString home, QString user, QString pass) {
}
void Controller::setConnection(QMatrixClient::Connection* conn) {
qDebug() << "Setting controller connection.";
m_connection = conn;
roomListModel = new RoomListModel(m_connection);
emit roomListModelChanged();
connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected);
connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect);
connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect);

View File

@ -14,7 +14,6 @@ class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(RoomListModel *roomListModel READ getRoomListModel NOTIFY roomListModelChanged)
Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged)
Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
@ -31,9 +30,6 @@ public:
// All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs.
RoomListModel* roomListModel;
RoomListModel* getRoomListModel() { return roomListModel; }
QMatrixClient::Connection* m_connection;
QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* conn);
@ -71,7 +67,6 @@ private:
void reconnect();
signals:
void roomListModelChanged();
void connectionChanged();
void isLoginChanged();
void userIDChanged();

View File

@ -5,7 +5,16 @@
#include "controller.h"
RoomListModel::RoomListModel(QMatrixClient::Connection* m_connection) : m_connection(m_connection) {
RoomListModel::RoomListModel() {
}
RoomListModel::~RoomListModel() {
}
void RoomListModel::setConnection(QMatrixClient::Connection *conn) {
m_connection = conn;
beginResetModel();
m_rooms.clear();
connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom);
@ -16,10 +25,6 @@ RoomListModel::RoomListModel(QMatrixClient::Connection* m_connection) : m_connec
endResetModel();
}
RoomListModel::~RoomListModel() {
}
QMatrixClient::Room* RoomListModel::roomAt(int row) {
return m_rooms.at(row);
}

View File

@ -16,14 +16,20 @@ class RoomListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged)
public:
explicit RoomListModel(QMatrixClient::Connection* m_connection = 0);
explicit RoomListModel();
~RoomListModel();
enum RoomModelRoles {
NameRole, ValueRole, AvatarRole
};
QMatrixClient::Connection* m_connection;
QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* conn);
QHash<int, QByteArray> roleNames() const;
Q_INVOKABLE QMatrixClient::Room* roomAt(int row);
@ -32,6 +38,7 @@ public:
Q_INVOKABLE int rowCount(const QModelIndex& parent=QModelIndex()) const override;
signals:
void connectionChanged();
public slots:
@ -41,7 +48,6 @@ private slots:
void addRoom(QMatrixClient::Room* room);
private:
QMatrixClient::Connection* m_connection;
QList<QMatrixClient::Room*> m_rooms;
};

View File

@ -22,12 +22,17 @@ ApplicationWindow {
connection: m_connection
}
// Settings {
// id: settings
RoomListModel {
id: roomListModel
connection: m_connection
}
// property var userID
// property var token
// }
Settings {
id: settings
property alias userID: matrixController.userID
property alias token: matrixController.token
}
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
@ -50,7 +55,7 @@ ApplicationWindow {
page: Room {
id: roomPage
roomListModel: matrixController.roomListModel//BUG: It will cause random crash as roomListModel may not be initialized.
roomListModel: roomListModel
}
}