2013年3月8日 星期五

Color Button for Qt (part 2)




void ColorButton::setColor(QColor color)
{

    QPixmap pixmap(this->iconSize());
    pixmap.fill(color);
    this->setIcon(QIcon(pixmap));

    selectedColor = color;
    this->update();
}

void ColorButton::init(QColor color)
{
    colorDialog.setCurrentColor(color);
    QObject::connect(this, SIGNAL(clicked()),
                          &colorDialog, SLOT(open()));
    QObject::connect(&colorDialog, SIGNAL(colorSelected(QColor)),
                          this, SLOT(setColor(QColor)));
    setColor(color);
}

void ColorButton::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);
    QPainter painter(this);
    painter.setCompositionMode(QPainter::CompositionMode_Screen);
    painter.fillRect(0, 0, this->width(), this->height(), selectedColor);
}

沒有留言: