如何在Qt中弹出一个消息窗口?

时间:2021-09-18 07:16:25

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文件中)执行此操作?

1 个解决方案

#1


20  

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

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

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;
}

#1


20  

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

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

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;
}