额,小弟期末答辩需要一个QT程序,故在网上DOWN了一个图片浏览器源代码。读来读去,发现半点都不明白(额,好吧,我是个贼水的货。)故想请问各位大大是否能教教我。额。麻烦给逐行解释下代码。。6月27就答辩了。。
mywindow.cpp
#include "myWidget.h"
#include "myWindow.h"
#include <QFileDialog>
MyWindow::MyWindow(QWidget *parent) :
QMainWindow(parent) {
this->setupUi(this);
scrollArea = new QScrollArea;
//scrollArea->setFixedSize(QSize(800,600));
//scrollArea->widget()->setMinimumSize(520,540);
this->setCentralWidget(scrollArea);
myWidget = new MyWidget;
scrollArea->setWidget(myWidget);
this->setCentralWidget(myWidget);
filter << "*.jpg" << "*.bmp" << "*.jpeg" << "*.png" << "*.xpm";
initConnect();
this->statusBar()->showMessage(tr("ready"));
}
void MyWindow::doOpen() {
QString str = QFileDialog::getExistingDirectory(this, tr("鎵撳紑鐩綍"),
tr("/home/suo"), QFileDialog::ShowDirsOnly);
curDir.setPath(str);
index = -1;
imageList = curDir.entryList(filter, QDir::Files);
myWidget->setScale(1);
doNext();
}
void MyWindow::initConnect() {
connect(actOpen, SIGNAL(triggered()), this, SLOT(doOpen()));
connect(actNext, SIGNAL(triggered()), this, SLOT(doNext()));
connect(actBack, SIGNAL(triggered()), this, SLOT(doBack()));
connect(actBig, SIGNAL(triggered()), this, SLOT(doBig()));
connect(actSmall, SIGNAL(triggered()), this, SLOT(doSmall()));
connect(actLeft, SIGNAL(triggered()), this, SLOT(doLeft()));
connect(actRight, SIGNAL(triggered()), this, SLOT(doRight()));
connect(actFull, SIGNAL(triggered()), this, SLOT(doFull()));
connect(actFit,SIGNAL(triggered()),this,SLOT(doFit()));
connect(actActual,SIGNAL(triggered()),this,SLOT(doActual()));
}
void MyWindow::doNext() {
if (index >= imageList.length() - 1)
return;
myWidget->setScale(1);
myWidget->setAngle(0);
myWidget->setFit(false);
myWidget->setFull(false);
index++;
this->statusBar()->showMessage(imageList.at(index));
myWidget->setPixmap(
curDir.absolutePath() + QString(tr("/")) + imageList.at(index));
}
void MyWindow::doBack() {
if (index <= 0)
return;
myWidget->setScale(1);
myWidget->setAngle(0);
myWidget->setFit(false);
myWidget->setFull(false);
index--;
this->statusBar()->showMessage(imageList.at(index));
myWidget->setPixmap(
curDir.absolutePath() + QString(tr("/")) + imageList.at(index));
}
void MyWindow::doBig() {
myWidget->setScale(myWidget->getScale() * 1.25);
actBig->setEnabled(myWidget->getScale() < 3);
actSmall->setEnabled(myWidget->getScale() > 0.7);
myWidget->doBig();
//qDebug()<<myWidget->width()<<" "<<myWidget->height()<<endl;
}
void MyWindow::doSmall() {
myWidget->setScale(myWidget->getScale() * 0.8);
actBig->setEnabled(myWidget->getScale() < 3);
actSmall->setEnabled(myWidget->getScale() > 0.7);
myWidget->doSmall();
//qDebug()<<myWidget->width()<<" "<<myWidget->height()<<endl;
}
void MyWindow::doLeft() {
myWidget->setAngle(myWidget->getAngle() - 90);
}
void MyWindow::doRight() {
myWidget->setAngle(myWidget->getAngle() + 90);
}
void MyWindow::doFull() {
statusBar()->hide();
menuBar()->hide();
toolBar->hide();
myWidget->setFull(true);
myWidget->setFixedSize(1366,768);
this->showFullScreen();
}
void MyWindow::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Escape) {
statusBar()->show();
menuBar()->show();
toolBar->show();
myWidget->setFull(false);
myWidget->setFixedSize(800,600);
this->showNormal();
}
}
void MyWindow::doActual(){
myWidget->setFit(false);
}
void MyWindow::doFit(){
myWidget->setFit(true);
}
____________________________________________________
mywidget.cpp
#include "myWidget.h"
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent) { pixmap = QPixmap(800, 600);
pixmap.fill(Qt::gray);
scale = 1;
angle = 0;
this->setFixedSize(QSize(800, 600));
fit = false;
full = false;
}
void MyWidget::paintEvent(QPaintEvent *e) {
QPainter painter(this);
painter.fillRect(0, 0, width(), height(), QColor(Qt::gray));
if (angle != 0) { painter.translate(width() / 2, height() / 2);
painter.rotate(angle);
painter.translate(-width() / 2, -height() / 2);
}
if (fit) {
QPixmap pix = pixmap;
pix = pix.scaled(width(), height());
painter.drawPixmap(0, 0, pix);
} else { if (full) {
QPixmap pix = pixmap;
pix = pix.scaled(1366, 768);
qDebug()<<width()<<" "<<height()<<endl;
painter.drawPixmap(0, 0, pix);
} else {
int x, y;
x = this->width() / 2 - pixmap.width() / 2;
y = this->height() / 2 - pixmap.height() / 2;
painter.drawPixmap(QPoint(x, y), pixmap);
}
}
}
void MyWidget::setPixmap(const QString& str) {
pixmap.fill(Qt::gray);
pixmap.load(str); this->update();
}
qreal MyWidget::getScale() {
return scale;
}
void MyWidget::setScale(qreal scale) { this->scale = scale;
}
void MyWidget::doBig() {
pixmap = pixmap.scaled(pixmap.size() * 1.25);
update();
//this->setFixedSize(1.25*this->size());
this->resize(1.25 * this->size());
}
void MyWidget::doSmall() { pixmap = pixmap.scaled(pixmap.size() * 0.8);
update();
//this->setFixedSize(0.8*this->size());
this->resize(1.25 * this->size());
}
void MyWidget::setAngle(const qreal angle) {
this->angle = angle;
update();
}
qreal MyWidget::getAngle() { return angle;
}
void MyWidget::setFit(const bool b) {
fit = b;
update();
}
void MyWidget::setFull(const bool b) { full = b;
update();
}
_________________________________________________
main.cpp
#include "myWindow.h"
#include <QTextCodec>
#include <QApplication>
int main(int argc,char *argv[]){ QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
MyWindow myWindow;
myWindow.show();
return app.exec();
}