qt显示1/4秒的图像 [英] qt show an image for 1/4 of a second

查看:105
本文介绍了qt显示1/4秒的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我已经尝试过几次来完成这个使用usleep或qt睡眠时显示图像,但有时(几乎每次)它显示白色而不是图像
基本上我想要它接受任何输入说im ready cin会做,然后显示随机图像编号1-28在随机时间范围1.5-3secs然后显示它250milisecs然后隐藏和等待2秒显示图片3秒然后重复。我在debian,g ++;

。提前感谢。

  int getRandInt(int x){
return rand ) % X;
}



类I:public QThread
{
public:
static void sleep(unsigned long secs){
QThread :: msleep(secs);
}
};



QApplication app(argc,argv);

std :: ostringstream oss;
oss<< images /< getRandInt(28)<< .jpg;
std :: cout<< oss.str();

QString qstr = QString :: fromStdString(oss.str());

QPixmap pixmap(qstr);
QPixmap pixmap2(qstr);

QSplashScreen splash(pixmap);
QSplashScreen splash2(pixmap2);

QMainWindow mainWin;

while(1 == 1){
splash.show();
splash.showMessage(QObject :: tr(test),
Qt :: AlignLeft | Qt :: AlignTop,Qt :: black);
app.processEvents();
I :: sleep(250);
splash.finish(0);
splash.raise();
I :: sleep(2 * 1000);
splash2.show();
splash2.showMessage(QObject :: tr(test),Qt :: AlignLeft | Qt :: AlignTop,Qt :: black);
app.processEvents(); I :: sleep(5000); splash2.finish(& mainWin); splash2.raise();
}


解决方案

在1.5-3秒的随机时间中编号为1-28的随机图像然后将其显示为250毫秒意味着在1.75秒和3.25秒之间的随机时间中显示编号为1-28的随机图像,下面是这样。 p>

代码需要Qt 5.x和C ++ 11编译器。我试图突出正确使用C ++ 11 伪随机数设施。我不知道你的代码的最终用途,但如果你用 std :: mt19937_64 替换 random_engine



显示的图像是从一组28张图片中随机抽取的。每个源图像也是随机生成的。



请注意,没有任何形式的睡眠或忙碌循环,以及热心使用lambdas。这个代码不会是任何更短,它写在Python。这是C ++ 11购买你的:简洁,除了别的以外:)

  #include< QApplication> 
#include< QStateMachine>
#include< QTimer>
#include< QStackedWidget>
#include< QPainter>
#include< QLabel>
#include< QPushButton>
#include< random>

typedef std :: default_random_engine random_engine;

QImage randomImage(int size,random_engine& rng){
QImage img(size,size,QImage :: Format_ARGB32_Premultiplied);
img.fill(Qt :: white);
QPainter p(& img);
p.setRenderHint(QPainter :: Antialiasing);
int N = std :: uniform_int_distribution<>(25,200)(rng);
std :: uniform_real_distribution<> dP(0,size);
std :: uniform_int_distribution<> dC(0,255);
QPointF pt1(dP(rng),dP(rng));
for(int i = 0; i QColor c(dC(rng),dC(rng),dC(rng)
p.setPen(QPen(c,3));
QPointF pt2(dP(rng),dP(rng));
p.drawLine(pt1,pt2);
pt1 = pt2;
}
return img;
}

int main(int argc,char * argv [])
{
QApplication a(argc,argv);
std :: random_device rd;
random_engine gen(rd());

int imageSize = 300;
QList< QImage>图片;
for(int n = 0; n <28; ++ n)images< randomImage(imageSize,gen);
std :: uniform_int_distribution<> dImage(0,images.size() - 1);

QStackedWidget显示;
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 ));
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();
}


Hey I have tried several times to complete this using usleep or the qt sleep when showing an image but sometimes (almost every time) it shows up white instead of the image
basically i want it to accept any input to say im ready cin will do, then show a random image numbered 1-28 in a random time ranging 1.5-3secs then show it for 250milisecs then hide and wait 2secs the show the picture for 3secs then repeat. I am on debian, g++;
thanks in advance.

int getRandInt(int x){
    return rand() % x;
}



class I : public QThread
{
public:
        static void sleep(unsigned long secs) {
                QThread::msleep(secs);
        }
};



QApplication app(argc, argv);

std::ostringstream oss;
oss << "images/" << getRandInt(28) << ".jpg";
std::cout << oss.str();

QString qstr = QString::fromStdString(oss.str());

QPixmap pixmap(qstr);
QPixmap pixmap2(qstr);

QSplashScreen splash(pixmap);
QSplashScreen splash2(pixmap2);

QMainWindow mainWin;

while(1==1){
    splash.show();
    splash.showMessage(QObject::tr("test"),
    Qt::AlignLeft | Qt::AlignTop, Qt::black);
    app.processEvents();
    I::sleep(250);
    splash.finish(0);
    splash.raise();   
    I::sleep(2*1000);
    splash2.show();
    splash2.showMessage(QObject::tr("test"),Qt::AlignLeft | Qt::AlignTop, Qt::black);
    app.processEvents(); I::sleep(5000);splash2.finish(&mainWin);splash2.raise(); 
}

解决方案

With an understanding that "show a random image numbered 1-28 in a random time ranging 1.5-3secs then show it for 250milisecs" means "show a random image numbered 1-28 in a random time between 1.75s and 3.25s", the below does it.

The code requires Qt 5.x and a C++11 compiler. I tried to highlight proper use of C++11 pseudorandom number facilities. I don't know the end-use of your code, but if you substituted std::mt19937_64 for random_engine, it'd be good enough for running psychological research as far as randomness goes.

The images shown are randomly drawn from a set of 28 images. Each of the source images are also randomly generated.

Notice the conspicuous absence of any form of sleep or busy-looping, and zealous use of lambdas. This code wouldn't be any shorter were it written in Python. That's what C++11 buys you: conciseness, among other things :)

#include <QApplication>
#include <QStateMachine>
#include <QTimer>
#include <QStackedWidget>
#include <QPainter>
#include <QLabel>
#include <QPushButton>
#include <random>

typedef std::default_random_engine random_engine;

QImage randomImage(int size, random_engine & rng) {
   QImage img(size, size, QImage::Format_ARGB32_Premultiplied);
   img.fill(Qt::white);
   QPainter p(&img);
   p.setRenderHint(QPainter::Antialiasing);
   int N = std::uniform_int_distribution<>(25, 200)(rng);
   std::uniform_real_distribution<> dP(0, size);
   std::uniform_int_distribution<> dC(0, 255);
   QPointF pt1(dP(rng), dP(rng));
   for (int i = 0; i < N; ++i) {
      QColor c(dC(rng), dC(rng), dC(rng));
      p.setPen(QPen(c, 3));
      QPointF pt2(dP(rng), dP(rng));
      p.drawLine(pt1, pt2);
      pt1 = pt2;
   }
   return img;
}

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();
}

这篇关于qt显示1/4秒的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆