Qt4 使用Qt将平面图像转换为具有3D highlight(一款la iPhone)的漂亮圆形图像

Qt4 使用Qt将平面图像转换为具有3D highlight(一款la iPhone)的漂亮圆形图像,qt4,image-manipulation,Qt4,Image Manipulation,在iPhone上,你给它45x45的平面图像作为应用程序图标,SDK会自动为你旋转并高亮显示,给它一个新的3D效果。有没有人有关于如何使用QT4API来模拟这种情况的示例代码或建议?谢谢,--DD(这是一个建议,因为我实际上还没有尝试过) 样式表,即: QPushButton { background: qlineargradient( /*specify pattern here, with colors with alpha channel (0, 0, 0, 150) for ex

在iPhone上,你给它45x45的平面图像作为应用程序图标,SDK会自动为你旋转并高亮显示,给它一个新的3D效果。有没有人有关于如何使用QT4API来模拟这种情况的示例代码或建议?谢谢,--DD

(这是一个建议,因为我实际上还没有尝试过)

样式表,即:

QPushButton {
    background: qlineargradient( /*specify pattern here, with colors with alpha channel (0, 0, 0, 150) for example */ ); 
    image: url(:/reference/to/img/in_q_resourceFile);
}
我认为这将是最简单的解决方案,因为它会给你一个可点击的qpushbutton


请向助手咨询如何编写样式表…

如果您想要iphone应用程序图标的全部效果,我不确定样式表是否能满足您的所有要求:圆角矩形、赋予其3D外观的细微渐变和光泽。但是,如果你能把两幅图像叠加在一起,也许可以。一个可以是具有透明度的圆形3D遮罩图像,然后您只需将45X45图像放在其后面即可。但是,我不知道在这一点上,qstylesheets的可扩展性有多强

但是,另一种选择是使用QPainter。它绝对可以满足您的所有要求。基本上,您要做的是覆盖小部件的paintEvent()、QPushButton、QLabel……等等。并使用源图像自己绘制。下面是一个链接,指向我在自定义绘制QPushButton时创建的wiki条目,以使其具有Windows Aero外观,这与iphone应用程序图标没有什么不同:

下面是类中的paintEvent(),为您提供了一个起点。一旦你开始使用助手,它就非常简单:

 void AeroButton::paintEvent(QPaintEvent * pe)
 {
     Q_UNUSED(pe);

     QPainter painter(this);  
     painter.setRenderHint(QPainter::Antialiasing);

     //test for state changes
     QColor button_color;
     if(this->isEnabled())
     {
         m_hovered ? button_color = m_highlight : button_color = m_color;

         if(m_pressed)
         {
              button_color = m_highlight.darker(250);
         }
     }
     else
     {
         button_color = QColor(50, 50, 50);
     }

     QRect button_rect = this->geometry();

     //outline
     painter.setPen(QPen(QBrush(Qt::black), 2.0));
     QPainterPath outline;
     outline.addRoundedRect(0, 0, button_rect.width(), button_rect.height(), m_roundness, m_roundness);
     painter.setOpacity(m_opacity);
     painter.drawPath(outline);

     //gradient
     QLinearGradient gradient(0, 0, 0, button_rect.height());
     gradient.setSpread(QGradient::ReflectSpread);
     gradient.setColorAt(0.0, button_color);
     gradient.setColorAt(0.4, m_shadow);
     gradient.setColorAt(0.6, m_shadow);
     gradient.setColorAt(1.0, button_color);

     QBrush brush(gradient);
     painter.setBrush(brush); 
     painter.setPen(QPen(QBrush(button_color), 2.0));

     //main button
     QPainterPath painter_path;
     painter_path.addRoundedRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, m_roundness, m_roundness);
     painter.setClipPath(painter_path);

     painter.setOpacity(m_opacity);
     painter.drawRoundedRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, m_roundness, m_roundness);

     //glass highlight
     painter.setBrush(QBrush(Qt::white));
     painter.setPen(QPen(QBrush(Qt::white), 0.01));
     painter.setOpacity(0.30);
     painter.drawRect(1, 1, button_rect.width() - 2, (button_rect.height() / 2) - 2);

     //text
     QString text = this->text();
     if(!text.isNull())
     {
         QFont font = this->font();
         painter.setFont(font);
         painter.setPen(Qt::white);
         painter.setOpacity(1.0);
         painter.drawText(0, 0, button_rect.width(), button_rect.height(), Qt::AlignCenter, text);
     }

      //icon
      QIcon icon = this->icon();
      if(!icon.isNull())
      {
         QSize icon_size = this->iconSize();
         QRect icon_position = this->calculateIconPosition(button_rect, icon_size);
         painter.setOpacity(1.0);
         painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size)));
      }
 }