QT的信息框弹出来以后,往往按钮都是英文的,而这个体验不是很好。我们需要实现的状态如图:
那么如何实现呢?
看到网上说用setbuttontext()方法,这个是不可行的,因为官方文档有这么一句话:
Sets the text of the message box button button to text. Setting the text of a button that is not in the message box is silently ignored.
Use addButton() instead.
这句话的意思说你用这个函数的时候,会被对话框默认忽略,请使用addbutton()函数来代替。
QMessageBox megBox;
megBox.addButton("是",QMessageBox::AcceptRole);
megBox.addButton("否",QMessageBox::RejectRole);
megBox.setText("退出游戏?");
int ret= megBox.exec();
switch(ret){
case QMessageBox::AcceptRole:
this->close();
break;
default:
break;
}
如上的代码,其实是给它添加了一个自定义的按钮,同时捕获用户单击的按钮返回值,这样,就可以实现中文的按钮啦
原文地址:https://my.oschina.net/u/2505464/blog/542385