• 大小: 842KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: 其他
  • 标签:

资源简介

该代码用Qt4开发工具编写而成,界面所用模板为MainWindow,该模板可带下拉菜单选项,该代码在此基础上实现了对图片的基本操作。例如缩放,窗口填充,大小还原。

资源截图

代码片段和文件信息

#include 
#include “imageviewer.h“

ImageViewer::ImageViewer()
{
    imageLabel = new QLabel;
    imageLabel->setBackgroundRole(QPalette::base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(imageLabel);
    setCentralWidget(scrollArea);

    createActions();
    createMenus();

    setWindowtitle(tr(“Image Viewer“));
    resize(500 400);
}

void ImageViewer::open()
{
    QString fileName = QFileDialog::getOpenFileName(this
                                    tr(“Open File“) QDir::currentPath());
    if (!fileName.isEmpty())
    {
        QImage image(fileName);
        if (image.isNull())
        {
            QMessageBox::information(this tr(“Image Viewer“)
                                     tr(“Cannot load %1.“).arg(fileName));
            return;
        }
        imageLabel->setPixmap(QPixmap::fromImage(image));
        scaleFactor = 1.0;

        printAct->setEnabled(true);
        fitToWindowAct->setEnabled(true);
        updateActions();

        if (!fitToWindowAct->isChecked())
            imageLabel->adjustSize();
    }
}
void ImageViewer::print()
{
    Q_ASSERT(imageLabel->pixmap());
#ifndef QT_NO_PRINTER
    QPrintDialog dialog(&printer this);
    if (dialog.exec()) {
        QPainter painter(&printer);
        QRect rect = painter.viewport();
        QSize size = imageLabel->pixmap()->size();
        size.scale(rect.size() Qt::KeepAspectRatio);
        painter.setViewport(rect.x() rect.y() size.width() size.height());
        painter.setWindow(imageLabel->pixmap()->rect());
        painter.drawPixmap(0 0 *imageLabel->pixmap());
    }
#endif
}
void ImageViewer::zoomIn()
{
    scaleImage(1.25);
}

void ImageViewer::zoomOut()
{
    scaleImage(0.8);
}

void ImageViewer::normalSize()
{
    imageLabel->adjustSize();
    scaleFactor = 1.0;
}

void ImageViewer::fitToWindow()
{
    bool fitToWindow = fitToWindowAct->isChecked();
    scrollArea->setWidgetResizable(fitToWindow);
    if (!fitToWindow)
    {
        normalSize();
    }
    updateActions();
}

void ImageViewer::about()
{
    QMessageBox::about(this tr(“About Image Viewer“)
            tr(“help“));
}

void ImageViewer::createActions()
{
    openAct = new QAction(tr(“&Open...“) this);
    openAct->setShortcut(tr(“Ctrl+O“));
    connect(openAct SIGNAL(triggered()) this SLOT(open()));

    printAct = new QAction(tr(“&Print...“) this);
    printAct->setShortcut(tr(“Ctrl+P“));
    printAct->setEnabled(false);
    connect(printAct SIGNAL(triggered()) this SLOT(print()));

    exitAct = new QAction(tr(“E&xit“) this);
    exitAct->setShortcut(tr(“Ctrl+Q“));
    connect(exitAct SIGNAL(triggered()) this SLOT(close()));

    zoomInAct = new QAction(tr(“Zoom &In (25%)“) 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    .CA....   2962761  2009-08-20 18:12  imageviewer\debug\imageviewer.exe

    .CA....   1343848  2009-08-20 18:12  imageviewer\debug\imageviewer.o

    .CA....    479132  2009-08-20 18:12  imageviewer\debug\main.o

    .CA....      2656  2009-08-20 18:12  imageviewer\debug\moc_imageviewer.cpp

    .CA....    485156  2009-08-20 18:12  imageviewer\debug\moc_imageviewer.o

    .CA....      5651  2009-08-21 15:03  imageviewer\imageviewer.cpp

    .CA....      1116  2009-08-15 22:35  imageviewer\imageviewer.h

    .CA....       382  2009-06-20 13:01  imageviewer\imageviewer.pro

    .CA....     12457  2009-08-21 15:03  imageviewer\imageviewer.pro.user

    .CA....       207  2009-08-20 08:55  imageviewer\main.cpp

    .CA....      5756  2009-08-21 14:04  imageviewer\Makefile

    .CA....      7993  2009-08-21 14:04  imageviewer\Makefile.Debug

    .CA....      8066  2009-08-21 14:04  imageviewer\Makefile.Release

    .CA....      2663  2009-06-22 21:43  imageviewer\tmp\moc\release_shared\moc_imageviewer.cpp

    .C.D...         0  2009-08-20 22:49  imageviewer\tmp\moc\release_shared

    .C.D...         0  2009-08-20 22:49  imageviewer\tmp\obj\release_shared

    .C.D...         0  2009-08-20 22:49  imageviewer\tmp\moc

    .C.D...         0  2009-08-20 22:49  imageviewer\tmp\obj

    .C.D...         0  2009-08-20 22:49  imageviewer\debug

    .C.D...         0  2009-08-20 22:49  imageviewer\release

    .C.D...         0  2009-08-20 22:49  imageviewer\tmp

    .C.D...         0  2009-08-20 22:49  imageviewer

----------- ---------  ---------- -----  ----

              5317844                    22


评论

共有 条评论

相关资源