#ifndef BARWIDGET_H
#define BARWIDGET_H
#include <QtGui>
{
Q_OBJECT
public:
explicit BarWidget
(QWidget *parent
= 0);
int value();
protected:
signals:
void valueChanged(int);
public slots:
void setValue(int);
private:
int currentValue;
};
#endif // BARWIDGET_H
#ifndef BARWIDGET_H
#define BARWIDGET_H
#include <QtGui>
class BarWidget : public QWidget
{
Q_OBJECT
public:
explicit BarWidget(QWidget *parent = 0);
int value();
protected:
void paintEvent(QPaintEvent *);
signals:
void valueChanged(int);
public slots:
void setValue(int);
private:
int currentValue;
};
#endif // BARWIDGET_H
To copy to clipboard, switch view to plain text mode
#include "barwidget.h"
BarWidget
::BarWidget(QWidget *parent
) :{
currentValue = 0;
}
int BarWidget::value()
{
return currentValue;
}
void BarWidget::setValue(int newValue)
{
if (newValue > 5)
newValue = 5;
else if (newValue < 0)
newValue = 0;
currentValue = newValue;
emit valueChanged(newValue);
repaint();
}
{
p.setPen(Qt::NoPen)
p.setBrush(palette().text()); // draw the bars using the current theme's text color
float barWidth = 0.15; // change this to the preferred width of the bars, as a fraction of the widget's width. Do not make it more than 0.20, or one fifth
float gapWidth = (1.0 - (barWidth * 5)) / 4;
switch (currentValue) // since there is no break after each case, it will fall through and draw the smaller bars
{
case 5:
p.
drawRect(QRectF( (barWidth
+gapWidth
)*4*width
(), height
()*(0/5.0), barWidth
*width
(), height
() ));
case 4:
p.
drawRect(QRectF( (barWidth
+gapWidth
)*3*width
(), height
()*(1/5.0), barWidth
*width
(), height
() ));
case 3:
p.
drawRect(QRectF( (barWidth
+gapWidth
)*2*width
(), height
()*(2/5.0), barWidth
*width
(), height
() ));
case 2:
p.
drawRect(QRectF( (barWidth
+gapWidth
)*1*width
(), height
()*(3/5.0), barWidth
*width
(), height
() ));
case 1:
p.
drawRect(QRectF( (barWidth
+gapWidth
)*0*width
(), height
()*(4/5.0), barWidth
*width
(), height
() ));
}
p.end();
}
#include "barwidget.h"
BarWidget::BarWidget(QWidget *parent) :
QWidget(parent)
{
currentValue = 0;
}
int BarWidget::value()
{
return currentValue;
}
void BarWidget::setValue(int newValue)
{
if (newValue > 5)
newValue = 5;
else if (newValue < 0)
newValue = 0;
currentValue = newValue;
emit valueChanged(newValue);
repaint();
}
void BarWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setPen(Qt::NoPen)
p.setBrush(palette().text()); // draw the bars using the current theme's text color
float barWidth = 0.15; // change this to the preferred width of the bars, as a fraction of the widget's width. Do not make it more than 0.20, or one fifth
float gapWidth = (1.0 - (barWidth * 5)) / 4;
switch (currentValue) // since there is no break after each case, it will fall through and draw the smaller bars
{
case 5:
p.drawRect(QRectF( (barWidth+gapWidth)*4*width(), height()*(0/5.0), barWidth*width(), height() ));
case 4:
p.drawRect(QRectF( (barWidth+gapWidth)*3*width(), height()*(1/5.0), barWidth*width(), height() ));
case 3:
p.drawRect(QRectF( (barWidth+gapWidth)*2*width(), height()*(2/5.0), barWidth*width(), height() ));
case 2:
p.drawRect(QRectF( (barWidth+gapWidth)*1*width(), height()*(3/5.0), barWidth*width(), height() ));
case 1:
p.drawRect(QRectF( (barWidth+gapWidth)*0*width(), height()*(4/5.0), barWidth*width(), height() ));
}
p.end();
}
To copy to clipboard, switch view to plain text mode
Bookmarks