Qt繪 —— QPixmap 的使用

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);
相關文章
相關標籤/搜索