【问题标题】:QInputDialog: OK and CANCEL Buttons: Removing button images for OK and CancelQInputDialog:确定和取消按钮:删除确定和取消的按钮图像
【发布时间】:2018-10-12 00:03:03
【问题描述】:

在 QInputDialog 中如何去掉 OK 和 Cancel 按钮中的图标?

注意取消和确定的图标。我查看了属性按钮无法弄清楚如何删除它们。

【问题讨论】:

    标签: c++ qt qt5 qinputdialog


    【解决方案1】:

    解决的策略是先获取按钮,但是这些属于QDialogButtonBox,所以你必须使用findChild()方法,然后重置图标,只有一个问题,按钮被创建必要时,例如当它可见或更改okButtonTextcancelButtonText 时。例如,我们可以通过使其可见来强制它。

    #include <QApplication>
    #include <QInputDialog>
    #include <QDebug>
    #include <QAbstractButton>
    #include <QDialogButtonBox>
    
    static int getInt(QWidget *parent,
                      const QString &title,
                      const QString &label,
                      int value=0,
                      int min=-2147483647,
                      int max=2147483647,
                      int step=1,
                      bool *ok=nullptr,
                      Qt::WindowFlags flags=Qt::Widget)
    {
        QInputDialog dialog(parent, flags);
        dialog.setWindowTitle(title);
        dialog.setLabelText(label);
        dialog.setIntRange(min, max);
        dialog.setIntValue(value);
        dialog.setIntStep(step);
        dialog.setVisible(true);
        for(QAbstractButton *btn:  dialog.findChild<QDialogButtonBox*>()->buttons()){
            btn->setIcon(QIcon());
        }
        int ret = dialog.exec();
        if (ok)
            *ok = !!ret;
        if (ret) {
            return dialog.intValue();
        } else {
            return value;
        }
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        bool ok;
        int res = getInt(nullptr, "Factorial Calc", "Factorial of:", 5, 0, 100, 1, &ok);
        if(ok)
            qDebug()<< res;
        return 0;
    }
    

    但是如果你使用像QInputDialog::getInt() 这样的静态方法,我们将无法直接访问QInputDialog,我们必须在显示QInputDialogQTimer 之后进行操作,所以有两种情况:

    • 将父级传递给静态方法:

    #include <QApplication>
    #include <QInputDialog>
    #include <QDebug>
    #include <QAbstractButton>
    #include <QDialogButtonBox>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget widget;
        bool ok;
        QTimer::singleShot(0, [&widget](){
            QInputDialog *dialog = widget.findChild<QInputDialog *>();
            for(QAbstractButton *btn:  dialog->findChild<QDialogButtonBox*>()->buttons()){
                btn->setIcon(QIcon());
            }
        });
        int res = QInputDialog::getInt(&widget, "Factorial Calc", "Factorial of:", 5, 0, 100, 1, &ok);
        if(ok)
            qDebug()<< res;
        return 0;
    }
    
    • 父级未传递给静态方法:

    #include <QApplication>
    #include <QInputDialog>
    #include <QDebug>
    #include <QAbstractButton>
    #include <QDialogButtonBox>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget widget;
        bool ok;
        QTimer::singleShot(0, [](){
            for(QWidget *widget: QApplication::topLevelWidgets()){
                if(QInputDialog *dialog = qobject_cast<QInputDialog*>(widget)){
                    for(QAbstractButton *btn:  dialog->findChild<QDialogButtonBox*>()->buttons()){
                        btn->setIcon(QIcon());
                    }
                }
            }
        });
        int res = QInputDialog::getInt(nullptr, "Factorial Calc", "Factorial of:", 5, 0, 100, 1, &ok);
        if(ok)
            qDebug()<< res;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      相关资源
      最近更新 更多