In QInputDialog how do I get rid of the icons in the OK and Cancel buttons?
Notice the the icons for cancel and ok. I looked through the properties button couldn't figure out how to remove them.
The strategy of the solution is to first obtain the buttons, but these belong to a QDialogButtonBox
, so you must use the findChild()
method, then reset the icons, there is only one problem, that the buttons are created when necessary, for example when it is visible or when you change the okButtonText
or cancelButtonText
. For example in the we can force it by making it visible.
#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;
}
But if you use static methods like QInputDialog::getInt()
we will not be able to access the QInputDialog
directly, we have to do it a moment after showing the QInputDialog
with a QTimer
, so there are 2 cases:
#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;
}