通过按下控制台上的按钮删除创建的进度条 window

Delete the created progress bars by pushing button on the console window

我正在努力学习自己使用 Qt,但仍然很挣扎...我将如何通过按删除按钮删除我的弹跳对象?我正在努力实现删除功能...

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

}

您必须在 class header 中将 sillyobject 声明为私有指针(以访问所有 class 成员),然后您只需删除指针:

 delete sillyobject;

在您的 header 文件中:

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

然后

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

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

  bouncyobject.push_back(sillyobject);

}

和:

void MainWindow::deleteSomethingSilly() {


delete sillyobject;

}