• 5928阅读
  • 3回复

关于qt参考文档中例子的疑惑 [复制链接]

上一主题 下一主题
离线undead8816
 

只看楼主 倒序阅读 楼主  发表于: 2007-12-03
关于qt参考文档中例子的疑惑
— 本帖被 XChinux 执行加亮操作(2008-07-17) —
以下是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";
}
离线myer

只看该作者 1楼 发表于: 2007-12-04
应该是Qt3下的程序:
头文件如下,pixmap()是个虚函数,C++语言……
class Q_EXPORT QListViewItem : public Qt
{
......
    virtual const QPixmap * pixmap( int ) const;

......
}
http://user.qzone.qq.com/56430808
离线米饭拌面

只看该作者 2楼 发表于: 2007-12-04
一般那个都是写成静态的 你当然看不到调用了啊

要不就是在构造函数定义的时候写在名字后面直接转成QPixmap了
QQ:32336134
离线foxyz

只看该作者 3楼 发表于: 2007-12-14
还是得对C++和面向对象作深入了解。这个函数返回一个类指针,很象一个callback函数。跟静态不静态没关系。
快速回复
限100 字节
 
上一个 下一个