主题 : qt4应用程序RGB_COLOR,附代码 复制链接 | 浏览器收藏 | 打印
大笑笑大
级别: 骑士
UID: 25314
精华: 8
发帖: 184
金钱: 1320 两
威望: 264 点
综合积分: 528 分
注册时间: 2010-07-22
最后登录: 2014-10-11
楼主  发表于: 2010-09-03 11:28

 qt4应用程序RGB_COLOR,附代码

使用minicom截图:#snapshot 1.png
可以看到效果图:





根据前一篇HELLO WORD的方法编译运行,记得在run-setting  auguments上输入-qws
部分代码如下:
#include <QtGui>
#include "rgb_color.h"
//#include "qstring.h"

rgbDialog::rgbDialog( QWidget* parent)
            :QDialog(parent)
{
        setupUi(this);
        Qt::WindowFlags flags=0;
        flags |= Qt::WindowMaximizeButtonHint;//add max button
        this->setWindowFlags(flags);//update()
        // this is must be in
        QObject::connect(RHSlider,SIGNAL(valueChanged(int)),
                         this,SLOT(setRValue()));
        QObject::connect(GHSlider,SIGNAL(valueChanged(int)),
                         this,SLOT(setGValue()));
        QObject::connect(BHSlider,SIGNAL(valueChanged(int)),
                         this,SLOT(setBValue()));
        QObject::connect(shapecomboBox,SIGNAL(activated(int)),
                         this,SLOT(setShape(int)));//shapecomboBox
        QObject::connect(stylecomboBox,SIGNAL(activated(int)),
                         this,SLOT(setStyle(int)));
        //SIGNAL AND SLOT
        rValue = 0;
    gValue = 0;
    bValue = 0;
    myShape = 0;
    myStyle = 0;        
        //*****MAIN CODE***
        setStyleSheet("background:rgb(200,255,255)");

        this->ssgroupBox->setStyleSheet("background:rgb(200,255,255)");
        this->Rlabel->setStyleSheet("background-color: rgb(200,255,255)");
        this->Glabel->setStyleSheet("background:rgb(200,255,255)");
        this->Blabel->setStyleSheet("background:rgb(200,255,255)");
        this->RHSlider->setStyleSheet("background:rgb(200,255,255)");
        this->GHSlider->setStyleSheet("background:rgb(200,255,255)");
        this->BHSlider->setStyleSheet("background:rgb(200,255,255)");
        redLCDNumber->setStyleSheet("background:rgb(255, 0, 0)");
        greenLCDNumber->setStyleSheet("background:rgb(0, 255, 0)");
        blueLCDNumber->setStyleSheet("background:rgb(0, 0, 255)");
        this->pixmaplabel->clear();
        pixmaplabel->setPalette( QPalette( QColor(192, 192, 192) ) );
        pixmaplabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
        pixmaplabel->setAlignment(Qt::AlignCenter);
        pixmaplabel->setText( "Designed By XPH \n2-9-2010" );
        rgbColor.setRgb(0,0,0);
}

void rgbDialog::paintEvent( QPaintEvent * )
{
    QPainter paint(this);
    
        paint.setPen(QPen(Qt::black,1,Qt::SolidLine));
    if(myStyle == 0)
                paint.setBrush(QBrush(rgbColor,Qt::SolidPattern));
    else if(myStyle == 1)
                paint.setBrush(QBrush(rgbColor,Qt::Dense4Pattern));
    else if(myStyle == 2)
                paint.setBrush(QBrush(rgbColor,Qt::HorPattern));
    else if(myStyle == 3)
                paint.setBrush(QBrush(rgbColor,Qt::VerPattern));
    else if(myStyle == 4)
                paint.setBrush(QBrush(rgbColor,Qt::BDiagPattern));
    else if(myStyle == 5)
                paint.setBrush(QBrush(rgbColor,Qt::CrossPattern));
    else if(myStyle == 6)
                paint.setBrush(QBrush(rgbColor,Qt::DiagCrossPattern));
    
    if(myShape == 0)
                paint.drawEllipse(10,10,100,80);//center int x, int y, int width ,int high
    else if(myShape == 1)
                paint.drawRoundRect(10,10,100,80,25,25);
    else if(myShape == 2)
        paint.drawPie(10,10,100,100,45*16,270*16);
    else if(myShape == 3)
    {
                //QPointArray a;  //USE IN QT3
                QPolygon a;       //qt4 USE THIS
        a.setPoints(11,60,10,110,40,80,40,110,70,75,70,75,110,45,110,45,70,10,70,40,40,10,40);
                //have 11 points
        paint.drawPolygon(a);                
    }
        
}

void rgbDialog::setRValue()
{
        rValue = RHSlider->value();
    redLCDNumber->display(rValue);        
        QString text=tr("%1%2%3")
                     .arg("background:rgb(")
                     .arg(rValue)
                     .arg(", 0, 0)");
        //setWindowTitle(text);//DEBUG
        redLCDNumber->setStyleSheet(text);
    rgbColor.setRgb(rValue,gValue,bValue);
    update();
}

void rgbDialog::setGValue()
{
        gValue = GHSlider->value();
    greenLCDNumber->display(gValue);
        QString text=tr("%1%2%3")
                     .arg("background:rgb(0,")
                     .arg(gValue)
                     .arg(", 0)");
        //setWindowTitle(text);
        greenLCDNumber->setStyleSheet(text);
    rgbColor.setRgb(rValue,gValue,bValue);
    update();
}

void rgbDialog::setBValue()
{
        bValue = BHSlider->value();
    blueLCDNumber->display(bValue);
        QString text=tr("%1%2%3")
                     .arg("background:rgb(0, 0,")
                     .arg(bValue)
                     .arg(")");
        //setWindowTitle(text);
        blueLCDNumber->setStyleSheet(text);
    rgbColor.setRgb(rValue,gValue,bValue);
    update();
}

void rgbDialog::setStyle(int index)
{
    if(index == 0)    
        myStyle = 0;    
    else if(index == 1)    
        myStyle = 1;    
    else if(index == 2)    
        myStyle = 2;    
    else if(index == 3)    
        myStyle = 3;    
    else if(index == 4)    
        myStyle = 4;    
    else if(index == 5)    
        myStyle = 5;    
    else if(index == 6)    
        myStyle = 6;        
    
    update();
}

void rgbDialog::setShape(int index)
{
    if(index == 0)
        myShape = 0;    
    else if(index == 1)    
        myShape = 1;    
    else if(index == 2)    
        myShape = 2;    
    else if(index == 3)    
        myShape = 3;        
    
    update();    
}
    
附件: rgb_color.zip (5 K) 下载次数:54
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2010-09-03 11:31
好,值得顶:)
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 侠客
UID: 13429
精华: 0
发帖: 51
金钱: 255 两
威望: 51 点
综合积分: 102 分
注册时间: 2010-01-24
最后登录: 2015-05-25
2楼  发表于: 2010-11-03 13:36
好,值得顶
级别: 骑士
UID: 11942
精华: 1
发帖: 144
金钱: 775 两
威望: 155 点
综合积分: 308 分
注册时间: 2009-12-25
最后登录: 2022-01-16
3楼  发表于: 2010-11-05 10:26
学习学习