Hi,
i'm making a QTreeWidget that uses an object that inherits from QTreeWidgetItem.
the code is:
Object.h
#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:
};
#pragma once
#include <QPixmap>
#include <QDir>
#include <QFile>
#include <QString>
#include <QTreeWidgetItem>
#include "Resources.h"
class Object:public QTreeWidgetItem{
public:
Object(QString file="",QString fileInfo="");
void init();
bool isObject(){return (m_pixmap!=NULL);};
void setName(QString newValue);
void setPixmap(QString file=RES_FOLDER_22x22);
private:
QPixmap *m_pixmap;
QString m_name;
QString m_file;
QString m_fileInfo;
};
To copy to clipboard, switch view to plain text mode
Object.cpp
#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;
}
}
#include "Object.h"
Object::Object(QString file,QString fileInfo):m_file(file),m_fileInfo(fileInfo){
m_pixmap=NULL;
init();
}
void Object::init(){
if(QFileInfo(m_file).isDir()){
QDir dir(m_file);
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;
m_pixmap=new QPixmap(file);
setIcon(0,QIcon(file));
}
}
To copy to clipboard, switch view to plain text mode
and the function that fills the QTreeWidget
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);
}
}
}
void Objects::refreshFiles(){
QString objectsPath=OBJECTS_PATH;
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);
}
}
}
To copy to clipboard, switch view to plain text mode
for the TopLevelItems it works fine, but in subitems it uses the first top level item again.
Any ideas. Thanks
Bookmarks