QPixmap 的使用spa
直接给个例子:code
QPixmap pix(16,16); //做为绘图设备 QPainter painter(&pix); //建立一直画笔 painter.fillRect(0,0,16,16.Qt::black);//在pix上绘制正方形
制做颜色选择框时能够利用上面绘制的正方形做为图标,效果以下图:it
其实想作出这个效果很简单,代码以下:
class
QComboBox *comBox; QPixmap pix(16,16); //建立绘图设备 QPainter painter(&pix);//建立一个画笔 painter.fillRect(0,0,16,16,Qt::black); comBox->addItem(QIcon(pix),tr("黑色"),Qt::black); //红色 painter.fillRect(0,0,16,16,Qt::red); comBox->addItem(QIcon(pix),tr("红色"),Qt::red); //green painter.fillRect(0,0,16,16,Qt::green); comBox->addItem(QIcon(pix),tr("绿色"),Qt::green); //blue painter.fillRect(0,0,16,16,Qt::blue); comBox->addItem(QIcon(pix),tr("蓝色"),Qt::blue); //yellow painter.fillRect(0,0,16,16,Qt::yellow); comBox->addItem(QIcon(pix),tr("黄色"),Qt::yellow); //cyan painter.fillRect(0,0,16,16,Qt::cyan); comBox->addItem(QIcon(pix),tr("蓝绿色"),Qt::cyan); //洋红 painter.fillRect(0,0,16,16,Qt::magenta); comBox->addItem(QIcon(pix),tr("洋红"),Qt::magenta);