• 4793阅读
  • 5回复

[提问]怎样实现改变控件的背景颜色 [复制链接]

上一主题 下一主题
离线ldqsingle
 
只看楼主 倒序阅读 楼主  发表于: 2013-01-08

如图,通过左侧的三个滑动条设置红绿蓝三色的值,然后改变右侧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



离线ldqsingle
只看该作者 1楼 发表于: 2013-01-08
通过
textLabel1->setBackgroundColor(QColor(100,100,100));
可以实现背景颜色设置
然后通过
a = spinBox1->text().toDouble();
可以获得spinBox里的值
最后改成
textLabel1->setBackgroundColor(QColor(a,b,c));
就不行了
离线ldqsingle
只看该作者 2楼 发表于: 2013-01-09
重新qmake一下功能就实现了
离线ldqsingle
只看该作者 3楼 发表于: 2013-01-09
我又添加了一个comboBox控件,下拉菜单中有红橙黄绿等7种颜色,选择每一种颜色就会显示所选的颜色。
comboBox返回的值是QString类型的,不能用switch语句
各路大神,求指教
离线ldqsingle
只看该作者 4楼 发表于: 2013-01-09
test02.rar (47 K) 下载次数:5
离线realfan

只看该作者 5楼 发表于: 2013-01-09
回 3楼(ldqsingle) 的帖子
不用switch可以用if ...else if....
如果顺序固定,也可以根据    currentIndex 对应颜色
还也可以为每个item设置一个值,例如
cbo->addItem ( “red”, 1 );
cbo->addItem ( “green”, 2 );
然后,获取当前选中的item,    itemData(currentIndex).toInt()等到数值,再switch
快速回复
限100 字节
 
上一个 下一个