Qt 小项目 -- 颜色拾取器
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Qt 小项目 -- 颜色拾取器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                顏色拾取器
文章目錄
- 顏色拾取器
 - 項目文件.pro
 - main.cpp
 - Widget.h
 - Widget.cpp
 
參考 liudianwu 大神的作品: http://www.qtcn.org/bbs/read-htm-tid-46711-ds-1.html#tpc
項目文件.pro
// 添加項目圖標 RC_ICONS = 1.icomain.cpp
#include "Widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }Widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QtWidgets> class Widget : public QWidget {Q_OBJECT public:Widget(QWidget *parent = nullptr);public slots:void showColorValue();void mouseDoubleClickEvent(QMouseEvent *event) override;void changeEvent(QEvent *) override;private:QWidget *render; //顯示區域QLabel *webLabel;QLabel *rgbLabel;QLabel *corLabel;QLineEdit *webEdit;QLineEdit *rgbEdit;QLineEdit *corEdit;bool shoot = false;}; #endif // WIDGET_HWidget.cpp
#include "Widget.h"Widget::Widget(QWidget *parent): QWidget(parent) {render = new QWidget;render->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);render->setMinimumSize(80,80);render->setAutoFillBackground(true);//調色板QPalette palette ;palette.setColor(QPalette::Base,Qt::black); //QLineEdit背景palette.setColor(QPalette::Text,QColor(238,188,30));//QLineEdit前景palette.setColor(QPalette::Window,QColor(238,188,30));//QWidget使用Window角色render->setPalette(palette);webLabel = new QLabel("Web值:");webLabel->setAlignment(Qt::AlignRight);webEdit = new QLineEdit;webEdit->setPalette(palette);webEdit->setReadOnly(true); //只讀rgbLabel = new QLabel("RGB值:");rgbLabel->setAlignment(Qt::AlignRight);rgbEdit = new QLineEdit;rgbEdit->setPalette(palette);rgbEdit->setReadOnly(true);corLabel = new QLabel("坐標值:");corLabel->setAlignment(Qt::AlignRight);corEdit = new QLineEdit;corEdit->setPalette(palette);corEdit->setReadOnly(true);QGridLayout *mainLayout = new QGridLayout(this);mainLayout->addWidget(render,0,0,3,1);mainLayout->addWidget(webLabel,0,1);mainLayout->addWidget(webEdit,0,2);mainLayout->addWidget(rgbLabel,1,1);mainLayout->addWidget(rgbEdit,1,2);mainLayout->addWidget(corLabel,2,1);mainLayout->addWidget(corEdit,2,2);setMinimumSize(344,112);setMaximumSize(344,112);setWindowTitle("顏色拾取器");setWindowFlags(Qt::WindowStaysOnTopHint);}void Widget::showColorValue() {int x = QCursor::pos().x();int y = QCursor::pos().y();corEdit->setText(QString("X:%1 Y:%2").arg(x,5).arg(y,5));QString strDecimalValues,strHex;QScreen *screen = QApplication::primaryScreen();QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(),x,y,2,2);if(!pixmap.isNull()) {QImage image = pixmap.toImage();if(!image.isNull()) {if(image.valid(0,0)) {QColor color = image.pixel(0,0);int red = color.red();int green = color.green();int blue = color.blue();QString strRed = QString("%1").arg(red&0xFF,2,16,QLatin1Char('0'));QString strGreen = QString("%1").arg(green&0xFF,2,16,QLatin1Char('0'));QString strBlue = QString("%1").arg(blue&0xFF,2,16,QLatin1Char('0'));strDecimalValues = QString("%1,%2,%3").arg(red).arg(green).arg(blue);strHex = QString("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());}}}QString str = QString("background-color: rgb(%1);").arg(strDecimalValues);render->setStyleSheet(str);webEdit->setText(strHex);rgbEdit->setText(strDecimalValues);if(shoot)QTimer::singleShot(100,this,&Widget::showColorValue); }void Widget::mouseDoubleClickEvent(QMouseEvent *) {shoot = true;showColorValue(); }void Widget::changeEvent(QEvent *) {shoot = false; }總結
以上是生活随笔為你收集整理的Qt 小项目 -- 颜色拾取器的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 打开word2016总是出现很抱歉,此功
 - 下一篇: Halcon例程(基于GMM模型的分类)