-3

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

控制.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);


};

控件.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()
{
}

主文件

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

1 回答 1

2

您根本不应该有复制构造函数或赋值运算符Controls,并且应该将 type 的值存储Controls*在 vector 而不是 type 中Controls

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

In short, you should almost never have a copy-constructor for objects that inherit from QObject.

于 2012-10-23T19:10:34.373 回答