PDA

View Full Version : Object Inherit from QTreeWidgetItem



xgoan
18th August 2006, 11:05
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"

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;
};

Object.cpp

#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));
}
}
and the function that fills the QTreeWidget


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);
}
}
}
for the TopLevelItems it works fine, but in subitems it uses the first top level item again.

Any ideas. Thanks

jpn
18th August 2006, 11:12
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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-suggestionsideas-about-a-file-browser-1625.html#5).

xgoan
18th August 2006, 11:35
Ouch...

So I will do the recursivity in the refreshFile() function.

Thanks for the help.

xgoan
18th August 2006, 12:15
Another question, can the QTreeWidget items be resized?

jpn
18th August 2006, 12:56
Try QTreeWidgetItem::setSizeHint (http://doc.trolltech.com/4.1/qtreewidgetitem.html#setSizeHint)

xgoan
18th August 2006, 13:03
Could be, but the icon dont resize properly because the item has the same space between QIcon and Text

jpn
18th August 2006, 13:20
How about QAbstractItemView::setIconSize (http://doc.trolltech.com/4.1/qabstractitemview.html#iconSize-prop)?