QDialog在hide()之后,就被销毁的原因

int QDialog::exec()
{
    Q_D(QDialog);
 
    if (d->eventLoop) {
        qWarning("QDialog::exec: Recursive call detected");
        return -1;
    }
 
    bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
    setAttribute(Qt::WA_DeleteOnClose, false);
 
    d->resetModalitySetByOpen();
 
    bool wasShowModal = testAttribute(Qt::WA_ShowModal);
    setAttribute(Qt::WA_ShowModal, true);
    setResult(0);
 
    show();
 
    QPointer<QDialog> guard = this;
    if (d->nativeDialogInUse) {
        d->platformHelper()->exec();
    } else {
        QEventLoop eventLoop;
        d->eventLoop = &eventLoop;
        (void) eventLoop.exec(QEventLoop::DialogExec);
    }
    if (guard.isNull())
        return QDialog::Rejected;
    d->eventLoop = 0;
 
    setAttribute(Qt::WA_ShowModal, wasShowModal);
 
    int res = result();
    if (d->nativeDialogInUse)
        d->helperD
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用 QDialog 在界面输入数字并将其调用,可以按照以下步骤进行操作: 1. 创建一个继承自 QDialog 的自定义对话框类。在对话框的头文件中,声明一个私有的 QLineEdit 成员变量用于接收用户输入的数字。 ```cpp class NumberInputDialog : public QDialog { Q_OBJECT public: explicit NumberInputDialog(QWidget *parent = nullptr); int getNumber() const; // 获取用户输入的数字 private: QLineEdit *m_numberLineEdit; // 用于接收数字输入的文本框 }; ``` 2. 在对话框的源文件中,实现构造函数和获取数字的函数。 ```cpp NumberInputDialog::NumberInputDialog(QWidget *parent) : QDialog(parent) { m_numberLineEdit = new QLineEdit(this); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_numberLineEdit); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); layout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } int NumberInputDialog::getNumber() const { bool ok; int number = m_numberLineEdit->text().toInt(&ok); if (!ok) { // 处理无效输入的情况 // 这里可以考虑弹出一个错误提示框或采取其他处理方式 } return number; } ``` 3. 在需要调用对话框的地方,创建一个 NumberInputDialog 对象并使用 exec() 函数来显示对话框: ```cpp NumberInputDialog dialog; if (dialog.exec() == QDialog::Accepted) { int number = dialog.getNumber(); // 在这里可以使用获取到的数字进行后续操作 } ``` 通过这种方式,你可以在界面中输入数字并在对话框被调用时获取到该数字。 希望这个解决方案对你有帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值