Lesson3-Qt dialog box _ PHP Tutorial

Source: Internet
Author: User
Tags getcolor
Lesson3-Qt dialog box. Lesson3-Qt dialog box 1, QDialog class 1, the concept of the dialog box is used in a variety of software, is generally used to give the user prompt information or receive user feedback information, so the lesson3-Qt dialog box
I. QDialog class
1. dialog box concept
The dialog box is used in various software and is generally used to prompt users or receive user feedback. Therefore, the dialog box is a platform for applications to interact with users.
A dialog box is a top-level window and cannot be embedded in other windows.
2. dialog box types
1) in the "mode" dialog box, other windows of the application cannot be accessed. you must wait for the current dialog box to disappear. the "Display mode" dialog box generally calls its exec () function.
2) in the non-mode dialog box, other windows of the application can be accessed. the show () function is generally called in the non-mode dialog box.
3. the parent class of the QDialog class is QWidget.





II. derived class of QDialog
To facilitate the use of developers, Qt encapsulates some special function dialogs and provides a set of standard dialogs. These built-in dialog boxes provide static functions for ease of use. Generally, they call the system's local dialog box.
1. QFileDialog

Usage:
1. open the file dialog box and return the selected file name
QString str = QFileDialog: getOpenFileName (
Parent window,
Dialog box name,
Path selected by default,
File Filter );
2. if the file is successfully opened by name, true is returned; otherwise false is returned.
QFile file (str );
File. open (QIODevice: ReadWrite );
3. get an input stream
QTextStream in (& file );
4. Read the input stream line by line
In. readLine ();


2. QColorDialog

Usage:
1. obtain the color palette
QPalette palette = textEdit-> palette ();
2. open the color dialog box to obtain the color.
QColor color = QColorDialog: getColor (
Palette. color (QPalette: Text), // The initial color of the dialog box.
This // parent window
);
3. set the color palette.
Palette-> setColor (
QPalette: Text, // The position of the color palette to be set
Color // the color to be set
);
4. load the palette
TextEdit-> setPalette (palette );

The GUI sets color labels for different parts.


3. QFontDialog


Usage:
1. open the Font dialog box and obtain the font.
Bool OK;
QFont font = QFontDialog: getFont (& OK );
If you click "OK" in the dialog box, the value of "OK" will change to "true". if you click "cancel" in the dialog box, the value of "OK" will change to "false ".
2. set the font
TextEdit-> setFont (font );

4. QInputDialog

Usage:
Open the input dialog box, and the entered content will return
QString str = QInputDialog: getText (
This, // parent window
"InputDialog", // window title
"Please input", // The Label text above the input box
QLineEdit: Normal, // Display mode of the edit box
QDir: home (), // Default content in the edit box
OK // backfill the bool variable
)

5. QProgressDialog

QProgress: setRange (0,100) // Set the progress bar range
QProgress: setValue (50) // sets the current value of the progress bar

III. QMessageBox
Qt provides message boxes for displaying information. these message boxes are modal dialogs, which are frequently used in software.
1. QMessageBox: question
A message inquiry box with titles and texts. developers can customize the number and function of buttons as needed.

2. QMessageBox: empty
A prompt message box with a title and prompt text. developers can customize the number of buttons and their functions as needed.

3. QMessageBox: warning
A warning message box with titles and text information. developers can customize the number and function of buttons as needed.

4. QMessageBox: critical
A fatal information box with titles and text information. developers can customize the number and function of buttons as needed.

5. QMessageBox: about
A message box with a title and text

6. QMessageBox: aboutQt
Display the message box about Qt

7. Message button formulation



IV. QDialog instance
1. header files
 
 
  1. #ifndef BUILDINDIALOG_H
  2. #define BUILDINDIALOG_H

  3. #include

  4. class buildInDialog : public QDialog
  5. {
  6. Q_OBJECT
  7. public:
  8. buildInDialog();
  9. private:
  10. QPushButton *fileBtn;
  11. QPushButton *colorBtn;
  12. QPushButton *fontBtn;
  13. QPushButton *saveBtn;
  14. QPushButton *closeBtn;

  15. QTextEdit *textEdit;
  16. private slots:
  17. void fileSlot();
  18. void colorSlot();
  19. void fontSlot();
  20. void saveSlot();
  21. void closeSlot();

  22. };



  23. #endif
2. implementation file
 
 
  1. # Include "buildInDialog. h"

  2. BuildInDialog: buildInDialog ()
  3. {
  4. FileBtn = new QPushButton ("open ");
  5. ColorBtn = new QPushButton ("color ");
  6. FontBtn = new QPushButton ("font ");
  7. SaveBtn = new QPushButton ("save ");
  8. CloseBtn = new QPushButton ("close ");

  9. TextEdit = new QTextEdit ();


  10. // Layout
  11. QVBoxLayout * vLay = new QVBoxLayout ();
  12. QHBoxLayout * hLay = new QHBoxLayout ();
  13. VLay-> addWidget (fileBtn );
  14. VLay-> addWidget (colorBtn );
  15. VLay-> addWidget (fontBtn );
  16. VLay-> addWidget (saveBtn );
  17. VLay-> addWidget (closeBtn );

  18. HLay-> addWidget (textEdit );
  19. HLay-> addLayout (vLay );

  20. SetLayout (hLay );

  21. Connect (fileBtn, SIGNAL (clicked (), this, SLOT (fileSlot ()));
  22. Connect (colorBtn, SIGNAL (clicked (), this, SLOT (colorSlot ()));
  23. Connect (fontBtn, SIGNAL (clicked (), this, SLOT (fontSlot ()));
  24. Connect (saveBtn, SIGNAL (clicked (), this, SLOT (saveSlot ()));
  25. Connect (closeBtn, SIGNAL (clicked (), this, SLOT (closeSlot ()));
  26. }

  27. Void buildInDialog: fileSlot ()
  28. {
  29. // Obtain the file name
  30. QString str = QFileDialog: getOpenFileName (this, "open File", "/", "All File (*.*)");

  31. // Open the file
  32. QFile file (str );
  33. If (! File. open (QIODevice: ReadWrite ))
  34. Return;
  35. // Obtain the input stream
  36. QTextStream in (& file );
  37. // Read data
  38. While (! In. atEnd ())
  39. {
  40. QString st = in. readLine ();
  41. TextEdit-> append (st );
  42. }
  43. }

  44. Void buildInDialog: colorSlot ()
  45. {
  46. // Obtain the color palette
  47. QPalette palette = textEdit-> palette ();
  48. // Open the dialog box to obtain the color
  49. QColor color = QColorDialog: getColor (palette. color (QPalette: Text), this );

  50. If (color. isValid ())
  51. {
  52. // Place the color into a color palette
  53. Palette. setColor (QPalette: Window, color );
  54. // Load the palette
  55. TextEdit-> setPalette (palette );
  56. }

  57. }

  58. Void buildInDialog: fontSlot ()
  59. {
  60. Bool OK;
  61. QFont font = QFontDialog: getFont (& OK );
  62. If (OK)
  63. TextEdit-> setFont (font );
  64. }

  65. Void buildInDialog: saveSlot ()
  66. {
  67. Bool OK;
  68. // Obtain the input information
  69. QString str = QInputDialog: getText (this, "input dialog box", "Enter the name", QLineEdit: Normal, "wj", & OK );

  70. // Open the file according to the input name
  71. QFile file (str );
  72. File. open (QIODevice: WriteOnly );
  73. // Obtain the output stream
  74. QTextStream out (& file );
  75. // Write the content of textEdit to the out
  76. Out < ToPlainText () <"\ n ";
  77. }

  78. Void buildInDialog: closeSlot ()
  79. {
  80. QProgressDialog * progress = new QProgressDialog ();
  81. Progress-> setRange (0,100 );
  82. For (int I = 0; I <= 100; I + = 10)
  83. {
  84. QApp-> processEvents ();
  85. Progress-> setValue (I );
  86. Sleep (1 );
  87. }
  88. }
3. main function
 
 
  1. # Include "buildInDialog. h"
  2. # Include

  3. Int main (int argc, char * argv [])
  4. {
  5. // Set the encoding to prevent Chinese characters from garbled characters
  6. QTextCodec: setCodecForCStrings (QTextCodec: codecForName ("UTF-8 "));
  7. QApplication app (argc, argv );

  8. BuildInDialog dialog;
  9. Dialog. show ();

  10. Return app.exe c ();
  11. }




Definition 1. QDialog class 1. the dialog box concept dialog box is used in various software. it is generally used to prompt users or receive user feedback...

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.