qt 定时器_Qt开源作品23-颜色拾取器
## 一、前言
在做很多項(xiàng)目的UI界面的時(shí)候,相信絕大部分人都有過抄襲別人的UI界面尤其是顏色的時(shí)候,畢竟十個(gè)程序員九個(gè)沒有審美,或者說審美跟一坨屎一樣,大家主要的精力以及擅長點(diǎn)都是在寫功能實(shí)現(xiàn)具體功能上面,這個(gè)事情怎么說呢,這確實(shí)是程序員的主要職責(zé),但是在大部分的小公司,UI也都是需要程序員自己去搞定的,自己想不出來怎么辦,借鑒咯,不知道顏色值怎么辦,用顏色拾取器點(diǎn)一下咯。
Qt內(nèi)置的grabWindow方法,可以指定句柄獲取對應(yīng)的顏色,所以如果要對屏幕取得顏色值的話,傳入整個(gè)屏幕的句柄即可,屏幕的句柄在Qt中的表示是QApplication::desktop()->winId(),要實(shí)時(shí)獲取怎么辦呢,當(dāng)然最簡單的辦法就是開個(gè)定時(shí)器咯,定時(shí)器不斷調(diào)用這個(gè)方法,獲取屏幕鼠標(biāo)坐標(biāo)和顏色值。
## 二、代碼思路
void ColorWidget::showColorValue(){ if (!pressed) { return; } int x = QCursor::pos().x(); int y = QCursor::pos().y(); txtPoint->setText(tr("x:%1 y:%2").arg(x).arg(y)); QString strDecimalValue, strHex, strTextColor; int red, green, blue;#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);#else QScreen *screen = QApplication::primaryScreen(); QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);#endif if (!pixmap.isNull()) { QImage image = pixmap.toImage(); if (!image.isNull()) { if (image.valid(0, 0)) { QColor color = image.pixel(0, 0); red = color.red(); green = color.green(); blue = color.blue(); QString strRed = tr("%1").arg(red & 0xFF, 2, 16, QChar('0')); QString strGreen = tr("%1").arg(green & 0xFF, 2, 16, QChar('0')); QString strBlue = tr("%1").arg(blue & 0xFF, 2, 16, QChar('0')); strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue); strHex = tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper()); } } } if (red > 200 && green > 200 && blue > 200) { strTextColor = "10, 10, 10"; } else { strTextColor = "255, 255, 255"; } QString str = tr("background-color: rgb(%1);color: rgb(%2)").arg(strDecimalValue).arg(strTextColor); labColor->setStyleSheet(str); txtRgb->setText(strDecimalValue); txtWeb->setText(strHex);}## 三、效果圖
## 四、開源主頁
**以上作品完整源碼下載都在開源主頁,會持續(xù)不斷更新作品數(shù)量和質(zhì)量,歡迎各位關(guān)注。**
1. 國內(nèi)站點(diǎn):[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo)
2. 國際站點(diǎn):[https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo)
3. 個(gè)人主頁:[https://blog.csdn.net/feiyangqingyun](https://blog.csdn.net/feiyangqingyun)
4. 知乎主頁:[https://www.zhihu.com/people/feiyangqingyun/](https://www.zhihu.com/people/feiyangqingyun/)
總結(jié)
以上是生活随笔為你收集整理的qt 定时器_Qt开源作品23-颜色拾取器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓 sharedpreferences
- 下一篇: vs无法写入量的大数据_一个每天服务数万