gpt4 book ai didi

c++ - QUiLoader 只创建自定义小部件的基类

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:46 25 4
gpt4 key购买 nike

我正在尝试使用 QUiLoader 加载包含自定义小部件的 ui 文件。

我的自定义小部件(称为 CustomButton)继承自 QPushButton。 ui 文件已加载并放入我的主布局中,但所有 CustomButtons 都只是 QPushButtons。看起来,QUiLoader 将我所有的自定义小部件创建为其基类的实例。

这是我的做法:

QUILoader loader;
loader.addPluginPath(MY_PLUGIN_PATH);

QStringList availableWidgets = loader.availableWidgets();

//fail if "CustomButton" is not available
if (!availableWidgets.contains("CustomButton")) {
return false;
}
//here I see that availableWidgets contain my "CustomButton"!

QString qFileName(MY_UI_FILE_PATH);
QFile file(qFileName);
file.open(QFile::ReadOnly);

//"mainFrame" is a QFrame in my main ui
QWidget *customWidget = loader.load(&file, mainframe);
file.close();

//layout
mainframe->layout()->addWidget(customWidget);

//Note: There are no QPushButtons in my ui file! There are only CustomButtons!

//Now I try to find my custom buttons
QList<QPushButton*> list1 = customWidget->findChildren<QPushButton *>(); //all my CustomButtons are listed here
QList<CustomButton*> list2 = customWidget->findChildren<CustomButton *>(); //this list is empty

//我在 CustomButton 的构造函数中也有一个从未被命中的断点。

我做错了什么?

最佳答案

我发现,QUiLoader 实际上创建了我的自定义按钮!

只有 findChildren() 方法似乎是错误的,因为它没有找到我的 CustomButtons,而是 QPushButtons。

关于c++ - QUiLoader 只创建自定义小部件的基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com