سؤال
أنا أقوم ببناء مشروع QT Symbian وأريد إظهار إشعار للمستخدم الذي يجب أن يغلق تلقائيا بعد بضع ثوان. لقد رأيت أن نوكيا تستخدم هذا كثيرا في واجهة المستخدم الخاصة بهم.
الآن أنا أستخدم الرمز أدناه بحيث يمكن للمستخدم إغلاق QMessageBox ولكني أود ذلك إذا كان من الممكن إغلاق السيارات QmessageBox بعد 1 أو 2 ثانية. كيف يمكنني القيام بذلك باستخدام QT؟
QMessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
المحلول
أود أن أقترح ل Subclass QMessageBox
لإضافة السلوك المرغوب الخاص بك ...
سيكون من المثير للاهتمام إضافة طرق مثل setAutoClose(bool)
و setAutoCloseTimeout(int)
وتشغيل أ QTimer
على showEvent
عند تمكين خيار Autoclose!
بهذه الطريقة، يمكنك حتى تغيير ونبها QMessageBox
وكان لديه نص يقول "سيقوم هذا المربع بإغلاق تلقائيا في ثواني XXX ..." أو شريط التقدم، إلخ ...
نصائح أخرى
شكرا جزيلا! بلدي الحل:
قمت بإنشاء صفي الخاص (MessageBox) هذا هو التعليمات البرمجية الخاصة بي لإظهارها:
MessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setAutoClose(true);
msgBox.setTimeout(3); //Closes after three seconds
msgBox.exec();
هذا هو صفي:
class MessageBox : public QMessageBox
int timeout;
bool autoClose;
int currentTime;
void MessageBox::showEvent ( QShowEvent * event ) {
currentTime = 0;
if (autoClose) {
this->startTimer(1000);
}
}
void MessageBox::timerEvent(QTimerEvent *event)
{
currentTime++;
if (currentTime>=timeout) {
this->done(0);
}
}
بدلا من ذلك يمكنك استخدام Singleshot
Timer لإغلاق أي مربع حوار أو QLabel
بكل سهولة:
QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));
مع هذا الرمز:
QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));
لقد حصلت:
QObject::connect: Incompatible sender/receiver arguments
QTimer::timeout() --> QMessageBox::
نظرا لأن MsgBox (جهاز الاستقبال) يجب أن يكون كائن Qtcore .. و QmessageBox الفئة الفرعية QTGUI. يرى https://srinikom.github.io/pyside-docs/pyside/qtcore/qtimer.htmlbyside.qtcore.pyside.qtcore.qtimer.singleshot.
هذا قد يساعد شخص ما،
msgBox.button(QMessageBox::Ok)->animateClick(5000);
يغلق صندوق MessageBox بعد 5 ثوان.