Frage

I made a custom QtDesigner plugin. It loads well in QtDesigner, I can use it in a .ui file and it works fine in the executable.

My only concern is that, when using the plugin in another widget, I can't change the values of the plugin's child widgets.

Sorry if it's confusing, here's a schematic of the situation:

MyDesignerPlugin -> QWidget -> QComboBox
                            -> QTextEdit

In QtDesigner:

MainWindow -> MyDesignerPlugin

And there I have no access to the underlying QComboBox and QTextEdit. Well, I found two ways. The first works well but is quite cumbersome. The second nearly works.

First Solution - Working

First, I can create setters and getters for the child widgets in the QWidget and, thanks to Q_PROPERTY magic, the properties appear in QtDesigner's property editor.

This is fine, but not what I want. I want to be able to click on the QTextEdit and edit the text directly from there. This is not possible by default.

Second Solution - Not Working

I looked in the example https://qt-project.org/doc/qt-5.0/qtdesigner/containerextension.html#multipagewidget-class-definition and stumbled upon this line in multipagewidget.cpp:

comboBox->setObjectName("__qt__passive_comboBox");

If I add this line to my plugin widget (respectively "__qt__passive_textEdit" for the textEdit), QtDesigner then allows me to click on the combobox and change the selected value. But the action (changing the selected item in the comboBox) is not saved when I close QtDesigner and reopen it.

Question

So my question is two-fold: how can I set the properties of child widgets when including a QtDesigner plugin and what magic does this "__qt__passive_..." string ?

EDIT: The multipagewidget example does not manage either to save the fact that the currentIndex is changed when changing from the comboBox.

So for now I'll go with the Q_PROPERTY route, like suggested by @ratchet freak's comment

If somebody's wondering how one can see if the changes will be saved, simply in QtDesigner the property's name becomes bold in the properties editor.

War es hilfreich?

Lösung

Yes, You can: complete solution is combination of Yours 1st and 2nd and some 3rd step:

  1. Create setters and getters wrappers for the child widgets in the QWidget using Q_PROPERTY
  2. Add this line to plugin widget (respectively "__qt__passive_textEdit" for the textEdit), QtDesigner then allows to click on the combobox and change the selected value. This behavior described here.
  3. Change property in property editor and mark it as changed by following code which You can add to combobox index change slot or handler:

changeQtDesignerProperty("currentIndex", index);

where

    void MyPlugin::changeQtDesignerProperty(QString propertyName, QVariant value)
    {
    #if defined(QT_PLUGIN)
      QDesignerFormWindowInterface *form =
          QDesignerFormWindowInterface::findFormWindow(this);

      if(form)
      {
        if(!mSheet) // Need to create sheet only once
        {
          QDesignerFormEditorInterface *editor = form->core();
          QExtensionManager *manager = editor->extensionManager();
          mSheet = qt_extension(manager, this);
        }
        // Set property in Qt Designer
        int propertyIndex = mSheet->indexOf(propertyName);
        mSheet->setProperty(propertyIndex, value);
        mSheet->setChanged(propertyIndex, true);
      }
    #endif
    }

With other properties need to do the same. And need to delete mSheet; in MyPlugin destructor. mSheet initialized as QDesignerPropertySheetExtension *mSheet = 0;.

Unlike 2nd step, 3rd step is well documented in Qt Doc. Even with examples.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top