Home > OS >  Delete the created progress bars by pushing button on the console window
Delete the created progress bars by pushing button on the console window

Time:04-24

I am trying to learn myself use Qt so still struggling a lot... How would I go about deleting my bouncing object by pressing the delete button? I am struggling to implement the delete function...

MainWindow::MainWindow(QWidget * parent): QMainWindow(parent) {

  setFixedSize(800, 600);
  int start;

  timer  = new QTimer(this);
  connect(timer , SIGNAL(timeout()), this, SLOT(step()));

  timer  -> setInterval(5);
  timer  -> start();

  QPushButton * sillyLabel  = new QPushButton(this);
  connect(sillyLabel , SIGNAL(clicked()), this, SLOT(doSomethingSilly()));

  sillyLabel  -> setText("Spawn Object");
  sillyLabel  -> setGeometry(400, 400, 200, 50);


  QPushButton * dLabel  = new QPushButton(this);
  connect(dLabel , SIGNAL(clicked()), this, SLOT(deleteSomethingSilly()));

  dLabel  -> setText("Delete Object");
  dLabel  -> setGeometry(200, 200, 200, 50);

MainWindow::~MainWindow() {

}

void MainWindow::doSomethingSilly() {
  QProgressBar * sillyobject = new QProgressBar(this);

  sillyobject -> setGeometry(400, 300, 80, 50);
  sillyobject -> show();
  sillyobject -> setValue(100);

  bouncyobject.push_back(sillyobject);

}

void MainWindow::deleteSomethingSilly() {


delete ?;

}

CodePudding user response:

You have to declare sillyobject inside the class header as a private pointer (to have access inside all the class members), then you just have to delete the pointer with :

 delete sillyobject;

In your header file :

 (...)
 private :
 QProgressBar * sillyobject 
 (...)

then

void MainWindow::doSomethingSilly() {
   sillyobject = new QProgressBar(this);

  sillyobject -> setGeometry(400, 300, 80, 50);
  sillyobject -> show();
  sillyobject -> setValue(100);

  bouncyobject.push_back(sillyobject);

}

and :

void MainWindow::deleteSomethingSilly() {


delete sillyobject;

}
  • Related