如图,通过左侧的三个滑动条设置红绿蓝三色的值,然后改变右侧textLabel的
背景色。
本人初学者,通过QT画了个
界面,就设置了spinBox与滑动条的信号与槽的连接,.cpp与.h文件都是自动生成的;
要实现上述功能,要添加怎样的代码、要添到何处呢?求大神指教
.cpp
#include "test02.h"
#include <qvariant.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qslider.h>
#include <qspinbox.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )
: QDialog( parent, name, modal, fl )
{
if ( !name )
setName( "Form1" );
lineEdit1 = new QLineEdit( this, "lineEdit1" );
lineEdit1->setGeometry( QRect( 420, 60, 101, 91 ) );
textLabel1_2 = new
QLabel( this, "textLabel1_2" );
textLabel1_2->setGeometry( QRect( 420, 210, 101, 20 ) );
textLabel1_3 = new QLabel( this, "textLabel1_3" );
textLabel1_3->setGeometry( QRect( 420, 230, 101, 20 ) );
textLabel1 = new QLabel( this, "textLabel1" );
textLabel1->setGeometry( QRect( 420, 190, 101, 20 ) );
slider1_3 = new QSlider( this, "slider1_3" );
slider1_3->setGeometry( QRect( 170, 150, 191, 31 ) );
slider1_3->setMaxValue( 255 );
slider1_3->setOrientation( QSlider::Horizontal );
slider1_2 = new QSlider( this, "slider1_2" );
slider1_2->setGeometry( QRect( 170, 100, 191, 31 ) );
slider1_2->setMaxValue( 255 );
slider1_2->setOrientation( QSlider::Horizontal );
slider1 = new QSlider( this, "slider1" );
slider1->setGeometry( QRect( 170, 50, 191, 31 ) );
slider1->setMaxValue( 255 );
slider1->setOrientation( QSlider::Horizontal );
spinBox1 = new QSpinBox( this, "spinBox1" );
spinBox1->setGeometry( QRect( 70, 50, 101, 31 ) );
spinBox1->setMaxValue( 255 );
spinBox1_2 = new QSpinBox( this, "spinBox1_2" );
spinBox1_2->setGeometry( QRect( 70, 100, 101, 31 ) );
spinBox1_2->setMaxValue( 255 );
spinBox1_3 = new QSpinBox( this, "spinBox1_3" );
spinBox1_3->setGeometry( QRect( 70, 150, 101, 31 ) );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
clearWState( WState_Polished );
// signals and slots connections
connect( spinBox1,
SIGNAL( valueChanged(int) ), slider1, SLOT( setValue(int) ) );
connect( slider1, SIGNAL( sliderMoved(int) ), spinBox1, SLOT( setValue(int) ) );
connect( spinBox1, SIGNAL( valueChanged(int) ), lineEdit1, SLOT( selectAll() ) );
connect( spinBox1_2, SIGNAL( valueChanged(int) ), slider1_2, SLOT( setValue(int) ) );
connect( slider1_2, SIGNAL( valueChanged(int) ), spinBox1_2, SLOT( setValue(int) ) );
connect( spinBox1_3, SIGNAL( valueChanged(int) ), slider1_3, SLOT( setValue(int) ) );
connect( slider1_3, SIGNAL( valueChanged(int) ), spinBox1_3, SLOT( setValue(int) ) );
connect( spinBox1, SIGNAL( valueChanged(int) ), lineEdit1, SLOT( setAlignment(int) ) );
connect( spinBox1, SIGNAL( valueChanged(int) ), textLabel1, SLOT( setNum(int) ) );
connect( spinBox1_2, SIGNAL( valueChanged(int) ), textLabel1_2, SLOT( setNum(int) ) );
connect( spinBox1_3, SIGNAL( valueChanged(int) ), textLabel1_3, SLOT( setNum(int) ) );
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
textLabel1_2->setText( tr( "Red" ) );
textLabel1_3->setText( tr( "Red" ) );
textLabel1->setText( tr( "Red" ) );
}
.h
#ifndef FORM1_H
#define FORM1_H
#include <qvariant.h>
#include <qdialog.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLineEdit;
class QLabel;
class QSlider;
class QSpinBox;
class Form1 : public QDialog
{
Q_OBJECT
public:
Form1( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~Form1();
QLineEdit* lineEdit1;
QLabel* textLabel1_2;
QLabel* textLabel1_3;
QLabel* textLabel1;
QSlider* slider1_3;
QSlider* slider1_2;
QSlider* slider1;
QSpinBox* spinBox1;
QSpinBox* spinBox1_2;
QSpinBox* spinBox1_3;
virtual void color();
protected:
protected slots:
virtual void languageChange();
};
#endif // FORM1_H