以下是qt参考文档中例子,其中const QPixmap *pixmap( int i ) const;函数并没有发现被任何函数调用,但是却执行了,有些资料显示是系统对控件重绘调用的,所以麻烦哪为大大能详细讲解下系统如何重绘控件?如何调用const QPixmap *pixmap( int i ) const;函数?
小生先深表谢意!
class Directory : public QListViewItem
{
public:
Directory( QListView * parent, const QString& filename );
Directory( Directory * parent, const QString& filename, const QString &col2 )
: QListViewItem( parent, filename, col2 ), pix( 0 ) {}
Directory( Directory * parent, const QString& filename );
QString text( int column ) const;
QString fullName();
void setOpen( bool );
void setup();
const QPixmap *pixmap( int i ) const;
void setPixmap( QPixmap *p );
private:
QFile f;
Directory * p;
bool readable;
bool showDirsOnly;
QPixmap *pix;
};
Directory::Directory( Directory * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( parent->showDirsOnly ),
pix( 0 )
{
p = parent;
readable = QDir( fullName() ).isReadable();
if ( !readable )
setPixmap( folderLocked );
else
setPixmap( folderClosed );
}
Directory::Directory( QListView * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( ( (DirectoryView*)parent )->showDirsOnly() ),
pix( 0 )
{
p = 0;
readable = QDir( fullName() ).isReadable();
}
void Directory::setPixmap( QPixmap *px )
{
pix = px;
setup();
widthChanged( 0 );
invalidateHeight();
repaint();
}
const QPixmap *Directory::pixmap( int i ) const
{
if ( i )
return 0;
return pix;
}
void Directory::setOpen( bool o )
{
if ( o )
setPixmap( folderOpen );
else
setPixmap( folderClosed );
if ( o && !childCount() ) {
QString s( fullName() );
QDir thisDir( s );
if ( !thisDir.isReadable() ) {
readable = FALSE;
setExpandable( FALSE );
return;
}
listView()->setUpdatesEnabled( FALSE );
const QFileInfoList * files = thisDir.entryInfoList();
if ( files ) {
QFileInfoListIterator it( *files );
QFileInfo * fi;
while( (fi=it.current()) != 0 ) {
++it;
if ( fi->fileName() == "." || fi->fileName() == ".." )
; // nothing
else if ( fi->isSymLink() && !showDirsOnly ) {
FileItem *item = new FileItem( this, fi->fileName(),
"Symbolic Link" );
item->setPixmap( fileNormal );
}
else if ( fi->isDir() )
(void)new Directory( this, fi->fileName() );
else if ( !showDirsOnly ) {
FileItem *item
= new FileItem( this, fi->fileName(),
fi->isFile()?"File":"Special" );
item->setPixmap( fileNormal );
}
}
}
listView()->setUpdatesEnabled( TRUE );
}
QListViewItem::setOpen( o );
}
void Directory::setup()
{
setExpandable( TRUE );
QListViewItem::setup();
}
QString Directory::fullName()
{
QString s;
if ( p ) {
s = p->fullName();
s.append( f.name() );
s.append( "/" );
} else {
s = f.name();
}
return s;
}
QString Directory::text( int column ) const
{
if ( column == 0 )
return f.name();
else if ( readable )
return "Directory";
else
return "Unreadable Directory";
}