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) { void Controller::setConnection(QMatrixClient::Connection* conn) {
qDebug() << "Setting controller connection.";
m_connection = conn; m_connection = conn;
roomListModel = new RoomListModel(m_connection);
emit roomListModelChanged();
connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected); connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected);
connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect); connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect);
connect(m_connection, &QMatrixClient::Connection::syncError, 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_OBJECT
Q_PROPERTY(RoomListModel *roomListModel READ getRoomListModel NOTIFY roomListModelChanged)
Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged) Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged)
Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged) Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
@ -31,9 +30,6 @@ public:
// All the non-Q_INVOKABLE functions. // All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
RoomListModel* roomListModel;
RoomListModel* getRoomListModel() { return roomListModel; }
QMatrixClient::Connection* m_connection; QMatrixClient::Connection* m_connection;
QMatrixClient::Connection* getConnection() { return m_connection; } QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* conn); void setConnection(QMatrixClient::Connection* conn);
@ -71,7 +67,6 @@ private:
void reconnect(); void reconnect();
signals: signals:
void roomListModelChanged();
void connectionChanged(); void connectionChanged();
void isLoginChanged(); void isLoginChanged();
void userIDChanged(); void userIDChanged();

View File

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

View File

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

View File

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