【原创分享】一个qt4图片浏览器

cpp:

 

#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //this->setAttribute(Qt::WA_PaintOutsidePaintEvent); picid = 0; repaintme = true; } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *pe) { if(repaintme) { //PaintPicture(""); // target.setX(0); target.setY(0); target.setWidth(this->width()); target.setHeight(this->height()); //image.load(picid?"/mnt/hgfs/D/pic/04.jpg" : "/mnt/hgfs/D/pic/05.jpg"); pixmap.load(picid?"/mnt/hgfs/D/pic/04.jpg" : "/mnt/hgfs/D/pic/05.jpg"); source.setX(0); source.setY(0); source.setWidth(pixmap.width()); source.setHeight(pixmap.height()); painter.begin(this); //painter.drawImage(target,image); painter.drawPixmap(target,pixmap,source); painter.end(); // repaintme = false; } } void MainWindow::mousePressEvent ( QMouseEvent * event ) { // int x = event->x(); int y = event->y(); qDebug("x=%d,y=%d/n",x,y); //left int persent = x*100/this->width(); if( persent<35 ) { qDebug("left/n"); on_btnPrev_clicked(); } else if(persent>65) { qDebug("right/n"); on_btnNext_clicked(); } else { qDebug("middle/n"); this->close(); } } void MainWindow::on_btnNext_clicked() { picid = !picid; repaintme = true; this->repaint(); } void MainWindow::on_btnPrev_clicked() { picid = !picid; repaintme = true; this->repaint(); }

 

h:

 

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> #include<QImage> #include<QPixmap> #include<QPainter> #include <QMouseEvent> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); QRectF target; QRectF source; QImage image; QPixmap pixmap; QPainter painter; int picid; int repaintme; private: Ui::MainWindow *ui; virtual void paintEvent(QPaintEvent *pe); //virtual void resizeEvent ( QResizeEvent * event ); //virtual void showEvent ( QShowEvent * event ); virtual void mousePressEvent ( QMouseEvent * event ); private : void on_btnNext_clicked(); void on_btnPrev_clicked(); }; #endif // MAINWINDOW_H

你可能感兴趣的:(UI,浏览器,image,delete,Class,qt)