我正在使用qt
,正在尝试使用该QInputDialog::getText()
功能从用户那里获取输入,根据文档,该功能的定义为:
QString QInputDialog::getText(QWidget * parent, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::WindowFlags flags = 0, Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
bool ok=0;
newAddress = QInputDialog::getText(0,"Enter an Address to Validate",
"Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)"
,&ok);
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
error: no matching function for call to 'QInputDialog::getText(int, const char [29], const char [80], bool*)'
,&ok);
^
Run Code Online (Sandbox Code Playgroud)
但是当我在参数之前传递所有*ok
参数时,例如:
bool ok=0;
newAddress = QInputDialog::getText(0,"Enter an Address to Validate",
"Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)"
,QLineEdit::Normal,
QString(),&ok);
Run Code Online (Sandbox Code Playgroud)
有用。
我真的不明白为什么我不能更改我想要的默认参数,而将其余参数保留为默认值?
P.W*_*P.W 14
当传递具有默认参数的特定参数的值时,必须在其之前传递所有默认参数的值。否则,您传递的值将用作第一个默认参数的值。
因此,您必须这样做:
newAddress = QInputDialog::getText(
0,
"Enter an Address to Validate",
"Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)",
QLineEdit::Normal,
QString(),
&ok);
Run Code Online (Sandbox Code Playgroud)
您可以在参数之后省略参数的传递值bool *
。
C ++标准在 [dcl.fct.default]/1
默认参数将在缺少尾部参数的调用中使用。
在C ++中,您只能在参数列表的末尾使用(一个或多个)默认参数。如果在中间省略参数,则编译器将无法知道哪个参数属于哪个参数。因此,必须QLineEdit::Normal and QString()
在传递之前手动指定默认参数&ok
。
在您无法使用的情况下,编译器将尝试使bool指针与参数列表中的下一个类型匹配QLineEdit::EchoMode
,因此不兼容。
归档时间: |
|
查看次数: |
774 次 |
最近记录: |