简体   繁体   English

如何检查QLabel中设置了哪个图像?

[英]How to check which image is set in my QLabel?

I have a Qt application where I need to show a blinking LED and for that I need to use some png image of off and on led.I created a Qlabel and used setstylesheet to display the image. 我有一个Qt应用程序,需要显示一个闪烁的LED,为此我需要使用关闭和打开led的png图像。我创建了一个Qlabel并使用setstylesheet来显示图像。 I created a timer and connected the signal to a slot. 我创建了一个计时器,并将信号连接到插槽。
Now the problem is how do I know if the current displayed image is OFF led or ON led . 现在的问题是我怎么知道当前显示的图像是OFF led or ON led

I have many led in GUI so is there any better way to check this? 我在GUI中有许多领导者,所以有没有更好的方法来检查这一点?

Don't bother trying to compare the image, just store a variable of the state of the LED. 不要费力尝试比较图像,只需存储LED状态的变量即可。 When the timer triggers you change the state of the variable and set the QImage accordingly. 当计时器触发时,您将更改变量的状态并相应地设置QImage。

// assuming a boolean variable
var = !var;
if(var)
   label->setImage(":/images/imageOn"); 
else
   label->setImage(":/images/imageOff");

This assumes the images imageOn and imageOff have been added to a Qt resource file and are under an 'images' prefix. 假定图像imageOn和imageOff已添加到Qt资源文件中,并且位于'images'前缀下。

It is good practise to separate logic from its visual representation. 好的做法是将逻辑与其视觉表示分开。

You can leverage the property mechanism to store the index of the next image to be used. 您可以利用属性机制来存储下一个要使用的图像的索引。 A QLabel is-a QObject . QLabelQObject Objects can have arbitrary properties assigned to them. 对象可以具有分配给它们的任意属性。

You also don't need to use style sheets to set image on a label. 您也不需要使用样式表在标签上设置图像。 It's a premature pessimization because the stylesheet needs to be parsed every time you set it. 这是过早的悲观,因为每次设置样式表时都需要对其进行分析。 If you're not using stylesheets for other purposes, to set an image on a label simply use setPixmap . 如果您不将样式表用于其他目的,只需使用setPixmap即可在标签上设置图像。

For example (Qt 5, C++11): 例如(Qt 5,C ++ 11):

#include <QApplication>
#include <QTimer>
#include <QLabel>
#include <QImage>
#include <QPainter>

void blink(QLabel * label, const QList<QImage> & images)
{
  const char * const prop = "imageIndex";
  Q_ASSERT(!images.isEmpty());
  if (label->property(prop).isNull()) {
    // We're setting the image for the first time
    label->setProperty(prop, images.size());
  }
  int i = (label->property(prop).toInt() + 1) % images.size();
  label->setPixmap(QPixmap::fromImage(images[i]));
  label->setProperty(prop, i);
}

QImage textImage(const QString & text, int size = 64)
{
  QImage image(size, size, QImage::Format_ARGB32_Premultiplied);
  image.fill(Qt::transparent);
  QPainter p(&image);
  p.setFont(QFont("helvetica", 20));
  QTextOption opt;
  opt.setAlignment(Qt::AlignCenter);
  p.drawText(image.rect(), text, opt);
  return image;
}

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QList<QImage> images;
  QLabel label;
  QTimer timer;

  images << textImage("0") << textImage("1") << textImage("2") << textImage("3");
  blink(&label, images);
  timer.start(250);
  QObject::connect(&timer, &QTimer::timeout, [&]{ blink(&label, images); });

  label.show();
  return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM