Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8723421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:37:20+00:00 2026-06-13T07:37:20+00:00

i have a class that has lot of problem with containers ,this time if

  • 0

i have a class that has lot of problem with containers ,this time if i create my objects with non default constructor function i can’t control the object in a container at all(change value , visible …) ; but if i define my object with default constructor i can do it easily .

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:37:21+00:00Added an answer on June 13, 2026 at 7:37 am

    You should not have a copy constructor or assignment operator for Controls at all, and you should be storing values of type Controls* in your vector instead of type Controls.

    A copy-constructor that creates GUI objects is highly suspect. There’s a reason QLabel, QSlider, QSpinBox and QHBoxLayout don’t have copy constructors themselves. It makes for very slow code, and quickly becomes a confusing mess.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class that has many class members, and a lot of different
I have a Class that has a private variable with a public setter/getter function:
I have a class that has several member classes as attributes. The constructor of
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have a class that has a big method that calls on a lot
I often come accross the problem that I have a class that has a
I have a class that has a vector member variable, which I fill up
I have a class that has an inner struct/class (I have decided which yet,
I have a class that has a property that I need to stub. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.