嘿我已经尝试了几次使用usleep或qt睡眠时显示图像但有时(几乎每次)它显示白色而不是图像
基本上我希望它接受任何输入到说我准备好cin会做,然后在随机时间1.5-3秒显示一个随机图像1-28然后显示为250milisecs然后隐藏并等待2secs显示图片为3secs然后重复。我是debian,g ++;
提前谢谢。
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();
}
答案 0 :(得分:0)
了解&#34;在1.5-3秒的随机时间内显示编号为1-28的随机图像,然后显示250毫升&#34;表示&#34;在1.75s和3.25s之间的随机时间内显示1-28的随机图像&#34;,下面是这样。
代码需要Qt 5.x和C ++ 11编译器。我试图强调正确使用C ++ 11 pseudorandom number facilities。我不知道您的代码的最终用途,但如果您将std::mt19937_64
替换为random_engine
,那么就随机性而言,它就足以运行心理学研究。< / p>
显示的图像是从一组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 < 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();
}