[英]Qt dynamic stylesheet with User Interface Compiler
我正在使用Qt應用程序,該應用程序通過Qt用戶界面編譯器使用XML文件生成用戶界面。
我無法訪問保存每個小部件的代碼(我可以,但是Qt UI編譯器每次都會重新生成它),所以我無法向其生成的類中添加其他方法。
我正在嘗試在一個QLineEdit窗口小部件上執行setStyleSheet,但它給了我一個QPixmap: It is not safe to use pixmaps outside the GUI thread
警告QPixmap: It is not safe to use pixmaps outside the GUI thread
,然后最終出現段錯誤。 在辭職之后,我決定測試每個小部件有兩個副本,每個副本都有所需的樣式表值。 然后,我會根據需要在小部件上觸發QLineEdit::hide()
和QLineEdit::show()
,我認為這樣可以工作。
沒有。 該程序現在吐出QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
每次運行時都QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
。
我該怎么做才能解決此問題? 我需要動態更改小部件的樣式表,但似乎無法以任何方式進行更改。
您不能從主線程以外的線程中調用任何QWidget
方法。 不過,從任何線程安全地間接調用此類方法相當容易。 有關詳細信息,請參見此答案 。
例如,假設您想從在其他線程中運行的代碼中調用小部件上的setStyleSheet
:
template <typename F>
static void postToMainThread(F && fun, QObject * object) {
QObject signalSource;
QObject::connect(&signalSource, &QObject::destroyed, object, std::forward(fun));
}
void threadCode(QWidget * widget) {
postToMainThread([widget]{
widget->setStyleSheet("color: black");
}, widget);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.