当前位置:网站首页>QT get random color value and set label background color code

QT get random color value and set label background color code

2022-07-04 05:56:00 Program yuan ZCC

Requirements creation widget when , Set random background color , Pictured .

  Main code :

#include <iostream>
#include <random>
using namespace std;
std::default_random_engine e;
std::uniform_real_distribution<double> random(0,1);

//  Get random colors 
QColor color = QColor::fromRgb(int(random(e)*255), int(random(e)*255), int(random(e)*255));
QString winColor = color.name();

// Method 1 of setting background color 
ui->signColor->setStyleSheet("QLabel{background-color: "+ winColor + ";border:2px groove #191919;}");

// Method 2 
ui->signColor->setStyleSheet("background-color:rgb("+ QString::number(int(random(e)*255)) + "," +
QString::number(int(random(e)*255)) + "," + QString::number(int(random(e)*255)) + ");");
// obtain  r/g/b  value  
   
QColor color = QColorDialog::getColor(Qt::white, this);
qDebug() << " Color choices " << color << color.name(); 
QRgb rgb = color.rgb();

qDebug() << "rgb== " <<  qRed(rgb) << qGreen(rgb) << qBlue(rgb);

 

原网站

版权声明
本文为[Program yuan ZCC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040549046274.html