modify parent QWidget inside child QWidget - c++

I have a QWidget B which is contained inside a QWidget A. QWidget A has a QLabel. I have some data inside QWidget B, which I want to set as the value of the QLabel. How can something like this be done?

First of all, if I understand your topic correctly, you have your own derivations of QWidget for A and B.
The clean solution to your problem is that B emits a signal, like newLabelText(QString text);.
Then, A provides a slot setLabelText(QString text);. You connect the signal to the slot and you are done.
In this solution, only one entity needs to know both A and B to perform the Signal/Slot-Connection, while A and B do not have to know each other, or mangle with each other.

Related

Accessing QWidget child slots/signals from a QWidgetList (QList<QWidget*>)

I am making a gui app designer. Every time the user create a new widget, it is stored in a QWidgetList. It can be a QPushButton, a QLineEdit, whatever.
For example, let's say I have a QPushButton (index 0) and a QLineEdit (index 1).
Is it possible to access the signal clicked of WidgetList[0], or use the slot setText of WidgetList[1] ?
Or do I really have to make a QList for each type, like QList<QPushButton> and QList<QLineEdit> ?
Thanks in advance
To use the new connect syntax, you have to cast the widgets to correct types. E.g.:
QPushButton b{“clear”};
QLineEdit e;
QWidgetList widgets{&b, &e};
QObject::connect(qobject_cast<QPushButton*>(widgets[0]), &QPushButton::clicked,
qobject_cast<QLineEdit*>(widgets[1]), &QLineEdit::clear);
In a designer use case, you’ll probably be referring to signals and slots using their text signatures or QMetaMethod handles, and then there’s no need for any casting at all, since those connect methods upcast the objects to QObject anyway.

Changing the behavior of a widget type in another class

I have a MainWindow class in Qt, in which several checkable QGroupBox widgets are placed.
What I want to do is, to trigger a general onClick(bool checked) slot whenever one (any) of the QGroupBox objects are clicked, identify the sender and trigger some code.
I need to capture the objects' "clicked" signal in order to prevent a disabling action it performs on its children when the control is clicked.
This is the signal I'm trying to handle:
class Q_WIDGETS_EXPORT QGroupBox : public QWidget
{
...
Q_SIGNALS:
void clicked(bool checked = false);
...
};
I tried adding a custom slot like this and tried connecting it with the signal above but since QGroupBox at its own is not an object or pointer, the operation fails.
void MainWindow::onClick(bool clicked)
{
qDebug()<<"Custom slot triggered";
}
Long story short, I need to handle a control type's default behavior within my MainWindow class.
Thanks for any ideas in advance.
I need to capture the objects' "clicked" signal in order to prevent a disabling action it performs on its children when the control is clicked.
Perhaps, but I'm smelling an XY Problem here. You can certainly prevent emission of signals by invoking blockSignals() on the widget. But that's a hack - it will also prevent Qt's internals from acting on the object's destroyed() signal, and you might subtly break other users of the object's signals.
Instead, one could make the UI stateful and have the controller implement the stateful aspect of the button's interaction with the rest of the application. Currently, the button's clicked() signal is connected directly to other users. Instead, connect the button to the controller, and have the controller disable the children only when it's appropriate to do so. One could use the QStateMachine to make this stateful behavior explicit in terms of states.
#Kuba Ober, maybe you're right, I could not state my problem and the facts around it seperately and clearly. Thank you for pointing that out.
After checking out the QGroupBox class' source, I discovered that a "toggle" signal is emitted after every other operation in a "clicked" event.
http://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qgroupbox.cpp
void QGroupBox::setChecked(bool b)
{
Q_D(QGroupBox);
if (d->checkable && b != d->checked) {
update();
d->checked = b;
d->_q_setChildrenEnabled(b);
#ifndef QT_NO_ACCESSIBILITY
QAccessible::State st;
st.checked = true;
QAccessibleStateChangeEvent e(this, st);
QAccessible::updateAccessibility(&e);
#endif
emit toggled(b);
}
}
By combining this information with that of #rbaleksandar 's suggestion, I managed to obtain a solution.
I iterated through the controls of my MainWindow class and connected every QGroupBox's "toggle" signal with a common "MainWindow::onToggle" slot in which I could handle the post-event operations I desire.
As for my previous approach, I could've created a class which inherits QGroupBox and override any methods I wish to. But that would be overkill compared to my current solution. The fix I've came up with did the trick perfectly.
Thanks everyone for their insight and help.

connecting QGraphicsview signal to UI slot

I am new to QT and C++ and i have legacy qt-c++ code here which i cant get to work.
Probably its something about the lifetime of the calling objects but hey, but please tell me what i am missing.
In a QT .ui i have various Frames and Widgets containing Frames and Widgets containing a QVBoxLayout which we shall call "myLayout"
On click in the .ui file i use
myWidget = new mywidget(some params);
myLayout->addWidget(myWidget);
where myWidget is declared as mywidget *myWidget; in the header file
myWidget is a QWidget which internally adds a QVBoxLayout to itself and adds a QGraphicsView. Using the MouseReleaseEvent i emit a signal from the QWidget.
now when i try to connect the signal slot (which i do in cpp file from the ui)
connect(myWidget, SIGNAL(mySignal(QString)), this, SLOT(mySlot(QString)));
the signals never catch the slot. the slot is public, the signal isnt.
What did i do wrong? Can somebody help. Feel free to ask further questions since i dont really know whats important in c++ questions;)
edit:
the signal gets emitted by QGraphicsObjects which themselfs connect to a slot in the QGraphicsView. This Slot is called and debuggable. at the end of the Routine an emit mySignal("..."); is called.
Maybe you forgot to add Q_OBJECT macro in your widget declaration. Qt documentation: http://doc.qt.io/qt-5/qobject.html#Q_OBJECT

How to call QMessageBox Static API outside of a QWidget Sub-Class

I have a utility class in my Qt GUI application. However, in my convenience class I wanted to call a QMessageBox::critical(), warning(), etc. The class isn't a QWidget, and therefore I cannot pass this as the parent. My class is subclassed from QObject, however, so it can run things such as signals and slots. So to work around this--if it's possible to--should I maybe look at the property API instead of using the Static API?
Class declaration:
class NetworkManager : public QObject
And here's an example of a Static API call that fails:
QMessageBox::critical(this, tr("Network"), tr("Unable to connect to host.\n"),
QMessageBox::Ok | QMessageBox::Discard);
So, if I were to build a Property based API message box, would it be possible to call it in a QObject somehow? I haven't really used the Property Based API, but I understand from the documentation that it seems to use an event loop (i.e. exec()).
Just pass NULL for the first parameter:
QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("..."));
A better way than passing nullptr is to use the qobject tree you are already using (assuming that the parent of the NetworkManager instance is a QWidget; adjust the number of parents according to whatever your qobject tree looks like)
QMessageBox::critical(qobject_cast<QWidget *> (parent()), "Title", "Message");
We use a qobject_cast<> instead of a C or C++ style cast is because it adds a little protection and will return 0 if it can't cast upward to the QWidget *.
If you use nullptr the QMessageBox will appear as centered over the topmost window (QWidget) rather than the window that actually appeared higher up in the qobject tree of your NetworkManager class. This really annoys people who have multiple monitors, lots of windows open, multiple windows from a single application spanning multiple monitors, etc.

How to recognise which QObject emitted a SIGNAL in Qt?

I have many QLineEdit's in my Main Window. I have a connected the editingFinished() SIGNAL of each object to a single SLOT in my QMainWindow class.
I want to implement my own Undo/Redo functionality. I find the Qt Undo framework pretty difficult to understand & complex to implement.
So I want to maintain a QList <QLineEdit *> undoList; which will store the sequence of QLineEdit's which were modified. I am maintaining an int variable to keep track of current state. When Undo/Redo is done I can simply update the int variable & find the QLineEdit which was edited at that state & then call undo() on that QLineEdit.
Currently my biggest problem is that since I have connected all my QLineEdit's to a single SLOT, I have no information as to which QLineEdit emitted that SIGNAL.
So I would like to know if there is any way by which I can understand which QLineEdit emitted the SIGNAL.
Thank You.
Use sender() inside the slot to get the the QObject that emitted the signal connected to it.