C++ 如何在Qt中弹出消息窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20841081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:21:22  来源:igfitidea点击:

How to pop up a message window in Qt?

c++qt

提问by unix_kid

I have to pop up a message in Qt when a particular test case is executed. Since I am a beginner in Qt, I do not want to risk trying with a qml...

当执行特定的测试用例时,我必须在 Qt 中弹出一条消息。由于我是 Qt 的初学者,我不想冒险尝试使用 qml ...

How can I do it (directly in .cpp file) without creating a qml file?

如何在不创建 qml 文件的情况下(直接在 .cpp 文件中)执行此操作?

回答by Andrea

If you want to display a simple message, you can use a QMessageBox::information.

如果要显示简单的消息,可以使用QMessageBox::information

Following the provided link, you can call a message box of that type this way:

按照提供的链接,您可以通过以下方式调用该类型的消息框:

QMessageBox::information( 
    this, 
    tr("Application Name"), 
    tr("An information message.") );

Edit:Since this question had a lot of visits during these years, I just wanted to include the other types of message for the sake of information (again, taken by the link above):

编辑:由于这些年来这个问题有很多访问量,我只是想包括其他类型的消息以提供信息(同样,通过上面的链接获取):

QMessageBox::warning( 
    this, 
    tr("Application Name"), 
    tr("A warning message.") );

QMessageBox::critical( 
  this, 
  tr("Application Name"), 
  tr("A critical message.") );

switch( QMessageBox::question( 
            this, 
            tr("Application Name"), 
            tr("An information message."), 

            QMessageBox::Yes | 
            QMessageBox::No | 
            QMessageBox::Cancel, 

            QMessageBox::Cancel ) )
{
  case QMessageBox::Yes:
    qDebug( "yes" );
    break;
  case QMessageBox::No:
    qDebug( "no" );
    break;
  case QMessageBox::Cancel:
    qDebug( "cancel" );
    break;
  default:
    qDebug( "close" );
    break;
}