Colour bars/Display: Difference between revisions

Content added Content deleted
No edit summary
Line 84: Line 84:
ExitApp
ExitApp
Return</lang>
Return</lang>

=={{header|C++}}==
using Qt 4.6
<PRE>file colorbars.h</PRE>
<lang C++>#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>

class QPaintEvent ;

class MyWidget : public QWidget {
public :
MyWidget( ) ;

protected :
void paintEvent( QPaintEvent * ) ;
private :
int width ;
int height ;
const int colornumber ;
} ;
#endif</lang>
<PRE>file colorbars.cpp</PRE>
<lang C++>#include <QtGui>
#include "colorbars.h"

MyWidget::MyWidget( ) :
width( 640 ) ,
height( 240 ) ,
colornumber( 8 ) {
setGeometry( 0, 0 , width , height ) ;
}

void MyWidget::paintEvent ( QPaintEvent * ) {
int rgbtriplets[ ] = { 0 , 0 , 0 , 255 , 0 , 0 , 0 , 255 , 0 ,
0 , 0 , 255 , 255 , 0 , 255 , 0 , 255 , 255 , 255 , 255 , 0 ,
255 , 255 , 255 } ;
QPainter myPaint( this ) ;
int rectwidth = width / colornumber ; //width of one rectangle
int xstart = 1 ; //x coordinate of the first rectangle
int offset = -1 ; //to allow for ++offset to define the red value even in the first run of the loop below
for ( int i = 0 ; i < colornumber ; i++ ) {
QColor rectColor ;
rectColor.setRed( rgbtriplets[ ++offset ] ) ;
rectColor.setGreen( rgbtriplets[ ++offset ] ) ;
rectColor.setBlue( rgbtriplets[ ++offset ] ) ;
myPaint.fillRect( xstart , 0 , rectwidth , height - 1 , rectColor ) ;
xstart += rectwidth + 1 ;
}
}</lang>
<PRE>file main.cpp</PRE>
<lang C++>#include <QApplication>
#include "colorbars.h"

int main( int argc, char * argv[ ] ) {
QApplication app( argc , argv ) ;
MyWidget window ;
window.setWindowTitle( QApplication::translate( "colorslides" , "color slides demonstration" ) ) ;
window.show( ) ;
return app.exec( ) ;
}</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==