我有一个与容器有很多问题的类,这次如果我使用非默认构造函数创建对象,我根本无法控制容器中的对象(更改值,可见 ...);但是如果我用默认构造函数定义我的对象,我可以很容易地做到这一点。
控制.h
class Controls : public QObject
{
private:
QHBoxLayout *Layout ;
string Controlname;
QLabel *Label ;
QSpinBox *Spin ;
public:
QSlider *Slider ;
Controls(QLayout &Parent , string name , const int &Default_value);
Controls(const Controls ©);
Controls();
Controls(QLayout &Parent);
~Controls();
QLabel * Get_Label()const { return Label ; }
QSlider * Get_Slider()const { return Slider ; }
QSpinBox * Get_Spin()const { return Spin ; }
QHBoxLayout * Get_Layout() {return Layout;}
void SetValue(const int &newvalue);
Controls &operator= (const Controls ©);
};
控件.cpp
#include "Interface.h"
Controls &Controls::operator= (const Controls ©)
{
Slider->setValue(copy.Get_Slider()->value());
Slider->setOrientation(Qt::Horizontal);
Label->setText(copy.Get_Label()->text());
Spin->setValue(copy.Get_Spin()->value());
Layout->addWidget(Label , 0 , 0);
Layout->addWidget(Slider , 0 , 0);
Layout->addWidget(Spin , 0 , 0);
QObject::connect(Slider , SIGNAL(valueChanged(int) ) ,
Spin , SLOT(setValue(int)));
QObject::connect(Spin , SIGNAL(valueChanged(int) ) ,
Slider , SLOT(setValue(int)));
return *this;
}
Controls::Controls(const Controls ©)
{
Label = new QLabel() ;
Slider = new QSlider() ;
Spin = new QSpinBox() ;
Layout = new QHBoxLayout();
Slider->setValue(copy.Get_Slider()->value());
Slider->setOrientation(Qt::Horizontal);
Label->setText(copy.Get_Label()->text());
Spin->setValue(copy.Get_Spin()->value());
Layout->addWidget(Label , 0 , 0);
Layout->addWidget(Slider , 0 , 0);
Layout->addWidget(Spin , 0 , 0);
QObject::connect(Slider , SIGNAL(valueChanged(int) ) ,
Spin , SLOT(setValue(int)));
QObject::connect(Spin , SIGNAL(valueChanged(int) ) ,
Slider , SLOT(setValue(int)));
}
Controls::Controls()
{
Label = new QLabel() ;
Slider = new QSlider() ;
Spin = new QSpinBox() ;
Layout = new QHBoxLayout();
Slider->setValue(0);
Slider->setOrientation(Qt::Horizontal);
Label->setText(QString ("unamed"));
Spin->setValue(0);
Layout->addWidget(Label , 0 , 0);
Layout->addWidget(Slider , 0 , 0);
Layout->addWidget(Spin , 0 , 0);
QObject::connect(Slider , SIGNAL(valueChanged(int) ) ,
Spin , SLOT(setValue(int)));
QObject::connect(Spin , SIGNAL(valueChanged(int) ) ,
Slider , SLOT(setValue(int)));
}
Controls::Controls(QLayout &Parent , string name , const int &Default_value)
{
Controlname = name ;
Label = new QLabel() ;
Slider = new QSlider() ;
Spin = new QSpinBox() ;
Layout = new QHBoxLayout();
Slider->setValue(Default_value);
Slider->setOrientation(Qt::Horizontal);
Label->setText(QString (name.c_str()));
Spin->setValue(Default_value);
Layout->addWidget(Label , 0 , 0);
Layout->addWidget(Slider , 0 , 0);
Layout->addWidget(Spin , 0 , 0);
QObject::connect(Slider , SIGNAL(valueChanged(int) ) ,
Spin , SLOT(setValue(int)));
QObject::connect(Spin , SIGNAL(valueChanged(int) ) ,
Slider , SLOT(setValue(int)));
Parent.addItem(Layout);
}
void Controls::SetValue(const int &newvalue)
{
Slider->setValue(newvalue);
}
Controls::~Controls()
{
}
主文件
int main()
{
QApplication app (argc , argv );
QVBoxLayout layout ;
QLabel Image ;
QWidget Panel ;
Camera Cm ;
vector <Controls> g ;
g.push_back(Controls(layout ,"c1" , 33));
g.push_back(Controls(layout ,"c2" , 13));
g.push_back(Controls(layout ,"c3" , 63));
g.push_back(Controls(layout ,"c1" , 33));
for ( int x =0 ; x<g.size() ; x++)
{
g.at(x).SetValue(33); ////not work
}
vector <Controls> g2 ;
g2.push_back(Controls());
g2.push_back(Controls());
g2.push_back(Controls());
g2.push_back(Controls());
for ( int x =0 ; x<g2.size() ; x++)
{
layout.addItem(g2.at(x).Get_Layout());
g2.at(x).SetValue(33); ////this work
}
Panel.setLayout(&layout);
Panel. show();
return app.exec();
return 0;
}