How to make QtDesigner offer Enumeration for Plugin Property - qt

I'm trying to deploy my Qt Ui Widget as a Qt Designer Plugin in accordance to the Guide at https://doc.qt.io/qt-5/designer-creating-custom-widgets.html
However, I just can't seem to figure out how to make QtDesigner present the User with a Dropdown in the Property-Editor for Enumeration Properties.
Any suggestions are greatly appreciated!

Answering my own question:
1) put the enum right after the "public:" of your class and immediately thereafter use the Q_ENUM macro
class myEnumContainingClass {
Q_OBJECT
Q_PROPERTY(FOO my_property READ getMyEnumProperty WRITE setMyEnumProperty DESIGNABLE true)
public:
enum FOO {
BAR
};
Q_ENUM(FOO)
explicit MyEnumContainingClass();
...
2) use only the key of the enum, not the whole enum::string in the domXml()-section of the plugin like this
<property name="theEnumHoldingProperty">
<enum>BAR</enum>
</property>
Although I would have still preferred to have the whole enum::string appear in the Dropdown within Qt Designer for consistency, at least it works this way.

Related

Qt5 dynamic translation issues for C++ defined properties

I am currently working on somebody else code and I need to fix a bug linked with dynamic translation.
When the language is changed, the Loader is reloaded, it works but it generates unwanted effects (including the bug mentioned above).
So I tried to look for a way to dynamically change the translation without reloading everything.
I added m_engine->retranslate() in my switchLanguage function and this works perfectly, but only for texts directly defined in QML files. The thing is there is also a lot of text defined with setContextProperty in the C++ main controller class, and for them, it doesn't work at all (which seems pretty normal since m_engine is a QQmlApplicationEngine).
I don't see how I can simply force these texts to retranslate too. I have them in pretty much every controller function and they are used by different QML files. I am afraid that there will be no other choice but to change completely the way translation is managed. I hope advanced programmers can help me with this.
Other information:
I work with 5.13.0 version of Qt.
I don't use Designer and cannot use ui.retranslateUi().
It's hard to tell how your main controller class looks like, so here is a short general answer.
You can install an eventFilter and listen for LanguageChange.
In constructor of "main controller class", add this:
auto *core = QCoreApplication::instance();
if(core != nullptr)
{
core->installEventFilter(this);
}
Then add a function to your class:
bool MainControllerClass::eventFilter(QObject *watched, QEvent *event)
{
Q_UNUSED(watched);
if(event->type() == QEvent::LanguageChange)
{
//set properties again or emit property changed signals
}
}

Best way to propagate variables from QWizard to the pages

Iam looking for a good and easy way to propagate a variable, in my case a string from QWizard on to the pages. I have tried to use fields but without registering it this won't work.
So on the third page of my wizard I need a path to a folder where some data can be found, this is used to init a QFileDialog. This QString is not directly coupled to a QWidget. So I can not call registerField on it.
pseudo code:
// setup wizard:
QMyWizard wiz;
QString s = "c:\\";
wiz.SetFolder(s);
I can of course in the Wizard SetFolder function access the page by looking it up and casting but I find this quite ugly. So Is there a good solution to pass on global variable without registering to widgets?
Thanks!
I'll answer my own question.. I did not find handy functionality in the wizard code itself. Now I am using the ability of any derived class from QObject to hold properties in a map from string to QVariant. In the wizard pages you can acces the properties of the parent now.
http://qt-project.org/doc/qt-4.8/qobject.html#setProperty

How to access the set qt stylesheet properties (css like grammar), or is there an css to xml converter?

Hy,
i m working on a qt application styled by a stylesheet, set like
QApplication qApplication(argc, argv);
QFile styleFile("myStyleFile.stylesheet");
bool check = styleFile.open(QFile::ReadOnly);
qApplication.setStyleSheet(styleFile.readAll());
the important window of this application uses a QGraphicsScene (which items are not styleable with stylesheets).
But i wannt a unique look.
So the questions are:
1.) Is there a way to access the set stylesheet properties ( like getProperty("QMenu::item:selected") )?
2.) or does anyone know a css-syntax to xml-file tool? (than one could access the set properties with the qt xml/dom support)
I know that some special properties can be accessed like
QColor mainWindowbackgroundColor =
palette().color( QWidget::backgroundRole() );//Get the backgroundcolor set by stylesheet.
but i am searching for a way to access ALL set properties.
Thank you!
I think you will need to use private Qt classes to do this. This is generally not a good idea as the interfaces are internal and subject to change.
In the Qt (4.8.4) sources \src\gui\text\qcssparser_p.h header the QCss namespace is declared.
Whilst I haven't tried this, it looks like you will need to create a QCss::Parser, call parse to get a QCss::StyleSheet. This object contains the parsed data including a vector of QCss::StyleRule which matches QCss::Selector and QCss::Declaration together, have a look at the comment above the QCss::Declaration to see how it is all broken down.
Final Warning: Using Qt private interfaces is liable to cause maintenance problems - don't do it without a very good reason.

global event catch for QEventfilter class

I want to add a help webpage for my Qt Application. The user can use the Qt::WhatsThis "tooltip" to have a explanation about the topic and a URL to a webpage. I edit the WhatsthisTooltips with the Qt Designer and have it language indepentend.
Now I create a "LinkFilter" class for filtering, install it in the viewclass and react on the QEvent::WhatsThisClicked event. This works fine.
Now i would catch every QEvent::WhatsThisClicked in my application, but i looks really messy to install it on really every dialog/view/action/widget. This there some kind of global eventHandler, were I can install my "LinkEvent"class? There must be a nice Qt style way for solving this.
For installing the EventFilter I strongly followed this video-tutorial http://youtu.be/DHgbYxpZkbg
http://qt-project.org/doc/qt-4.8/qapplication.html#notify
Essentially you need to subclass QApplication and override this function. Then you can check if it's the type of event you're looking for and act accordingly.

How to know that widget is currently is running in Qt Designer

How can I in code of the custom Qt widget know that it is currently instantiated in Qt designer?
Use case:
I build a complex custom widget that has several child widgets like QPushButton, QLabel etc.
As application logic require, when widget is created most of those sub component are not visible but in design time when I put it on a form I would like to see them.
To be able to play with style sheet at design time.
Currently what I get is a empty is only a result of constructor - minimal view (actually empty in my case).
What I am looking for is to be able to do something like
MyQWidget::(QWidget *parent)
{
....
if(isRunningInDesigner())
{
myChildWidget1->setVisible(true);
myChildWidget2->setVisible(true);
myChildWidget3->setVisible(true);
}
else
{
myChildWidget1->setVisible(false);
myChildWidget2->setVisible(false);
myChildWidget3->setVisible(false);
}
....
}
So what should I put in to this bool isRunningInDesigner() ?
From the Qt Designer manual:
To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin’s createWidget() function.
Those are methods from the QDesignerCustomWidgetInterface plugin interface. In short: you tell the widget to behave differently when Qt Designer asks your plugin to create instances of your custom widget.

Resources