【问题标题】:How to I make QMessageBox delete the content of a vector如何让 QMessageBox 删除向量的内容
【发布时间】:2021-11-27 22:55:48
【问题描述】:

我对代码很陌生,所以如果这是一个简单的问题,我很抱歉,但我很挣扎。 我有一个 GUI 程序,可以将用户输入保存到向量中,然后显示它,然后可以将其保存为 txt 文件。保存后,我希望出现一个 QMessageBox 询问用户是否希望删除现在保存的矢量数据。该向量被命名为 v_History。

QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
msgBox.exec();    

非常感谢任何帮助或建议。 谢谢

【问题讨论】:

  • 查看文档中的示例:https://doc.qt.io/qt-5/qmessagebox.htmlswitch(ret) { 开头,您需要使用auto ret = msgBox.exec();int ret = msgBox.exec(); 而不是msgBox.exec();,然后使用与文档相同的技术。
  • 谢谢 - 我已经取得了一些进展,但仍然无法弄清楚按下按钮后如何分配我需要的结果。我现在有这样的东西,当然,它不起作用 - 但它接近我想要实现的目标:msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); int ret = msgBox.exec(); msgBox.exec(); if (QMessageBox::Yes.clicked() == clear.v_history) { } else if
  • @TheNuck 你打电话给msgBox.exec() 两次。只调用一次。然后比较它返回的值,例如:int ret = msgBox.exec(); if (ret == QMessageBox::Yes) { /* clear the history here */ }

标签: c++ user-interface vector qmessagebox


【解决方案1】:

使用setStandardButtons()时,exec()返回一个值,表示点击了哪个标准按钮,例如:

QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
int ret = msgBox.exec();
if (ret == QMessageBox::Discard)
{
    // delete the history as needed ...
}

【讨论】:

  • 现在似乎可以工作了,非常感谢大家!
猜你喜欢
  • 2018-06-07
  • 2020-12-03
  • 2015-01-11
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2015-08-27
相关资源
最近更新 更多