Object Inherit from QTreeWidgetItem
Hi,
i'm making a QTreeWidget that uses an object that inherits from QTreeWidgetItem.
the code is:
Object.h
Code:
#pragma once
#include <QPixmap>
#include <QDir>
#include <QFile>
#include <QString>
#include <QTreeWidgetItem>
#include "Resources.h"
public:
void init();
bool isObject(){return (m_pixmap!=NULL);};
void setPixmap
(QString file=RES_FOLDER_22x22
);
private:
};
Object.cpp
Code:
#include "Object.h"
Object
::Object(QString file,
QString fileInfo
):m_file
(file),m_fileInfo
(fileInfo
){ m_pixmap=NULL;
init();
}
void Object::init(){
setName(dir.dirName());
setPixmap();
foreach
(QString entry, dir.
entryList(QDir::Dirs|QDir
::NoDotAndDotDot)){ Object *newObject;
addChild(newObject);
newObject=new Object(m_file+"/"+entry);
}
}
else{
}
}
void Object
::setName(QString newValue
){ m_name=newValue;
setText(0,newValue);
}
void Object
::setPixmap(QString file){ if(QFile(file).
exists()){ if(m_pixmap) delete m_pixmap;
}
}
and the function that fills the QTreeWidget
Code:
void Objects::refreshFiles(){
QDir dirInfo
(objectsPath
);
if(dirInfo.exists()){
foreach
(QString entry, dirInfo.
entryList(QDir::Dirs|QDir
::NoDotAndDotDot)){ Object *item=new Object(dirInfo.absolutePath()+"/"+entry);
treeObjects->addTopLevelItem(item);
}
}
}
for the TopLevelItems it works fine, but in subitems it uses the first top level item again.
Any ideas. Thanks
Re: Object Inherit from QTreeWidgetItem
Quote:
Originally Posted by xgoan
for the TopLevelItems it works fine, but in subitems it uses the first top level item again.
As far as I remember, there's some restriction in QTreeWidget code that one can not construct a tree of items first and then add the top level item. One might need to add the top level item first and then continue with adding child items.. I didn't read through your code carefully though so I'm not sure if that's the case.
You might consider using QDirModel+QTreeView too, it might simplify things quite a lot.. take a look at this post.
Re: Object Inherit from QTreeWidgetItem
Ouch...
So I will do the recursivity in the refreshFile() function.
Thanks for the help.
Re: Object Inherit from QTreeWidgetItem
Another question, can the QTreeWidget items be resized?
Re: Object Inherit from QTreeWidgetItem
Re: Object Inherit from QTreeWidgetItem
Could be, but the icon dont resize properly because the item has the same space between QIcon and Text
Re: Object Inherit from QTreeWidgetItem