資源簡介
最近復(fù)習(xí)已經(jīng)學(xué)習(xí)的Qt知識,制作了一個簡單的圖表顯示工具。目前它能夠很好地顯示柱狀圖。
這個柱狀圖支持任意多的項(xiàng)目(柱子),只需要在代碼中添加了相關(guān)數(shù)據(jù)后,使用Paint()函數(shù)就可以將其渲染成一個Pixmap,然后作為中央控件(centeral widget)的一張圖片顯示出來。
代碼片段和文件信息
#include?
#include?“DiagramViews.h“
#include?“Histogram.h“
DiagramViews::DiagramViews(QWidget?*parent):?QWidget(?parent?)
{
m_BKColor?=?QColor(?255?197?243?);
m_VecPixmap.clear(?);
setAutoFillBackground(?true?);
setPalette(?QPalette(?m_BKColor?)?);
setMinimumSize(?640?480?);
adjustSize(?);
DrawPixmaps(?);
}
void?DiagramViews::DrawPixmaps(?void?)
{
m_VecPixmap.clear(?);
for?(?quint32?i?=?0;?i?3;?i++?)
{
QPixmap?pixmap(?minimumSize(?)?*?0.8?);
pixmap.fill(?QColor(?170?255?255?)?);
m_VecPixmap.push_back(?pixmap?);
}
//?繪制第一張像素圖
Histogram?histogram;
histogram.SetMaxValue(?64?);
histogram.AddItem(?tr(?“East“?)?32?QColor(?125?0?0?)?);
histogram.AddItem(?tr(?“West“?)?48?QColor(?0?125?0?)?);
histogram.AddItem(?tr(?“South“?)?24?QColor(?0?0?125?)?);
histogram.AddItem(?tr(?“North“?)?26?QColor(?125?125?125?)?);
histogram.Paint(?&m_VecPixmap[0]?);
}
void?DiagramViews::paintEvent(?QPaintEvent*?)
{
QPainter?painter(?this?);
QPoint?pixmapPt;
pixmapPt.setX(?minimumSize(?).width(?)?*?0.1?);
pixmapPt.setY(?minimumSize(?).height(?)?*?0.1?);
painter.drawPixmap(?pixmapPt?m_VecPixmap[0]?);
}
void?DiagramViews::SetBackgroundColor(?QVariant?color?)
{
//?因?yàn)樗袀€復(fù)制構(gòu)造函數(shù)(和QRgb),
//?而QRgb?=?uint?=?unsigned?int
//?所以編譯能夠成功。
setPalette(?QPalette(?color.toUInt(?)?)?);
}
評論
共有 條評論