The secondary confirmation function is added to QT, and qmessagebox is used.

Source: Internet
Author: User
Tags button type

For some important operations, you need to confirm them again and provide several basic instances.

Yes and no

[CPP] View plaincopyprint?

Switch (qmessagebox: Warning (null, "warning", qstring: fromlocal8bit ("are you sure you want to import the file channel? "),

Qmessagebox: Yes | qmessagebox: default,

Qmessagebox: No | qmessagebox: escape ))

{

Case qmessagebox: Yes:

Break;

Case qmessagebox: No:

Return;

}

    switch( QMessageBox::warning(NULL, "warning",QString::fromLocal8Bit("确定导入文件的频道吗?"),                                 QMessageBox::Yes | QMessageBox::Default,                                 QMessageBox::No | QMessageBox::Escape ))    {    case QMessageBox::Yes:        break;    case QMessageBox::No:        return;    }

File not saved

[CPP] View plaincopyprint?

Switch (qmessagebox: Information (this, "Application name here ",

"The document contains unsaved changes \ n"

"Do you want to save the changes before exiting? ",

"& Save", "& discard", "cancel ",

0, // enter = button 0

2) {// escape = button 2

Case 0: // save is clicked, ALT + S is pressed, or enter is pressed.

// Save

Break;

Case 1: // The discard is clicked or the Alt + D is pressed.

// Do not save but exit

Break;

Case 2: // cancel is clicked, ALT + C is pressed, or escape is pressed.

// Do not exit

Break;

}

switch( QMessageBox::information( this, "Application name here",        "The document contains unsaved changes\n"        "Do you want to save the changes before exiting?",        "&Save", "&Discard", "Cancel",        0,      // Enter == button 0        2 ) ) { // Escape == button 2    case 0: // Save被点击或者Alt+S被按下或者Enter被按下。        // 保存        break;    case 1: // Discard被点击或者Alt+D被按下。        // 不保存但退出        break;    case 2: // Cancel被点击或者Alt+C被按下或者Escape被按下。        // 不退出        break;}

Retry and discard

[CPP] View plaincopyprint?

Switch (qmessagebox: Warning (this, "Application name here ",

"Cocould not save the user preferences, \ n"

"Because the disk is full. You can delete \ n"

"Some files and press retry, or you can \ n"

"Abort the Save preferences operation .",

Qmessagebox: retry | qmessagebox: default,

Qmessagebox: Abort | qmessagebox: escape )){

Case qmessagebox: retry: // The retry is clicked or the enter is pressed.

// Try again

Break;

Case qmessagebox: Abort: // abort is clicked or escape is pressed.

// Discard

Break;

}

switch( QMessageBox::warning( this, "Application name here",        "Could not save the user preferences,\n"        "because the disk is full.  You can delete\n"        "some files and press Retry, or you can\n"        "abort the Save Preferences operation.",        QMessageBox::Retry | QMessageBox::Default,        QMessageBox::Abort | QMessageBox::Escape )) {    case QMessageBox::Retry: // Retry被点击或者Enter被按下。        // 再次尝试        break;    case QMessageBox::Abort: // Abort被点击或者Escape被按下。        // 放弃        break;}

Custom prompts

[CPP] View plaincopyprint?

If no standard message box is available, you can manually create a qmessagebox and define the button text:

Qmessagebox MB ("Application name here ",

"Saving the file will overwrite the original file on the disk. \ n"

"Do you really want to save? ",

Qmessagebox: information,

Qmessagebox: Yes | qmessagebox: default,

Qmessagebox: No,

Qmessagebox: Cancel | qmessagebox: escape );

MB. setbuttontext (qmessagebox: Yes, "save ");

MB. setbuttontext (qmessagebox: No, "discard ");

Switch (mb.exe C ()){

Case qmessagebox: Yes:

// Save and exit

Break;

Case qmessagebox: No:

// Exit without saving

Break;

Case qmessagebox: Cancel:

// Do not save and do not exit

Break;

}

如果没有任何标准消息框是合适地,你可以自己手工创建一个QMessageBox并且自己定义按钮文本: QMessageBox mb( "Application name here",        "Saving the file will overwrite the original file on the disk.\n"        "Do you really want to save?",        QMessageBox::Information,        QMessageBox::Yes | QMessageBox::Default,        QMessageBox::No,        QMessageBox::Cancel | QMessageBox::Escape );mb.setButtonText( QMessageBox::Yes, "Save" );mb.setButtonText( QMessageBox::No, "Discard" );switch( mb.exec() ) {    case QMessageBox::Yes:        // 保存并且退出        break;    case QMessageBox::No:        // 不保存退出        break;    case QMessageBox::Cancel:        // 不保存并且不退出        break;}

Other problems

Button types:

    • OK-default value in the single-button message box
    • Cancel-pay attention to thisNoAutomatically associated with escape
    • Yes
    • No
    • Abort
    • Retry
    • Ignore

The button type can be combined by the above type and the following two modifiers or "|.

    • Default-let the press ENTER be equal to clicking this button. Usually used for OK, yes, or other similar.
    • Escape-Press the Escape key to click this button. It is usually used for abort, cancel, or other similar

The secondary confirmation function is added to QT, and qmessagebox is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.