QInputDialog 使用方法
在Qt中,如果想快速生成一个对话框,可以和用户进行简单的交互,而不需要写一个新的类的时候,就要用到QInputDialog类,这个类就是专门用来建立简单对话框的,其主要能建下列几种对话框:
bool ok; double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"), tr("Amount:"), 37.56, -10000, 10000, 2, &ok); if (ok) doubleLabel->setText(QString("$%1").arg(d));
bool ok; int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"), tr("Percentage:"), 25, 0, 100, 1, &ok); if (ok) integerLabel->setText(tr("%1%").arg(i));
QStringList items; items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter"); bool ok; QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"), tr("Season:"), items, 0, false, &ok); if (ok && !item.isEmpty()) itemLabel->setText(item);
bool ok; QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"), tr("User name:"), QLineEdit::Normal, QDir::home().dirName(), &ok); if (ok && !text.isEmpty()) textLabel->setText(text);