int main(int argc, char *argv[]) { QApplication a(argc, argv); std::random_device rd; random_engine gen(rd()); int imageSize = 300; QList<QImage> images; for (int n = 0; n < 28; ++n) images << randomImage(imageSize, gen); std::uniform_int_distribution<> dImage(0, images.size()-1); QStackedWidget display; QPushButton ready("I'm Ready!"); QLabel label, labelHidden; display.addWidget(&ready); display.addWidget(&label); display.addWidget(&labelHidden); QTimer splashTimer; QStateMachine machine; QState s1(&machine), s2(&machine), s3(&machine), s4(&machine); splashTimer.setSingleShot(true); QObject::connect(&s1, &QState::entered, [&]{ display.setCurrentWidget(&ready); ready.setDefault(true); ready.setFocus(); }); s1.addTransition(&ready, "clicked()", &s2); QObject::connect(&s2, &QState::entered, [&]{ label.setPixmap(QPixmap::fromImage(images.at(dImage(gen)))); display.setCurrentWidget(&label); splashTimer.start(250 + std::uniform_int_distribution<>(1500, 3000)(gen)); }); s2.addTransition(&splashTimer, "timeout()", &s3); QObject::connect(&s3, &QState::entered, [&]{ display.setCurrentWidget(&labelHidden); splashTimer.start(2000); }); s3.addTransition(&splashTimer, "timeout()", &s4); QObject::connect(&s4, &QState::entered, [&]{ display.setCurrentWidget(&label); splashTimer.start(3000); }); s4.addTransition(&splashTimer, "timeout()", &s1); machine.setInitialState(&s1); machine.start(); display.show(); return a.exec(); }
static QByteArray randomJPEGByteArray(int w=512, int h=512, int q=85 ) { QImage image=randomImage(w,h); QBuffer bu; bu.open(QBuffer::WriteOnly); QImageWriter iw; iw.setFormat("jpeg"); iw.setDevice(&bu); iw.setOptimizedWrite(true ); iw.setQuality(q); iw.setCompression(9); iw.setProgressiveScanWrite(true ); iw.write( image ); bu.close(); QByteArray ba=bu.buffer(); //utility::byteArrayToFile("random_test.jpeg",ba ); //qDebug()<<"Random JPEG image of size "<<w<<"x"<<h<<" was "<< utility::humanReadableSize(ba.size())<< "("<<(qreal)ba.size()/((qreal)w*h) << " bytes/pixel)"; return ba; }
static QByteArray randomImageByteArray(int w=512, int h=512 ) { QImage image=randomImage(w,h); QByteArray ba=imageToByteArray(image); return ba; }