C++ (Qt)
#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
#include <QWidget>
#include <QGridLayout>
#include <QLabel>
class ContentsLabel : public QWidget
{
public:
ContentsLabel( const QString& s, QWidget* p = 0 )
: QWidget( p )
, text_( s ) {}
QSize sizeHint() const
{
br_ = QFontMetrics( font() ).boundingRect( text_ );
return br_.size();
}
void setDotColor( const QColor& clr )
{
dot_clr_ = clr;
update();
}
protected:
void paintEvent( QPaintEvent* e )
{
QPainter p( this );
QFontMetrics fm = p.fontMetrics();
int y = ( height() - br_.height() ) / 2 + fm.ascent();
p.drawText( 0, y, text_ );
int dot_w = fm.width( '.' );
if( dot_clr_ != QColor() )
p.setPen( dot_clr_ );
for( int x = br_.width() + 1; x < width(); x += dot_w )
p.drawText( x, y, "." );
}
private:
QString text_;
mutable QRect br_;
QColor dot_clr_;
};
int main( int argc, char** argv )
{
QApplication app( argc, argv );
QWidget w;
QGridLayout l( &w );
for( int i = 0; i < 20; i++ )
{
ContentsLabel* cl = new ContentsLabel( QString( "Contents text [%1]" ).arg( i + 1 ) );
cl->setDotColor( Qt::blue );
l.addWidget( cl, i, 0 );
QLabel* pl = new QLabel( QString::number( i + 1 ) );
pl->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
l.addWidget( pl, i, 1 );
}
w.show();
return app.exec();
}