我的班级对容器不起作用

时间:2012-10-23 16:45:09

标签: c++ qt class containers

我有一个容器有很多问题的类,这次如果我使用非默认构造函数创建我的对象,我根本无法控制容器中的对象(更改值,可见...);但如果我使用默认构造函数定义我的对象,我可以轻松完成。

Control.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 &copy);
  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 &copy);


};

controls.cpp

#include "Interface.h"
Controls &Controls::operator= (const Controls &copy)
{
  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 &copy)
{
  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()
{
}

的main.cpp

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;
}

1 个答案:

答案 0 :(得分:2)

您根本不应该拥有Controls的复制构造函数或赋值运算符,并且您应该在向量中存储类型为Controls*的值,而不是类型Controls

创建GUI对象的复制构造函数非常可疑。有一个原因QLabelQSliderQSpinBoxQHBoxLayout本身没有复制构造函数。它会使代码变得非常慢,并且很快变成混乱的混乱。

简而言之,对于从QObject继承的对象,你几乎不应该有一个复制构造函数。