还要问一个问题,为什么我在这个类里实现的keyPressEvent,ImageorDir ,set buttonpixmap,都没作用,实际调用的是ScanList 里的虚函数???
源码:
#include "/root/subclass/scanlist.h"
#include <qscrollview.h>
#include <qapplication.h>
class PictureList: public ScanList
{
//  Q_OBJECT       ///就是这句话加了就出错
       public :       
              PictureList(QWidget* parent=0, const char* name=0, WFlags f=Qt::WStyle_Customize | Qt::WStyle_NoBorder )
                 :ScanList( parent, name, f )
              {
                 // create the picture view 
              pictureView = new QScrollView( this );
              pictureView->setGeometry(0, 0, 320, 240);
              pictureView->viewport()->setBackgroundColor( blue );
              
              viewLabel = new QLabel( pictureView->viewport() );
              pictureView->addChild(viewLabel);
              pictureView->hide();
              image_flag = 0; // single view flag is initialise to zero  
              size = 0;              
               }
        virtual void setButtonPixmap( MPushButton1 *b1, unsigned int index );
        virtual bool isImageOrDic( QString tmpName );
protected :
        virtual void keyPressEvent( QKeyEvent *e );
private :
       void singleView();
       void GetZoom_orign();
       void GetZoom_standard();
       void zoomIn();
       void zoomOut();
       void FitSize();
       void SetPosition();
       QScrollView              *pictureView;
       QLabel                     *viewLabel;
       QPixmap                     map;
       QPixmap                     pixmap;
       QImage                     image;
       
       bool               image_flag;
       int                      size; 
       unsigned int       orign_w; 
       unsigned int        orign_h;
       unsigned int        zoom_w;
       unsigned int        zoom_h;
       unsigned int        stard_w;
       unsigned int        stard_h;
       bool                     fat_flag;
       
};
void PictureList::keyPressEvent( QKeyEvent *e )
{
    if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Down )
    {
              if( image_flag )
              {
                     pictureView->scrollBy(0,15);
              }
              else
                     DownPressEvent( );
              
    }
    else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Up )
    {
              if( image_flag )
              {
                     pictureView->scrollBy(0,-15);
              }
              else
                     UpPressEvent( );
    }
    else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Left )
    {
              if(image_flag)
                     pictureView->scrollBy(-15,0);
           else
                     LeftPressEvent();
       }
    else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Right )
    {
              if(image_flag)
                     pictureView->scrollBy(15,0);
           else
                     RightPressEvent();
    }       
       else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Return )
    {
 //          printf("go to return\n");
              if(image_flag)
              {       
                     pictureView->hide();
                     image_flag = 0;
              }       
              else
              {              
                     if( pHandle[handleX][handleY]->isDir )
                     {
                            ScanDir();
                     }
                     else
                            singleView();
               }
       }
    else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Minus  )
    {
              if( image_flag )
              {
                     printf("zoomin key press\n");
                     zoomIn();
              }
              else
              {       
/*                     if(current_page > 0 )
                            GetPixmap( --current_page );       
                  else
                            GetPixmap( current_page = page_count-1 );
                     for( unsigned int i=0;i<image_count;i++  )
                            free(namelist);
                     free(namelist);
*/                     qApp->quit();                     
              }
       }
    else if( ( ( QKeyEvent * )e )->key( ) == Qt::Key_Equal )
    {
              if( image_flag )
                     zoomOut();
              else
              {       
                     if(current_page < page_count-1 )
                            GetPixmap( ++current_page );       
                  else
                            GetPixmap( current_page = 0 );
              }
              
    }                            
    else 
    {
              e->ignore();
    }   
}
bool PictureList::isImageOrDic( QString tmpName ) //check the filename is dir or image file?
{
       printf( "hahaahalololo\n" );
       struct stat tmpstat;
       char tmpchar[200];
       
       strcpy(tmpchar,scan_dir );
//       printf("tmpChar is ....%s\n",tmpchar);
       strcat(tmpchar,"/");
       strcat(tmpchar,tmpName);
       printf("tmpChar is..........%s\n",tmpchar);
       lstat(tmpchar,&tmpstat);
       if(S_ISDIR( tmpstat.st_mode )) //check if or not dir
       {
              printf("is dir:%s\n",tmpchar);
              if(!strcmp(tmpName,".")||!strcmp(tmpName,"..")) //omit the . and .. dir
                     return 0;
              else
                     return 1;
       }
       tmpName = tmpName.right(5);
       if( tmpName.contains(".jpg")||tmpName.contains(".jpeg") //check if or not image file
         ||tmpName.contains(".png")||tmpName.contains(".gif")
         ||tmpName.contains(".bmp")||tmpName.contains(".xpm"))       
              return 1;       
       else
              return 0;
}
void PictureList::singleView( )
{
       current_image = current_page *6+handleY*3+handleX;
       strcpy( nameString, scan_dir );
       sprintf(nameString, "%s/%s",nameString, fileNameList.at(current_image));
       printf( "current image name is -------------%s\n",nameString );
       image.load(nameString);
       FitSize();
       image_flag = 1;
       size = 0; // reset the size to zero in new picture
}
void PictureList::FitSize( )// adjust the size and position in scrollview
{
       pictureView->moveChild( viewLabel,0,0 );
       pictureView->setContentsPos( 0,0 );
       orign_w = image.width();
       orign_h = image.height();
       size=1;
       double w_scale=orign_w/315.0;
       double h_scale=orign_h/237.0;
       double scale = w_scale>h_scale?w_scale:h_scale;
       if(scale>1)
              fat_flag=1;
       else
              fat_flag=0;
       stard_w = (unsigned int)(orign_w/scale);
       stard_h = (unsigned int)(orign_h/scale);
       //       viewLabel->setPixmap( pixmap );
       zoomIn();
       pictureView->show(); //after set the position ,show the picture
}
void PictureList::zoomIn()// shear the image
{
       if (size<=-5)
              return;
       size--;
       printf("zoomin move child\n");
       pictureView->setContentsPos( 0,0 );
//       pictureView->moveChild(viewLabel,0,0); // recover the position to lefttop
       strcpy(nameString, scan_dir);
       sprintf(nameString, "%s/%s",nameString, fileNameList.at(current_image));
       printf("nameString is .......%s\n", nameString);
       image.load(nameString);
       printf("zoomin smoothscale image\n");
       if( fat_flag )
              GetZoom_standard();
       else       
              GetZoom_orign();
       printf("zoom_w:%d zoom_h:%d\n",zoom_w,zoom_h);
       image = image.smoothScale( zoom_w, zoom_h );
//       printf("zoomin convert image\n");
       pixmap.convertFromImage(image);
//       printf("zoomin set pixmap\n");
       viewLabel->setPixmap(pixmap);
       SetPosition();       
}
void PictureList::zoomOut() // enlarge the image
{
       if( size>=5 )
              return;
       size++;
//       pictureView->moveChild(viewLabel,0,0); // recover the position to lefttop
       pictureView->setContentsPos( 0,0 );
       strcpy(nameString, scan_dir);
       sprintf(nameString, "%s/%s",nameString, fileNameList.at(current_image));
       image.load(nameString);
       if( fat_flag )       // zoom by the 320*240
              GetZoom_standard();
       else       
              GetZoom_orign(); // zoom by the orgin size
       image = image.smoothScale( zoom_w, zoom_h );
       pixmap.convertFromImage(image);
       viewLabel->setPixmap(pixmap);
       SetPosition();       
}
void PictureList::GetZoom_orign()
{
       switch(size)
       {
       case 0: 
              zoom_w = orign_w;
              zoom_h = orign_h;
              break;
       case 1:
              zoom_w = orign_w*4/3;
              zoom_h = orign_h*4/3;
              break;
       case 2: 
              zoom_w = orign_w*16/9;
              zoom_h = orign_h*16/9;
              break;
       case 3:
              zoom_w = orign_w*64/27;
              zoom_h = orign_h*64/27;
              break;
       case 4: 
              zoom_w = orign_w*256/81;
              zoom_h = orign_h*256/81;
              break;
       case 5:
              zoom_w = orign_w*1024/243;
              zoom_h = orign_h*1024/243;
              break;
       case -1: 
              zoom_w = orign_w*3/4;
              zoom_h = orign_h*3/4;
              break;
       case -2:
              zoom_w = orign_w*9/16;
              zoom_h = orign_h*9/16;
              break;
       case -3: 
              zoom_w = orign_w*27/64;
              zoom_h = orign_h*27/64;
              break;
       case -4:
              zoom_w = orign_w*81/256;
              zoom_h = orign_h*81/256;
              break;
       case -5:
              zoom_w = orign_w*243/1024;
              zoom_h = orign_h*243/1024;
              break;
       default:
              printf("size error\n");              
       }
}
void PictureList::GetZoom_standard()
{
       switch(size)
       {
       case 0: 
              zoom_w = stard_w;
              zoom_h = stard_h;
              break;
       case 1:
              zoom_w = stard_w*4/3;
              zoom_h = stard_h*4/3;
              break;
       case 2: 
              zoom_w = stard_w*16/9;
              zoom_h = stard_h*16/9;
              break;
       case 3:
              zoom_w = stard_w*64/27;
              zoom_h = stard_h*64/27;
              break;
       case 4: 
              zoom_w = stard_w*256/81;
              zoom_h = stard_h*256/81;
              break;
       case 5:
              zoom_w = stard_w*1024/243;
              zoom_h = stard_h*1024/243;
              break;
       case -1: 
              zoom_w = stard_w*3/4;
              zoom_h = stard_h*3/4;
              break;
       case -2:
              zoom_w = stard_w*9/16;
              zoom_h = stard_h*9/16;
              break;
       case -3: 
              zoom_w = stard_w*27/64;
              zoom_h = stard_h*27/64;
              break;
       case -4:
              zoom_w = stard_w*81/256;
              zoom_h = stard_h*81/256;
              break;
       case -5:
              zoom_w = stard_w*243/1024;
              zoom_h = stard_h*243/1024;
              break;
       default:
              printf("size error\n");              
       }
}
void PictureList::SetPosition()
{
       int w = image.width();
       int h = image.height();
       int offsetx,offsety;
       printf("set Position\n");
       if( w<320||h<240 )       //move to viewport center 
       {
              if(w<320)
              {
                     offsetx = (315-w)/2;
                     offsetx=offsetx>0?offsetx:0;
              }
              else
                     offsetx = 0;              
              if(h<240)
              {
                     offsety = (237-h)/2;
                     offsety=offsety>0?offsety:0;
              }
              else
                     offsety = 0;
              printf("set Position execute\n");
              printf(" offsetx:%d offsety:%d\n",offsetx,offsety);
              pictureView->moveChild( viewLabel,offsetx,offsety );                     
              pictureView->setContentsPos( offsetx,offsety );                     
       }
       else
       {
              pictureView->moveChild( viewLabel,0,0 );                     
              pictureView->setContentsPos( 0,0 );
       }
       printf("set Position ok\n");
}
void PictureList::setButtonPixmap( MPushButton1 *b1, unsigned int index ) //set the index pixmap for b1 button
{
       strcpy(nameString,scan_dir);
       sprintf(nameString, "%s/%s",nameString, fileNameList.at(index));
       printf("the %d index image name is.. %s\n", index, nameString );
       struct stat tmpstat;
       lstat(nameString,&tmpstat);
       if(S_ISDIR( tmpstat.st_mode )) // if or not the dictory
       {
              image.load("/usr/pictures/other/dir.png");
              b1->isDir = TRUE;
              printf("is dir:%s\n",nameString);       
       }
       else
       {
              image.load( nameString );
              b1->isDir = FALSE;
              image = image.smoothScale(90,84); //resize the image fit the button
       }
       map.convertFromImage(image);
       b1->setPixmap(map, fileNameList.at(index));
}
int main(int argc, char** argv)
{
    QApplication app(argc,argv);
        app.setFont(QFont("Simsun",16) );
    PictureList pic;
    pic.resize( 320, 240);
    app.setMainWidget(&pic);
    pic.show();
    return app.exec();
}