qt 開發新問題QList使用小記


QList應該是很好用的。這是理論。實際上也好用 。

我要把類放到QList能做到嗎?


class XClient
{
public:
XClient();
XClient (const XClient &xc);
// bool operator<(const XClient& c1, const XClient &c2) const;


int operator==(const XClient & c2 )const;//比較數組的相等性
// int operator!=(const XClient &)const;//比較數組的不等性
// int &operator[](int);//下標運算符

int id;

public: //站點基本信息;
XCommStyle CommStyle ;//主通訊方式
XCommStyle BakCommStyle ;//備份通訊方式
XClientStyle ClientStyle ;//自動站采集器的類型
bool bManual ;//是否有人職守站
QString Name ;//站點名稱

QString ID ;//區站號
double fX ;//台站站點的 經度

double fY ;//台站站點的 緯度

int nX ;//站點顯示的 屏幕橫坐標
int nY ;//站點顯示的 屏幕縱坐標

};

這樣一個類 ;

在頭文件里面定義 ;

class XClientGroups
{
public:
    XClientGroups();
    static     QList<XClient> *pList;
};
//cpp文件前面必須要加這個要不然編譯過不去;
  QList<XClient>* XClientGroups::pList ;
  XClient * XClientGroups::ppList ;
 
        
 
        
XClientGroups::XClientGroups()
{
    ppList=NULL;
 
        
}
通過參數把大量的類信息讀出來。加到鏈表
pList里面去,結果在別的地方利用這個類可以。反正是static類型的。要用的時候先包含頭文件 ,在直接打類名稱兩個冒號就能用這個pList了 ;
說實在的要把類加到列表里面。在程序任何地方訪問這些列表是非常常用的一個問題。
要用的時候很簡單了

int XClientGroups::mGetGroupsNames(QStringList & mlist)
{
    mlist.clear();
 
        
    if(pList==NULL || pList->count()<=0)
    {
        return 0;
    }
 
        
    QString strGroupName = "";
    int i=0; qDebug()<<pList->count();
    for(  i=0;i<pList->count();i++);
    {
        qDebug()<<"pList->count()";
        XClient xc = pList->at(i);//注意這里是全局的靜態的指針;在任何地方用這個指針不出錯。就這么搞定; 
         qDebug()<<xc.ID;
        strGroupName = xc.strGroupName;
          qDebug()<<strGroupName;
        if(!mlist.contains(strGroupName))
        {
            mlist.append(strGroupName);
 
        
        }
    }
    return mlist.size();
}


 
        
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM