I have this code:
QWidget *w = qobject_cast<QWidget *>(d->m_object);
w->setObjectName("test");
it's can not emit objectNameChanged signal, so I want to manual emit signal,
w->objectNameChanged("test",QWidget::QPrivateSignal)); but report error QWidget::QPrivateSignal is private). How can I emit QWidget::objectNameChanged() signals?
From the Documentation (5.7):
Note: This is a private signal. It can be used in signal connections
but cannot be emitted by the user.
Anyway, the signal should be emitted, when you change the name, there should be a mistake somewhere else.
Related
I know how to call a signal from inside the class where the signal is located: by using emit. But what if I want to call it externally, from the parent object?
The reason why I want to do is is because a given QPushButton is connected to a slot which picks the button that called it by using sender(). Now I want the same functionallity of that slot to be called but not after a manual click in the button, but from within the code itself. Normally I would just programatically call for the slot, but because of the usage of sender(), that way is invalid: calling the slot directly will not give it the id of the button.
So how could I "externally" ask for the object for it to emit one of its signals? That is, how can I make my QPushButton emit its clicked() signal from within the code, not by clicking the button with the mouse?
You cannot emit signal directly from the parent object.
Only the class that defines a signal and its subclasses can emit the signal. (http://qt-project.org/doc/qt-4.8/signalsandslots.html#signals)
You can define a method in your QPushButton emitClicked() where you emit the signal. Then you call emitClicked() on instance of your QPushButton from the code.
The Qt headers contain this interesting line (in qobjectdefs.h):
#define emit
Which means that presence or absence of the emit keyword has no effect on the code, it's only there to make it more obvious to the human reader that you are emitting a signal. That is to say:
emit clicked();
is exactly the same (as far as the C++ compiler is concerned) as
clicked();
Given that, if you want your button to emit its clicked() signal, it's just a matter of doing this:
myButton->clicked();
(although if you wanted to be clever about it, this would work equally well):
emit myButton->clicked();
Seems that Qt Test module is just for this case.
Check out this tutorial on simulating GUI events.
Basically you use QTest::​mouseClick and pass pointer to your push button.
Iam new in Qt and I have problem how to pass QAction as parameter like this code:
connect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(fileToolBarAct));
And this my slots function:
void MainWindow::ToggleBar(QAction& what)
{
what.isCheckable();
}
QObject::connect doesn't work like this. You can not pass objects to SIGNAL and SLOT macros. SIGNAL and SLOT macros should take function signatures. In addition the signature of a signal must match the signature of the receiving slot as described in the Qt documentation.
I see that you lack in understanding the signals and slots mechanism and I recommend you read the Qt Signals and Slots documentation for more info. Reading the Qt Signals and Slots documentation will clear everything for you.
onnect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(bool));
void MainWindow::ToggleBar(bool checked)
{
QAction* action = qobject_cast<QOAction*>(sender());
if (action)
action->setChecked(checked);
}
I am trying to emit a signal on completion of creating an object. Like this:
Component.onCompleted: mySignal()
This is in a QML file deep in the hierarchy. I now see two solutions for accessing this signal in C++.
First is passing the signal up the hierarchy until main.qml and then in C++ do this:
//Create a Quick View object.
QQuickView *view = new QQuickView();
//Object to access QML properties and childs.
QObject *container = (QObject *) view->rootObject();
//Connect signal and slots
QObject::connect(container, SIGNAL(mySignal()), this, SLOT(onMySignal()));
This I have tried and for some reason the slot is not called. It works for all the other signals I send and emit from main.qml, but not the one emitted from Component.onComplete. I can verify that the signal is emitted from the QML side, but never received on the C++ side.
The second thing I tried was instead of passing the signal to main.qml, I would get a reference to the QML file emitting the signal I want. I tried that doing so:
//Create a Quick View object.
QQuickView *view = new QQuickView();
//Object to access QML properties and childs.
QObject *container = (QObject *) view->rootObject();
//Connect signal and slots
QObject::connect(container->findChild<QObject*>("mySignalQmlFile"), SIGNAL(mySignal()), this, SLOT(onMySignal()));
where mySignalQmlFile is the ID of the main rectangle that has the signals defined within it.
and I get the error:
QObject::connect: No such signal QQuickRectangle_QML_54::mySignal() in ..\GC\mainwindow.cpp:62
I am now not sure how to proceed.
For your first try, I think it is possible that your signal is emitted before you even connect your signal and slot so that it is never received the signal.
For your second try, you need to use objectName from your qml file, not "mySignalQmlFile".
I recommend you to read this following tutorial.
http://developer.nokia.com/community/wiki/Using_objectName_to_find_QML_elements_from_Qt
Also, your qml file needs to be called or used before you connect signals and slot. Otherwise, it will not be able to find the object so that you would get the same error.
I am unable to find a proper simulation for ItemClicked() SIGNAL for QTreeWidget.
Is there a way to simulate it so that ItemClicked Signal is generated ?
e.g: we can emit ItemClicked in a derived class of QTreeWidget but cannot (as a QT rule) outside of it.
You can't use the emit call for class A to emit class B's signals. But note that the documentation for signals and slots says:
"You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)"
So you can work around this by declaring a signal in class A of the same signature as the one you want class B to emit, and connecting the signals together:
connect(
myclass, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
treewidget, SIGNAL(itemClicked(QTreeWidgetItem*, int))
);
Then emit itemClicked from myclass. If I'm not mistaken, it will work for this case...and fire the treewidget's itemClicked signal for you.
I want to display a message box from a separate thread, however, I get this error:
QThread: Destroyed while thread is still running
Can anyone explain how to display a message box from a thread?
Emit a signal. Since you cannot do UI stuff in a Qthread, instead send your message as an argument of your signal.
signal decalaration in your qthread:
signals:
void write2SysStatus(QString theMessage);
emitting the signal from the qthread:
emit write2SysStatus("Some status");
slot declaration/definition in QMainWindow:
public slots:
void eWriteLine ( QString theMessage ){
//this is where you use you message box.
}
connecting of the slot and signal:
connect(pFPSengine, SIGNAL(write2SysStatus(QString)), this,SLOT(eWriteLine(QString)));