久久久久久久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 ()?環形?)
主站蜘蛛池模板: 亚洲一区中文字幕 | 国产精品久久久久一区二区三区 | 久久久久国产精品一区 | 日本又色又爽又黄的大片 | 欧美日韩精品免费观看 | 亚洲国产精久久久久久久 | 亚洲高清av | 久久91精品久久久久久9鸭 | 亚洲精品白浆高清久久久久久 | 亚洲热在线视频 | 国产高清久久久 | 2018天天干天天操 | 欧美性生活免费 | 操操日 | 国产精品久久久久久久久图文区 | 一区二区三区四区在线 | 精品欧美一区二区三区免费观看 | 欧美一级免费看 | 成人一区二区在线 | 欧美福利精品 | 91伊人| 国产精品久久在线 | 黄网站在线播放 | 成人伊人| 日本激情视频在线播放 | 亚洲精品99 | a a毛片| 亚洲精品一区二区三区 | 欧美一区在线看 | 国产精品99久久久久久久vr | 怡红院免费的全部视频 | 久久伊人一区 | 日韩在线欧美 | 日本午夜免费福利视频 | 这里有精品| 亚洲午夜视频在线观看 | 91视频国产区| 男女视频在线观看网站 | 日本黄色的视频 | 在线观看av网站 | 午夜影院在线观看版 |