⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showimage.cpp

📁 QT4.5实现的图片浏览器源码, 实现放大缩小图片,图片切换等功能,可以在LINUX平台和wingdows平台移植.
💻 CPP
字号:
#include <QtGui>
#include "showimage.hpp"
#include <QtDebug>

ShowImage::ShowImage(QWidget *parent)
    : QWidget(parent), wscale(1.0), hscale(1.0)
{
    init_path(QDir("C:/"), 0);

    setBackgroundRole(QPalette::Background);
    setAutoFillBackground(true);
}


QSize ShowImage::minimumSizeHint() const
{
    return QSize(100, 100);
}

QSize ShowImage::sizeHint() const
{
    return QSize(640, 480);
}

void ShowImage::init_path(QDir newdir, QString file)
{
    //qDebug(qPrintable(newdir));
    filepath=QDir(newdir);

    QStringList filters;
    filters << "*.png" << "*.bmp" << "*.gif" << "*.jpg";
    filepath.setNameFilters(filters);

    filepath.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
    filepath.setSorting( QDir::Size | QDir::Reversed );
    list = filepath.entryList();
    //debug
   //while (iterator != list.end())
     //  qDebug("%s", qPrintable((*iterator++)));
    if (!list.empty())
    {
        emit get2beg(false);
        emit get2end(false);
    }
    else
    {
        emit get2beg(true);
        emit get2end(true);
    }
    pixmap = QPixmap(file);
    QStringList::const_iterator itmp = list.begin();
    for (; itmp != list.end(); ++itmp)
        if (filepath.absoluteFilePath((*itmp)) == file)
        {
            iterator = itmp;
            break;
        }
    update();
}

void ShowImage::show_next(void)
{
        if (iterator+1 == list.end())
            iterator = list.begin();
        ++iterator;
        pixmap = QPixmap(filepath.absoluteFilePath((*iterator)));
        wscale = 1.0;
        hscale = 1.0;
        emit turn2default(100);
        update();
}

void ShowImage::show_prev(void)
{  
        if (iterator == list.begin())
            iterator =list.end();
        --iterator;
        pixmap = QPixmap(filepath.absoluteFilePath((*iterator)));
        wscale = 1.0;
        hscale = 1.0;
        emit turn2default(100);
        update();
}

void ShowImage::arise_scale(int rate)
{
    wscale = static_cast<double>(rate)/100.0;
    hscale = wscale;
    update();
}



void ShowImage::paintEvent(QPaintEvent * /* event */)
{
    double pixwidth = static_cast<double>(pixmap.width());
    double pixheight = static_cast<double>(pixmap.height());
    double showwidth = static_cast<double>(width());
    double showheight = static_cast<double>(height());

    double Wscalerate = pixwidth / showwidth;
    double Hscalerate = pixheight / showheight;
    double compare = (Wscalerate > Hscalerate ? Wscalerate : Hscalerate);
    //debug
    //qDebug() << (double)compare;
    QRect rect(
          showwidth/2.0 - pixwidth/compare/2.0,
          showheight/2.0- pixheight/compare/2.0,
          pixwidth/compare,
          pixheight/compare);
    QRect midrect(
          (showwidth/2.0-pixwidth/2.0),
          (showheight/2.0-pixheight/2.0),
          pixwidth,
          pixheight);
    QPainter painter(this);   

    painter.save();

    painter.translate(showwidth/2.0, (showheight/2.0));
    painter.scale(wscale, hscale);
    painter.translate(-(showwidth/2), -(showheight/2.0));

    if (compare < 1.0)
        painter.drawPixmap(midrect, pixmap);
    else
        painter.drawPixmap(rect, pixmap);
    painter.restore();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -