Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Đối với các truy vấn đơn giản, Qt cung cấp hộp thoại mẫu đầu vào khác nhau, bao gồm một widget đầu vào phù hợp và hai nút OK và Cancel. QInputDialog lớp có liên quan đã chỉ sẵn sàng thực hiện phương pháp tĩnh mà bây giờ chúng ta sẽ xem xét chi tiết hơn. Thường xuyên, bạn được đặt ở vị trí có yêu cầu người dùng nhập vào một giá trị | 6.5 Ready-made Dialogs in Qt 6.5.4 Input Dialogs For simple queries Qt provides various template input dialogs consisting of a suitable input widget and two buttons OKand Cancel. The relevant QInputDialog class has only ready-made static methods that we will now look at in more detail. Frequently you are put in the position of having to ask the user to enter a value. Qt distinguishes here between whole number values and floating-point values which it provides in double precision. Accepting Integer Input Values The following example Figure 6.13 shows how the getInteger method of QIn-putDialog is used bool ok int alter QInputDialog getInteger this tr Enter Age tr Please enter year of birth 1982 1850 QDate currentDate .year 1 ok if ok . Figure 6.13 QInputDialog getIn-teger with preset default value The first two arguments of this are-as in other dialogs-a pointer to the parent widget and the heading of the dialog. Then getInteger expects an explanatory text which it displays above the input widget. This is followed by a default value and then the limits of the allowed input range. This example restricts the upper limit to the current year to avoid input that makes no sense i.e. specifying a year of birth in the future . To do this we use QDate a class for processing date details. The currentDate static method provides the system time according to the current date and in turn year extracts the year from this and returns it as an integer value. Also instead of inserting a static lower limit 1850 as is done here this can be formed dynamically e.g. with an expression such as QDate currentDate .year - 200 . 179 I 6 Dialogs In the next-to-last parameter getInteger asks for the amount by which the integer value should be increased or decreased if the user clicks on one of the two buttons to the right of the input field to increment or decrement the value a so-called spin box . Since the return value provides no information on whether the user has canceled the dialog or has .