QComboBox с линиями
Делал примерно так:
Код:
cLineStyleComboBox::cLineStyleComboBox( QWidget * parent )
: QComboBox( parent )
, width_( 92 )
, height_( 16 )
, thickness_( 0 )
, color_( QColor( Qt::black ) )
, styles_( QList< Qt::PenStyle >() )
{
setCurrentIndex( 0 );
setIconSize( QSize( width_, height_ ) );
styles_ << Qt::SolidLine << Qt::DashLine << Qt::DotLine << Qt::DashDotLine << Qt::DashDotDotLine;
for( int i = 0; i < styles_.count(); i++ )
addItem( QString() );
}
cLineStyleComboBox::~cLineStyleComboBox()
{
}
void cLineStyleComboBox::setLineThickness( double thickness )
{
if( thickness_ != thickness )
{
thickness_ = thickness;
repaint();
}
}
void cLineStyleComboBox::setLineColor( QColor color )
{
if( color != color_ )
{
color_ = color;
repaint();
}
}
void cLineStyleComboBox::paintEvent( QPaintEvent * event )
{
QBrush brush( color_ );
for( int i = 0; i < styles_.count(); i++ )
{
QBitmap pix( iconSize() );
pix.clear();
QPainter painter( &pix );
painter.setPen( QPen( brush, thickness_, styles_[ i ], Qt::RoundCap ) );
painter.drawLine( 2, height_ / 2, width_ - 2, height_ / 2 );
setItemIcon( i, pix );
}
QStylePainter stylePainter(this);
QStyleOptionComboBox opt;
initStyleOption(&opt);
stylePainter.drawComplexControl(QStyle::CC_ComboBox, opt);
QRect rect = stylePainter.style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
int height = rect.height();
int width = rect.width();
stylePainter.setPen( QPen( brush, thickness_, styles_[ currentIndex() ] , Qt::RoundCap ) );
stylePainter.drawLine( 10, height / 2 + 2, width - 2, height / 2 + 2 );
}
: QComboBox( parent )
, width_( 92 )
, height_( 16 )
, thickness_( 0 )
, color_( QColor( Qt::black ) )
, styles_( QList< Qt::PenStyle >() )
{
setCurrentIndex( 0 );
setIconSize( QSize( width_, height_ ) );
styles_ << Qt::SolidLine << Qt::DashLine << Qt::DotLine << Qt::DashDotLine << Qt::DashDotDotLine;
for( int i = 0; i < styles_.count(); i++ )
addItem( QString() );
}
cLineStyleComboBox::~cLineStyleComboBox()
{
}
void cLineStyleComboBox::setLineThickness( double thickness )
{
if( thickness_ != thickness )
{
thickness_ = thickness;
repaint();
}
}
void cLineStyleComboBox::setLineColor( QColor color )
{
if( color != color_ )
{
color_ = color;
repaint();
}
}
void cLineStyleComboBox::paintEvent( QPaintEvent * event )
{
QBrush brush( color_ );
for( int i = 0; i < styles_.count(); i++ )
{
QBitmap pix( iconSize() );
pix.clear();
QPainter painter( &pix );
painter.setPen( QPen( brush, thickness_, styles_[ i ], Qt::RoundCap ) );
painter.drawLine( 2, height_ / 2, width_ - 2, height_ / 2 );
setItemIcon( i, pix );
}
QStylePainter stylePainter(this);
QStyleOptionComboBox opt;
initStyleOption(&opt);
stylePainter.drawComplexControl(QStyle::CC_ComboBox, opt);
QRect rect = stylePainter.style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
int height = rect.height();
int width = rect.width();
stylePainter.setPen( QPen( brush, thickness_, styles_[ currentIndex() ] , Qt::RoundCap ) );
stylePainter.drawLine( 10, height / 2 + 2, width - 2, height / 2 + 2 );
}