• 7334阅读
  • 7回复

[讨论]上传某高手QT编程实例 [复制链接]

上一主题 下一主题
离线dabaolzz
 
只看楼主 倒序阅读 楼主  发表于: 2011-06-22
额,小弟期末答辩需要一个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();
}
f f f  ff
离线dabaolzz
只看该作者 1楼 发表于: 2011-06-22
急啊!~!~!~
各位大大。。可提供10RMB悬赏值。。。
额,如果帖子里说不明白,就加我QQ   4666838795
f f f  ff
离线dabaolzz
只看该作者 2楼 发表于: 2011-06-22
现在悬赏值达到30RMB了。。刚刚赚的!
f f f  ff
离线dabaolzz
只看该作者 3楼 发表于: 2011-06-22
= =原来论坛里的财富值不能给别人啊。。。额,我犯2了。。犯2了……
f f f  ff
离线dabaolzz
只看该作者 4楼 发表于: 2011-06-22
额。。还是没人么…… 会死人的……
f f f  ff
离线jdwx

只看该作者 5楼 发表于: 2011-06-22
还真不知道是谁的问题,想你这样的太多了。
如果学校没教过,就出题,那也太缺德了。
如果教过了,你不会,那就不能怪别人了。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线XChinux

只看该作者 6楼 发表于: 2011-06-22
上面的代码很容易理解的啊,选择目录、选择图片文件,根据当前设置的参数进行图片显示(paintEvent函数里的内容),花点心思看看就行了。


论坛没有启用积分交易功能
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线zzjin

只看该作者 7楼 发表于: 2011-06-23
看了一下.流程很简单的啊.就是一步步的怎么怎么来就行了啊.代码量也就180行啊= =随便几个小时就搞定了吧
快速回复
限100 字节
 
上一个 下一个