How to access to parent widget on qt? - c++

I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow.
How can I access to the parent's slots?
I've tried getParent()->mySlot() from the QTreeWidget class but I've got
error: no matching function for call to 'PackList::mySlot()'
Does anybody know the correct way? Thanks

If you know the parent's class, you will have to cast parentWidget() to that class and then call your slot. Keep in mind whether or not it's a slot makes no difference in this case. You are just calling a method.
((KXmlGuiWindow*)parentWidget())->mySlot();
You can make the call without casting by wiring up your signal to the slot.
connect( this, SIGNAL(mySignal()), parentWidget(), SLOT(mySlot()) );
Lastly, you can use QMetaObject::invokeMethod to call it if you don't want to cast it. That's probably overkill.

I'm not sure I fully understand your question.
However, you can access the parent widget of a widget with parentWidget().
Then, you should be able to call any public slot :
parentWidget()->a_slot();

Related

Emitting a singal from QChartViews mouseMoveEvent

I am trying to pass the variable "event" from QChartView::mouseMoveEvent(QMouseEvent* event) to my MainWindow, so I can use it in my MainWindow. Since I am new to Qt and dont know exactly how to do it, I tried to subclass QChartView and override the function mouseMoveEvent, but I can only call static functions from this method, which are in my MainWindow class, but I need to call a non static function, so I can access variables and functions in my MainWindow class.
Is it possible to solve this problem with signals? My first attempt was this:
connect(this, ChartView::mouseMoveEvent(QMouseEvent* event), ui->chartView, SLOT(showInformation(QMouseEvent*)));
But it didnt work. Is there a way to emit a signal which executes a function in my MainWindow every time when ChartView::mouseMoveEvent(QMouseEvent* event) is called?

QListView setModel problems calling via QMetaObject::invoke

Hi I have a QListView widget displayed correctly on my user interface.
I am registering a callback to a completely separate 3rd party library. This callback will be called on a completely separate thread to my user interface. I need this callback to interact with the QListView widget and set a new data model, so I believe I have to use
MyDataModel * model = new MyDataModel( ui->listViewWidget );
QMetaObject::invokeMethod( ui->listViewWidget, "setModel", Q_ARG( MyDataModel *, model ) );
However, It doesn't seem to work. i.e. no data is presented in the QListView widget. I've debugged the return value from QMetaObject::invokeMethod and it's returning false which suggests there's no method called "setModel" on the QListView. However, when I arrange for the callback to be called via the user interface thread, i.e. through a pushbutton on_clicked() event and make a call to
MyDataModel * model = new MyDataModel( ui->listViewWidget );
ui->listViewWidget->setModel( model );
This works perfectly, so there is a "setModel" method on the QListView..
Can someone please help me understand why QMetaObject::invokeMethod isn't working and maybe clear up wether I need to call invokeMethod in this way. i.e. are my thread assumptions correct about it needing to be ran on the event loop thread.
Yours, dazed and confused..
Mark.
The method QMetaObject::invokeMethod invokes only slot or signal on the object. Therefore your setModel must declared as slot. Also Q_ARG() takes a type name and a const reference of that type.

How to determine which sibling receieves the event?

A QWidget Class is a parent of multiple QWidget siblings which overlap. When implementing the mousePressEvent just the most recent constructed child is recieving the event.
Is there a way that all siblings get the event?
Or even better a way to set which child is expected to be recieving it?
p.s. I'm assuming this is clear enough without providing sourcecode, especially since the minimal example would be quiet big anyway. If some one expects the code to be required anyway, leave a comment and I'll add it.
You might want to use QEvent::ignore() function to mark an event as ignored in the widget. Doing so you will propagate it to the parent widget. According to Qt docs on QEvent::ignore() function:
Clears the accept flag parameter of the event object, the equivalent
of calling setAccepted(false). Clearing the accept parameter indicates
that the event receiver does not want the event. Unwanted events might
be propagated to the parent widget.
You should generate new event in callback method of each sibling class.
void QWidgetChild::mousePressEvent(QMouseEvent *event)
{
// Act
// Pass to parent
QWidget::mousePressEvent(event);
// Generate new event for objects of sibling classes
// How?
}
See postEvent documentation.

Qt add function call to event loop from other thread

I've stumbled across a problem I can't solve on an elegant way right now.
The situation: I have a callback function which is called from outside my application. The callback function has to update some gui object.. Since I can't call (for example) repaint() from within another thread, I have to finde some way to add a function call to the main event loop so the task gets executed at some time.
One possible way would be to use this:
QMetaObject::invokeMethod(object, "functionName", Qt::QueuedConnection, Q_ARG(float, value));
However, this just gives me the response that no such Method "Object::functionName". (which is obviously a lie!)
I've also read about connecting a signal to a slot which will be called from the event loop by setting the connection type to Qt::QueuedConnection. However, using QOjbect.:connect() won't work since I don't knwo which object the signal needs to get. Nice would be something like
QObject::emit(object, SIGNAL(function(flaot)), arg);
QMetaObject::invokeMethod is usually what you should use in this kind of situation. Make sure that:
object is a QObject subclass with the Q_OBJECT macro at the top
functionName is either declared in the slots section or has the Q_INVOKABLE macro

How to access QHeaderView::sectionsInserted slot

I wanted to know how I can gain access to the sectionsInserted slot.I need access to the parameters of that method. Since it is a protected slot I think I would need to inherit from QHeaderView. Now even if I inherit from QheaderView how would I attach that Qheaderview to my tableview ?
You can set the vertical and horizontal headers of a QTableView with QTableView::setVerticalHeader() and QTableView::setHorizontalHeader(), respectively.
You should use QAbstractItemModel::columnsInserted or QAbstractItemModel::rowsInserted signals instead. They have exactly the same signature. I believe they are connected to the QHeaderView::sectionsInserted slot.
Subclassing QHeaderView will not help you. QHeaderView::columnsInserted is not virtual, so your implementation will not be called by Qt.