久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

強制 QObject 作為 QWidget 的父級有什么后果?

What are consequences of forcing QObject as a parent of QWidget?(強制 QObject 作為 QWidget 的父級有什么后果?)
本文介紹了強制 QObject 作為 QWidget 的父級有什么后果?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

以下代碼完美編譯:

QObject* o = new QObject(0);
QWidget* w = new QWidget(0);
qobject_cast<QObject*>(w)->setParent(o);

我不能合法地將 QObject 設置為 QWidget 的父級.但是使用 qobject_cast 是可能的.是否有負面影響?

I cannot legally set QObject as a parent of QWidget. But using qobject_cast it is possible. Are there negative consequences?

推薦答案

強制 QObject 作為 QWidget 的父級有什么后果?

What are consequences of forcing QObject as a parent of QWidget?

不可撤銷的未定義行為.

Irrevocable undefined behavior.

Qt 并非旨在支持 QWidget 的非小部件父級.我認為這是 Qt 中的一個 API 錯誤,因為由于這種限制,QWidget 在 Liskov 替換原則意義上并不完全是 QObject.

Qt is not designed to support a non-widget parent to a QWidget. I consider it an API bug in Qt, since a QWidget isn't fully a QObject in the Liskov Substitution Principle sense because of that limitation.

Qt 4.x 在嘗試激活小部件時會崩潰.所以它會一直工作,直到你專注于你的應用程序,然后會崩潰.

Qt 4.x will crash when attempting to activate the widget. So it'll work until you focus your application and then will crash.

Qt 5.x 在 QObject::setParent() 中斷言.

Qt 5.x asserts in QObject::setParent().

雖然可以繞過斷言:

// https://github.com/KubaO/stackoverflown/tree/master/questions/widget-parent-28992276
#include <QApplication>
#include <QLabel>

class ParentHacker : private QWidget {
public:
   static void setParent(QWidget * child_, QObject * parent) {
      // The following line invokes undefined behavior
      auto child = static_cast<ParentHacker*>(child_);
      Q_ASSERT(child->d_ptr->isWidget);
      child->d_ptr->isWidget = 0;
      child->QObject::setParent(parent);
      child->d_ptr->isWidget = 1;
   }
};

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QLabel w{"Hello!"};
   w.setMinimumSize(200, 100);
   w.show();
   ParentHacker::setParent(&w, &app);
   return app.exec();
}

然后它會在其他地方崩潰.

It will crash somewhere else then.

您會在嘗試修補 Qt 以使其正常工作時進行一場艱苦的戰斗.我認為這不是一場值得的斗爭 - 除非決定使 QWidget 真正成為 QObject 并更改其構造函數簽名.這最早可以在 Qt 6 中完成,因為它是一個二進制不兼容的更改 AFAIK.

You'd be fighting an uphill battle trying to patch Qt to get it to work. It's not a worthwhile fight, I think - not unless a decision is made to make a QWidget truly-a QObject and change its constructor signature. That can be done at the earliest in Qt 6 since it's a binary-incompatible change AFAIK.

此外,您嘗試做的事情大多是不必要的.您當然可以為多個獨立的頂級小部件設置一個隱藏的 QWidget 父級.

Moreover, what you're trying to do is mostly unnecessary. You can certainly have a hidden QWidget parent to multiple stand-alone top-level widgets.

#include <QApplication>
#include <QLabel>

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget parent;
   QLabel l1{"Close me to quit!"}, l2{"Hello!"};
   for (auto label : {&l1, &l2}) {
      label->setMinimumSize(200, 100);
      label->setParent(&parent);
      label->setWindowFlags(Qt::Window);
      label->setText(QString("%1 Parent: %2.").
                     arg(label->text()).arg((quintptr)label->parent(), 0, 16));
      label->show();
   }
   l2.setAttribute(Qt::WA_QuitOnClose, false);
   return app.exec();
}

隱藏小部件的開銷很小,您不會通過使用 QWidget 而不是 QObject 為父級浪費任何資源.

The overhead of having the widget hidden is minimal, you're not wasting any resources by using a QWidget instead of a QObject for the parent.

這篇關于強制 QObject 作為 QWidget 的父級有什么后果?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 欧美日韩一区二区在线观看 | 日本在线视频一区二区 | 草b视频 | 精品免费视频 | 久久久久久久久一区 | 一区二区三区日韩 | ww 255hh 在线观看| 91se在线 | 日本精品一区二区三区视频 | 欧美日韩福利视频 | 狠狠干狠狠操 | 国产精品免费一区二区三区四区 | 在线播放中文字幕 | 久久99精品视频 | 精品一区二区三区在线视频 | 亚洲精品黄色 | 亚洲av毛片成人精品 | 天天干成人网 | 91精品国产色综合久久 | 欧美精品区 | 久久伊人免费视频 | 国产成人一区二区 | 久草热播 | 欧美不卡一区二区三区 | 日韩高清中文字幕 | 午夜久草 | www.性色 | 国产高清毛片 | 中文字幕在线精品 | 日一区二区 | 色婷婷一区二区三区四区 | 国产一区二区在线看 | 日韩综合 | 国产免费观看一级国产 | 青青久在线视频 | 久久久久精 | 欧美日韩在线一区二区三区 | 一区二区三区av | 国产福利视频网站 | 成人a在线 | 成人免费视频网站在线观看 |