您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Qt顏色拾取器怎么實(shí)現(xiàn)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Qt顏色拾取器怎么實(shí)現(xiàn)”吧!
在做很多項(xiàng)目的UI界面的時(shí)候,相信絕大部分人都有過(guò)抄襲別人的UI界面尤其是顏色的時(shí)候,畢竟十個(gè)程序員九個(gè)沒(méi)有審美,或者說(shuō)審美跟一坨屎一樣,大家主要的精力以及擅長(zhǎng)點(diǎn)都是在寫(xiě)功能實(shí)現(xiàn)具體功能上面,這個(gè)事情怎么說(shuō)呢,這確實(shí)是程序員的主要職責(zé),但是在大部分的小公司,UI也都是需要程序員自己去搞定的,自己想不出來(lái)怎么辦,借鑒咯,不知道顏色值怎么辦,用顏色拾取器點(diǎn)一下咯。 Qt內(nèi)置的grabWindow方法,可以指定句柄獲取對(duì)應(yīng)的顏色,所以如果要對(duì)屏幕取得顏色值的話,傳入整個(gè)屏幕的句柄即可,屏幕的句柄在Qt中的表示是QApplication::desktop()->winId(),要實(shí)時(shí)獲取怎么辦呢,當(dāng)然最簡(jiǎn)單的辦法就是開(kāi)個(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); }
到此,相信大家對(duì)“Qt顏色拾取器怎么實(shí)現(xiàn)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。