简体   繁体   中英

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.

The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.

在此处输入图片说明

The Problem

Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.

I have tried things as simple as:

setProperty("button0Icon", getButton0Icon());

That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.

I have also tried:

#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
    QDesignerFormEditorInterface *editor = form->core();
    QExtensionManager *manager = editor->extensionManager();
    QDesignerPropertySheetExtension *sheet;
    sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
    int propertyIndex = sheet->indexOf("button0Icon");
    sheet->setChanged(propertyIndex, true);
    sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif

And:

int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());

Nothing seems to update the property list in the designer.

I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.

To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.

If anyone has any experience with this or any ideas please let me know. Thanks.

I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.

Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.

I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.

I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM