Accessing the child UI elements in a Qt UI - qt

I have defined a UI (let's call it myUI) using the Qt designer, and using it in my applications. I need to access all the sub-widgets (QToolButtons) in myUI. I want to get all the subwidgets as a QObjectList.
Is there any way to do this?
The QObject::children() doesn't work here because the Qt UI Compiler, when converting the .ui file to a C++ class, doesn't define the ui_myUI class as a subclass of any QObject derived class. Is there any way to force it to do this, and then use the children() function?
Thanks.

Call children() on the top level widget instance.
Assuming your top level widget is called 'tlWidget':
myUI->tlWidget->children()

Usually what happens is that you either inherit from the UI class or you have it as a member and invoke it's setupUi method, sending this as the parameter. The default in Qt Creator/Designer is to have it as a member, named ui.
You can use this member to access any widgets defined in your form.

You might find this interesting:
Designer: Using a .ui file in your application

How do you use your UI ?
(a) something like:
class MyWidget: public QWidget, protected myUI
{
//...
};
(b) or rather something like:
class MyWidget: public QWidget
{
protected:
myUI ui;
};
The Solution is similar for both cases, assumed that you call setupUi(this) or ui.setupUi(this) in the constructor of MyWidget
setupUi(QWidget* p) registers every widget of the UI as children of QWidget p,
so you can easily access them by calling the children() function of p:
this->children(); //"this" refers to an object of MyWidget

Related

How to make QtDesigner offer Enumeration for Plugin Property

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.

QFrame : create mousePressEvent

actually i'm trying to get a mousePressEvent for a QFrame in QT 5 (VS2010 with Qt-Addin). Saw so many suggestons here and on other forums, but newer got it to work, or i didnt understood the princip. Also looked at the scribble example, with no luck
How can i get the mousePressEvent for a QFrame? Do i have to create a class that is inheriting from a QFrame and then initialize this frame on my own in the main()-function? isnt there a solution that i can bind it to the Qt-Designer?
tried multiple things, mostly saw something like this:
protected:
void TestQtFormsApplication::mousePressEvent(QMouseEvent *qevent)
{
if (qevent->button() == Qt::LeftButton)
{
this->close();
}
};
with this i ever got the error C2027: use of undefined type 'QMouseEvent'
Subclass QFrame and reimplement mousePressEvent(..) much like you have in your example. Remember to accept() the event to stop it propagating to the parent widget. Your error is because QMouseEvent is only forward declared in the QWidget header file, just include it yourself.
If you want to use your subclass in Qt Designer, just use a QFrame and the 'Promote' it to your subclass (docs).

Proper way to encapsulate QWidgets?

I'm making a Qt widget (let us call it A) that really is a subclassed QGraphicsView. However, I don't want to expose QGraphicsView methods in my widget, just the basic QWidget interface and some of my own methods. Thus, I'd like to encapsulate an instance of A as a member of a simple QWidget subclass we'll call B.
Now I wonder, what is the proper way to draw this? So far I've given B a layout whose only item is the A member. But this feels sort of wrong; for example, I'll need to override default layout settings to avoid adding extra margins etc. This leads me to feel that there is some correct way to do this that I'm missing. Any thoughts?
Edit: In the setting of RedX' answer, my question becomes: What is the proper way to use gv in this setting? Give A a layout and add gv to it, or override A's painting methods to instead use those of gv? Or something else?
I don't think there is a better way. If you don't want to use a layout, you can override the parent's resizeEvent() like this:
void A::resizeEvent( QResizeEvent* ) {
m_graphicsView->setGeometry( QRect( 0, 0, size() ) );
}
I think you are trying to do this?
class A : public QWidget{
QGraphicsView* gv; //use this to do whatever you need
};
This should expose a as a QWidget and internally you'd use the gv to do whatever you need from the QGraphicsWidget.

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.

install EventFilter on QWidget (qt4.4.3/kde4)

I have a K* window, and within it, a widget which needs the events filtered.
For example I do not want the possibility of clicking it...
How can I do that?
Have I to use eventfilters? In this case, what's the best way?
but my problem is that I can't subclass my widget,because it's a TerminalInterface->widget(), not an object like others :\
Besides the setEnabled sledgehammer approach in the first answer, there are two other approaches, one of which is to use eventfilters.
The other is to subclass the widget, and then reimplement, say, the mouse* events. Simply leaving them empty will prevent any mouse interaction. So:
MyWidget : public QSomeWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent);
protected:
void mousePressEvent(QMouseEvent *) {}
.. etc ..
};
QWidget has an enabled property. Just call widget->setEnabled(false) and this will prevent it from accepting mouse clicks. It may also modify its appearance: for example a QPushButton will be grayed out.
Event Filters sound like overkill for what you want.
It looks like eventFilter() is what you want.
Here's the section of Qt docs that talk about it:
Event Filters
Basically you have to create a class that inherits QObject and then implement the virtual function eventFilter(). Then call the installEventFilter() method on the object that you want to filter with the filter as a parameter.

Resources