注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

Qt---自绘圆角控件  

2013-12-29 13:50:22|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
class aa : public QWidget
{
Q_OBJECT

public:
aa(QWidget *parent = 0);
~aa();

private slots:
void ChangeColor();
void ChangeText();

private:
Ui::aaClass ui;
PriceBox*   m_Painter;
};
aa::aa(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);


/////////////////////////////////////--test...
this->resize(500,500);

m_Painter = new  PriceBox("85",this);
m_Painter->SetSize(QSize(100,40));
m_Painter->SetRadius(30);
m_Painter->SetFontSize(16);
m_Painter->SetShowWidget(true);
m_Painter->SetShowFrame(true);


QPushButton* btnColor = new QPushButton("turn color",this);
// btnColor->setGeometry(350,300,100,60);
btnColor->show();

QPushButton* btnText = new QPushButton("turn Text",this);
// btnText->setGeometry(350,370,100,60);
btnText->show();

connect(btnColor,SIGNAL(clicked()),this,SLOT(ChangeColor()));
connect(btnText,SIGNAL(clicked()),this,SLOT(ChangeText()));

////---test;

QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(m_Painter,0,Qt::AlignCenter);
layout->addWidget(btnColor,1,Qt::AlignCenter);
layout->addWidget(btnText,2,Qt::AlignCenter);
setLayout(layout);

}

aa::~aa()
{

}
/////////////////////////////////---test...
void aa::ChangeColor()
{
if(m_Painter->GetBackGroundColor()==Qt::blue)
m_Painter->SetBackGroundColor(Qt::red);
else
m_Painter->SetBackGroundColor(Qt::blue);
}
void aa::ChangeText()
{
if(m_Painter->GetText()=="85")
m_Painter->SetText("63");
else
m_Painter->SetText("85");
}
//////---test;

class PriceBox : public QWidget
{
Q_OBJECT

public:
PriceBox(QWidget *parent=0);
PriceBox(const QString& text,QWidget *parent=0);
~PriceBox();
protected:
void paintEvent(QPaintEvent* event);
QSizePolicy sizePolicy() const;
QSize minimumSizeHint() const;

public:
void  SetParent(QWidget* parent);
QWidget* GetParent();
QSize GetSize();
void  SetSize(const QSize& size);
void  SetRadius(int radius);
void  SetShowWidget(bool bShow);
bool  IsShowWidget();
QColor GetBackGroundColor();
void  SetBackGroundColor(const QColor& color);
void  SetText(const QString& text);
QString GetText();
void  SetShowFrame(bool bShow);
bool  IsShowFrame();
void  SetCornerText(const QString& text);
QString GetCornerText();
void  SetFontSize(int fontSize);
int   GetFontSize();


private:
void Init(QWidget* parent);

private:
QWidget* m_Parent;
int m_Radius; //圆角半径;
QSize m_Size;
QColor  m_Color;
bool    m_ShowWidget;
QString m_Text;
bool    m_ShowFrame;
QString m_CornerText;
};
PriceBox::PriceBox(QWidget *parent)  : QWidget(parent)
{
this->Init(parent);
}


PriceBox::PriceBox(const QString& text,QWidget *parent) : QWidget(parent)
{
this->Init(parent);
m_Text = text;
}

PriceBox::~PriceBox()
{
  
}

void PriceBox::paintEvent(QPaintEvent* event)
{
if(m_ShowWidget)
{
QPainter painter(this);
// painter.setRenderHint(QPainter::Antialiasing, true);
if(m_ShowFrame)
painter.setPen(Qt::SolidLine);
else
painter.setPen(Qt::DotLine);

painter.setBrush(QBrush(m_Color));
int height = m_Size.height();
int width = m_Size.width();
double ratio = (double)height/(double)width;
int radiusHeight = m_Radius;
int radiusWidth = ratio*m_Radius;
painter.drawRoundRect(0,0,width-1,height-1,radiusWidth,radiusHeight);
QSize ss = this->size();
QFont font = this->font();
QFontMetrics fm(font);
int textHeight = fm.height();
if(!m_Text.isEmpty())
{
int textWidth = fm.width(m_Text);
int x = (m_Size.width() - textWidth ) / 2;
int y = (m_Size.height() + textHeight ) / 2;
painter.drawText(x,y,m_Text); 
}
if(!m_CornerText.isEmpty())
{
painter.drawText(m_Size.width()-fm.width(m_CornerText),textHeight,m_CornerText);
}
QWidget::paintEvent(event);
}
QSizePolicy PriceBox::sizePolicy() const
{
QSizePolicy  sizePolicy;
sizePolicy.setHorizontalPolicy(QSizePolicy::Fixed);
sizePolicy.setVerticalPolicy(QSizePolicy::Fixed);
return sizePolicy;
}
QSize PriceBox::minimumSizeHint() const
{
return m_Size;
}


void PriceBox::SetParent(QWidget* parent)
{
m_Parent = parent;
this->setParent(parent);
}
QWidget* PriceBox::GetParent()
{
return m_Parent;
}
QSize PriceBox::GetSize()
{
return m_Size;
}
void PriceBox::SetSize(const QSize& size)
{
m_Size.setWidth(size.width());
m_Size.setHeight(size.height());
this->resize(size);
this->repaint();
}
void PriceBox::SetRadius(int radius)
{
m_Radius = radius;
this->repaint();
}
void PriceBox::SetShowWidget(bool bShow)
{
m_ShowWidget = bShow;
this->show();
this->repaint();
}
bool PriceBox::IsShowWidget()
{
return m_ShowWidget;
}
QColor PriceBox::GetBackGroundColor()
{
return m_Color;
}
void PriceBox::SetBackGroundColor(const QColor& color)
{
m_Color = color;
this->repaint();
}
void PriceBox::SetText(const QString& text)
{
m_Text = text;
this->repaint();
}
QString PriceBox::GetText()
{
return m_Text;
}
void PriceBox::SetShowFrame(bool bShow)
{
m_ShowFrame = bShow;
this->repaint();
}
bool PriceBox::IsShowFrame()
{
return m_ShowFrame;
}
void PriceBox::SetCornerText(const QString& text)
{
m_CornerText = text;
this->repaint();
}
QString PriceBox::GetCornerText()
{
return m_CornerText;
}
void PriceBox::SetFontSize(int fontSize)
{
QFont font;
// font.setPointSize(fontSize);
font.setPixelSize(fontSize);
this->setFont(font);
}
int PriceBox::GetFontSize()
{
// return this->font().pointSize();
return this->font().pixelSize();
}
void PriceBox::Init(QWidget* parent)
{
m_Parent = parent;
m_Size = QSize(100,50);
m_Radius = 50;
m_Color = Qt::gray;
m_ShowWidget = false;
m_Text = "";
m_ShowFrame = true;
m_CornerText = "";

}

附,当然还有更轻松的办法,如设置pushButton的stylesheet,或是使用图片,遮罩的方式或是调色板的方式,然
在这些所有的方法中,上面所自绘的方式在处理颜色频繁变换以及文字变动时,cpu率是最低的,而stylesheet的方式最高。
最方便也意味着付出了其他的代价。
  评论这张
 
阅读(3150)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018