I understand that I cannot execute the beginInsertRows() and endInsertRows() since they are not public but instead protected. It would make sense for me to allow inserting empty objects and if I need to, I will remove the built-in data validity checking that my classes currently have. Right now, as an example, my Net class stores an IP address, Netmask, and it checks to make sure it is not a duplicate IP or an invalid IP. I guess I would need to either handle an exception or move the validity checking code out into Qt and utilize its classes like QRegExp...
The other approach that you mention which I have also considered but do not know enough about how Qt creates its QModelIndexes is to not use insertRows() and provide my own function. Here is how I think it might work. Below is a code snippet that will help me explain:
bool BUSModel::insertRows(int row, int count, const QModelIndex& parent) {
beginInsertRows(parent, row, count);
BUSDataMoverItem tmpnet10('L', "NetworkTWELVE", "192.168.12.0", "255.255.255.0", 310000000);
endInsertRows();
return true;
}
bool BUSModel::insertRows(int row, int count, const QModelIndex& parent) {
beginInsertRows(parent, row, count);
BUSDataMoverItem tmpnet10('L', "NetworkTWELVE", "192.168.12.0", "255.255.255.0", 310000000);
endInsertRows();
return true;
}
To copy to clipboard, switch view to plain text mode
Above, I pass the parent, the row (I use rowCount() to append to end of child list.), and the count as 1. I start the beginInsertRows(), add the row to my custom data structure, and then execute an endInsertRows(). The BUSDataMoverItem is a temporary handle that I use to create pointers to BUSDataMover objects. I strip the handle off and use a BUSDataMover* in my child list for each object. Above pass the root item of the tree as the parent and do a append to its child list. Here is the question. Does the model look at the row I am inserting, create an internalPointer to this appended child, create a model index, and update the view? The only thing I have not done is 'emit dataChanged()'
Do I need to somehow utilize the createIndex() function in my custom insert function? Could the problem be that I really don't have a container where my objects live, I only track them by pointers to the free-store (new/delete).
This is my base class for the custom data structure and how I establish the hierarcy:
//=========================== BUSDataMover Abstract Class ================================
// The base BUSDataMover class
class BUSDataMover {
friend class BUSNetworkSupervisor;
friend class BUSDataMoverItem;
friend ostream& operator<<(ostream&, const BUSDataMover*);
public:
BUSDataMover(const string& n, const string& i, BUSDataMover* parent = 0);
BUSDataMover() : mName(""), mIsA(""), qParent(0) {}
virtual ~BUSDataMover();
const string& getName() const { return mName; }
void setName(string& name) { mName = name; }
const int getThruput() { return mThruput; }
virtual void setThruput(int tpkb);
virtual BUSNetId& getNet(int n=0) { }
virtual const string& getIpString(int n=0) { }
virtual const string& getNetmaskString(int n=0) { }
virtual void setIp(const string&, int nn=0) { }
virtual void setnm(const string&, int nn=0) { }
virtual BUSDataMover* addStgV(int, int) { }
virtual BUSDataMover* addNIC(const string&, const string&, int tpkb=0) { }
virtual void handleBUSNetworkMessage(BUSNetworkMessage& m);
virtual map<BUSNetId, BUSDataMover*>& getNodeList() { }
virtual int findCost(const BUSNetId&) { }
virtual void setIn(BUSDataMover* i) { mIn=i; }
virtual void setOut(BUSDataMover* o) { mOut=o; }
BUSDataMover* getIn() { return mIn; }
BUSDataMover* getOut() { return mOut; }
void setIsA(string& isa) { mIsA=isa; }
string& getIsA() { return mIsA; }
virtual map<BUSNetId, BUSDataMover*>::iterator findNode(const BUSNetId&) { }
virtual void addNode(const BUSNetId&, BUSDataMover*, int cost=0) { }
virtual map<BUSNetId, BUSDataMover*>::iterator nodeListEnd() { }
virtual map<BUSNetId, BUSDataMover*>::iterator nodeListBegin() { }
protected:
virtual void print(ostream& o) const;
//============================== Qt members ======================================
public:
BUSDataMover* parent() { return qParent; }
void setParent(BUSDataMover* p) { qParent=p; }
void addChild(BUSDataMover* c) { qChildItems.append(c); }
virtual int columnCount();
virtual BUSDataMover* child(int row);
bool setData
(int column,
QVariant data
);
virtual int row();
int childCount() { return qChildItems.count(); }
int getIconType() { return qIcon; }
protected:
// The destruction process will utilize this to ensure killing all children.
QList<BUSDataMover*> qChildItems;
BUSDataMover* qParent;
// Icon
int qIcon;
//============================ End Qt members ====================================
private:
string mName;
int mThruput;
string mIsA;
BUSDataMover* mIn;
BUSDataMover* mOut;
};
//=========================== BUSDataMover Abstract Class ================================
// The base BUSDataMover class
class BUSDataMover {
friend class BUSNetworkSupervisor;
friend class BUSDataMoverItem;
friend ostream& operator<<(ostream&, const BUSDataMover*);
public:
BUSDataMover(const string& n, const string& i, BUSDataMover* parent = 0);
BUSDataMover() : mName(""), mIsA(""), qParent(0) {}
virtual ~BUSDataMover();
const string& getName() const { return mName; }
void setName(string& name) { mName = name; }
const int getThruput() { return mThruput; }
virtual void setThruput(int tpkb);
virtual BUSNetId& getNet(int n=0) { }
virtual const string& getIpString(int n=0) { }
virtual const string& getNetmaskString(int n=0) { }
virtual void setIp(const string&, int nn=0) { }
virtual void setnm(const string&, int nn=0) { }
virtual BUSDataMover* addStgV(int, int) { }
virtual BUSDataMover* addNIC(const string&, const string&, int tpkb=0) { }
virtual void handleBUSNetworkMessage(BUSNetworkMessage& m);
virtual map<BUSNetId, BUSDataMover*>& getNodeList() { }
virtual int findCost(const BUSNetId&) { }
virtual void setIn(BUSDataMover* i) { mIn=i; }
virtual void setOut(BUSDataMover* o) { mOut=o; }
BUSDataMover* getIn() { return mIn; }
BUSDataMover* getOut() { return mOut; }
void setIsA(string& isa) { mIsA=isa; }
string& getIsA() { return mIsA; }
virtual map<BUSNetId, BUSDataMover*>::iterator findNode(const BUSNetId&) { }
virtual void addNode(const BUSNetId&, BUSDataMover*, int cost=0) { }
virtual map<BUSNetId, BUSDataMover*>::iterator nodeListEnd() { }
virtual map<BUSNetId, BUSDataMover*>::iterator nodeListBegin() { }
protected:
virtual void print(ostream& o) const;
//============================== Qt members ======================================
public:
BUSDataMover* parent() { return qParent; }
void setParent(BUSDataMover* p) { qParent=p; }
void addChild(BUSDataMover* c) { qChildItems.append(c); }
virtual int columnCount();
QVariant data(int column);
virtual BUSDataMover* child(int row);
bool setData(int column, QVariant data);
virtual int row();
int childCount() { return qChildItems.count(); }
int getIconType() { return qIcon; }
protected:
// The destruction process will utilize this to ensure killing all children.
QList<BUSDataMover*> qChildItems;
BUSDataMover* qParent;
// Icon
int qIcon;
//============================ End Qt members ====================================
private:
string mName;
int mThruput;
string mIsA;
BUSDataMover* mIn;
BUSDataMover* mOut;
};
To copy to clipboard, switch view to plain text mode
Bookmarks