2013年3月7日 星期四

Color Button for Qt


void ColorButton::setColor(QColor color)
{
    QPixmap pixmap(this->iconSize());
    pixmap.fill(color);
    QPainter painter;
    painter.begin(&pixmap);
    painter.setPen(0xffffff-color.rgb());
    painter.drawRect(0,0, pixmap.width()-1, pixmap.height()-1);
    painter.end();
    this->setIcon(QIcon(pixmap));
}

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);
}

沒有留言: