QAction "data" member not found - c++

As openNote is a QAction I can set its object name via setObjectName. Then why can't I have access to the data? I have no clue.
QAction *openNote;
QVariant noteID;
openNote = m_mainContextMenu.addAction(menuEntryName);
openNote->setObjectName("noteEntry"); //QAction::setObjectName
int ID = m_noteList[0].data()->getID();
noteID.setValue(ID);
openNote->setData(noteID); //QAction::setData
connect(openNote,SIGNAL(triggered()),this,SLOT(s_showNote()));
my slot:
void Traymenu::s_showNote(){
QObject* obj = sender(); //sender is "openNote" of type QAction
qDebug() << "objectName" << obj->objectName(); //works, because obj = QAction
obj->data(); //no member data found?! Why? Documentation says there is...
My auto completion shows every member, e.g. setData, objectName, but not data. How can I get access to it?
If I write
QAction bla;
bla.data(); //<== auto completion shows "data"
Where is my problem?

This is the solution that I have found out:
void Traymenu::s_showNote(){
QObject* obj = sender();
QAction *noteEntry = qobject_cast<QAction *>( sender() );
int noteID = noteEntry->data().toInt();
qDebug() << "noteID" << noteID;
}

Related

Qt focusObjectChanged example?

I'm looking for an example on how to process the QObject pointer received by the slot when a 'focusObjectChanged' signal is received. I'd like to know how to identify the object that is getting the focus?
The documentation indicates that the pointer is to the focus'd object, but how do I know which?
I've tried analysing the pointer in the debugger but I don't see anything obvious.
You can determine the class and object name, widget properties...:
QObject::connect(qApp, &QApplication::focusObjectChanged, [](QObject *obj){
if(obj) qWarning() << obj->metaObject()->className() << obj->objectName();
QWidget* widget = qobject_cast<QWidget*>(obj);
if(widget) qWarning() << widget->geometry(); // or other properties
BaseType* baseType = qobject_cast<BaseType*>(obj);
if(baseType) qWarning() << baseType->some_actions();
Type1* type1 = qobject_cast<Type1*>(obj);
if(Type1) qWarning() << type1->some_actions();
Type2* type2 = qobject_cast<Type2*>(obj);
if(Type2) qWarning() << type2->some_actions();
});
or compare the pointer with widget pointers to find the widget you need:
SomeClassSlot(QObject* obj) {
if(!obj) return;
if(obj == m_mainWindows) ....
if(obj->parent() == m_table) ...
MyClass myClass = qobject_cast<MyClass*>(obj);
if(myClass) myClass->some_method();
}

Do an action while any checkbox state is modified with Qt

In my programm, I fill my interface with a lot of checkbox by this way :
void VGCCC::addMaterialToUI(QDomNodeList _materialNodeList, QWidget* _areaWidget, QLayout* _layout, QWidget* _layoutWidget, int _maTable)
{
for(int i=0; i< _materialNodeList.count();i++)
{
QDomElement materialElement = _materialNodeList.at(i).toElement();
QString elementFile = materialElement.attribute("file");
QString elementId = materialElement.attribute("id");
QString elementLabel = elementId;
elementLabel += " - ";
elementLabel += materialElement.attribute("label");
QCheckBox* checkbox = new QCheckBox(elementLabel);
_layout->addWidget(checkbox);
_layoutWidget->adjustSize();
_areaWidget->setMinimumHeight(_layoutWidget->height());
_areaWidget->setMinimumWidth(_layoutWidget->width());
configuration c;
c.path = (m_igmPath+elementFile).toStdString();
c.id = elementId.toInt();
c.name = elementLabel.toStdString();
if(_maTable==0)
{
m_materialSectionMap[checkbox] = c;
}
else
{
m_materialPostMap[checkbox] = c;
}
}
}
I would like to know how to retrieve these "abstract" checkbox. More exactly, if one of these checkbox is checked, I would like to call another function like this :
connect(anyCheckbox,SIGNAL(stateChanged(anyCheckbox)), this, SLOT(doSomethingFunctionIfCheckboxIsChecked()));
The difficulty is that in my UI, these checkbox didn't exist, so I can't connect them to my function. How can I solve it ?
You can e.g. collect pointers to your checkbox objects to a list so can access or "retrieve" them later.
You can connect each checkbox's stateChanged signal to a same slot which is then called when state of any of the checkboxes is changed. In the slot you can cast the sender() to a checkbox if you need to know which specific checkbox is in question. Another alternative is to use QSignalMapper.
In your class declaration:
private slots:
void checkboxStateChanged(int state)
private:
QList<QCheckBox*> m_checkboxes;
In your class definition:
void VGCCC::addMaterialToUI(QDomNodeList _materialNodeList, QWidget* _areaWidget, QLayout* _layout, QWidget* _layoutWidget, int _maTable)
{
...
QCheckBox* checkbox = new QCheckBox(elementLabel);
m_checkboxes.append(checkbox);
connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(checkboxStateChanged(int)));
...
}
void VGCCC::checkboxStateChanged(int state)
{
// Here your can e.g. call doSomethingFunctionIfCheckboxIsChecked()
QCheckBox* checkbox = qobject_cast<QCheckBox*>(sender());
if (checkbox)
{
// checkbox points to the object whose state changed
}
}

How to pass a parameter using QSignalMapper, incompatible sender/receiver arguments

Implementation:
void Test::addProcessToList(const QString &command, const QString &id, const BasicInfo &basicInfo) {
QProcess *console = new QProcess();
QSignalMapper* signalMapper = new QSignalMapper (this) ;
connect (console, SIGNAL(readyRead()), signalMapper, SLOT(map())) ;
connect (console, SIGNAL(finished(int)), signalMapper, SLOT(processFinished(int))) ;
signalMapper->setMapping (console, id) ;
connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(pidOut(QString))) ;
console->start(command);
}
void Test::registerProcess(QString id) {
QProcess *console = qobject_cast<QProcess*>(QObject::sender());
QByteArray processOutput = console->readAll();
int mainPID = parsePID(processOutput);
BasicInfo basicInfo;
qDebug() << "Registering id: " + id + " mainPID: " + mainPID;
if(mainPID != 0) {
Main::getInstance()->addProcessToList(mainPID, packageId, basicInfo);
} else {
qWarning() << "pidOut Error fetching mainPID";
}
}
void Test::processFinished(int exitCode) {
QProcess *console = qobject_cast<QProcess*>(QObject::sender());
QByteArray processOutput = console->readAll() + QString("Finished with code %1").arg(exitCode).toLatin1();
qDebug() << " processFinished: " + processOutput;
}
prototypes:
private
void addProcessToList(const QString &command, const QString &id, const BasicInfo &basicInfo);
private slots:
void registerProcess(QString);
void processFinished(int);
I get this errors when I call connect, which tells me I'm doing it wrong:
"QObject::connect: Incompatible sender/receiver arguments
QSignalMapper::mapped(int) --> Test::registerProcess(QString)"
I'm not understanding where I'm suppose to specify my parameter (QString id) so that registerProcess will receive it when it's called? I'm assuming I'm doing this part wrong, cut from above:
signalMapper->setMapping (console, id) ;
connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(pidOut(QString))) ;
QSignalMapper can emit either mapped(const QString & text) or mapped(int i) signals. The type is defined by setMapping(QObject * sender, int id) or setMapping(QObject * sender, const QString & text).
That led to confusion probably by autocompletion in
connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(pidOut(QString)));
The types of signal and slot must be the same for connection.
You set string mapping (QString &id), so the signal in the connection should be QString:
connect (signalMapper, SIGNAL(mapped(QString)), this, SLOT(pidOut(QString)));
Update
After deeper review of the code flow I suspect that you wanted to connect mapper to registerProcess() slot instead of pidOut(). In that slot you can have as an argument QString id that was passed to signalMapper in setMapping() call. That is the purpose of using QSignalMapper.
However, beside that id it is not possible to extract console pointer, since in that case sender() is signalMapper object. If it is the case, QSignalMapper cannot help you here. You should use direct connection of console and this on readReady (of course with slot of this with void argument as readReady()). To get the string id in that slot it is possible to use simple QMap<QProces*, QString> map stored as a Test class member.
// addProcessToList(...):
map[console] = id;
//registerProcess():
QString id = map[console];
//processFinished(...):
map.remove(console);
By the way, it is not needed to created a new instance of QSignalMapper for each map item.

Extract menu action data in receiving function or slot

In my menu, I am setting data to the menu actions. How can I extract that data in my slot? Or even better, instead of connecting a slot, can I also connect a member function that is able to extract the action data (like in the 1st connect)? The action data is meant to identify each action. As a sidenode, I am not sure if I can use several menu action entries on only one openNote-action.
void Traymenu::createMainContextMenu() {
QAction *actionNewNote = m_mainContextMenu.addAction("Neue Notiz");
actionNewNote->setIcon(QIcon("C:\\new.ico"));
actionNewNote->setIconVisibleInMenu(true);
QObject::connect(actionNewNote,&QAction::triggered,this,&Traymenu::newNote);
QString menuEntryName;
QAction *openNote;
QVariant noteID;
for (int i = 0; i<m_noteList.count(); i++) {
std::string noteTitle = m_noteList[i].data()->getTitle();
menuEntryName = QString::fromStdString(noteTitle);
openNote = m_mainContextMenu.addAction(menuEntryName);
connect(openNote,SIGNAL(triggered()),this,SLOT(s_showNote()));
noteID.setValue(m_noteList[i].data()->getID());
openNote->setData(noteID);
}
m_mainIcon.setContextMenu(&m_mainContextMenu);
}
And the slot:
void Traymenu::s_showNote() {
QObject* obj = sender();
//int noteID = data.toInt();
//Search all notes in noteList for that ID and show it
}
Using QObject::sender()
You can use QObject::sender() to get the signal's sender, followed by qobject_cast to cast the sender pointer to the right type.
void Traymenu::s_showNote()
{
QAction* act = qobject_cast<QAction *>(sender());
if (act != 0)
{
QVariant data = act->data();
int noteID = data.toInt();
showNote(noteID); // isolate showNote logic from "get my ID" stuff
}
}
void Traymenu::showNote(int noteID)
{
// Do the real work here, now that you have the ID ...
}
As the Qt documentation warns, "This function violates the object-oriented principle of modularity." It's still a fairly safe and standard practice, though — just one with some shortcomings. In particular, note that you're committing to having a s_showNote method that only works when it's accessed as a slot (otherwise sender is 0).
Using QSignalMapper
Alternatively, you can use the QSignalMapper class to return a pointer to teh item or to associate a unique identifier (int or QString) with each item.
Something like this:
void Traymenu::createMainContextMenu()
{
signalMapper = new QSignalMapper(this); // (or initialize elsewhere)
// ... (create your newNote here same as before) ...
QString menuEntryName;
QAction *openNote;
int noteID;
for (int i = 0; i<m_noteList.count(); i++) {
std::string noteTitle = m_noteList[i].data()->getTitle();
menuEntryName = QString::fromStdString(noteTitle);
openNote = m_mainContextMenu.addAction(menuEntryName);
noteID = m_noteList[i].data()->getID();
openNote->setData(QVariant(noteID)); // (if you still need data in the QActions)
signalMapper->setMapping(openNote, noteID);
}
connect(signalMapper, SIGNAL(mapped(int)),
this, SLOT(showNote(int)));
m_mainIcon.setContextMenu(&m_mainContextMenu);
}
void Traymenu::showNote(int noteID) {
// Now you have the ID ...
}
This pattern has the benefit of isolating all the ugly "Wait, how do I get my identifier?" stuff in one spot, instead of having both the initialization code and the slot function having code for associating actions and IDs.
I would write it like:
void Traymenu::s_showNote() {
QObject* obj = sender();
QAction *action = qobject_cast<QAction *>(obj);
int id = action->data().toInt();
for (int i = 0; i < m_noteList.count(); i++) {
if (m_noteList[i].data()->getID() == id) {
[..]
}
}
}

Drag and drop issue in Qt: Pass parameter to receiving dropEvent

A healthPackButton is dropped on a mysquare. Now I would like to add a value to this button (because I have a number of healthPackButtons and I want to be able to differentiate them). I have tried changing the makeDrag function for it to accept an extra parameter but than my SIGNAL was no longer matched.
Question: How can I pass additional information (=> int value) to my dropEvent handler inside another class.
Dialog class
for (int i=0; i<healthPks.size(); i++){
int value = healthPks.at(i);
QPushButton *healthPackButton = new QPushButton(title,this);
connect(healthPackButton,SIGNAL(pressed()),this,SLOT(makeDrag()));
}
void Dialog::makeDrag(){
QDrag *drag = new QDrag(this);
QMimeData *mime = new QMimeData;
mime->setText("This is a test");
drag->setMimeData(mime);
drag->start();
}
mysquare class
void MySquare::dropEvent(QGraphicsSceneDragDropEvent *event){
isHealthPack=true;
int xCoord = curX/width;
int yCoord = curY/height;
int value = 0; //what's the value??
const QMimeData *mimeData = event->mimeData();
emit healthMapChanged(xCoord,yCoord,value);
update();
}
To get an additional parameter into your slot, you can use a QSignalMapper – the documentation has an example of adding a QString const& parameter, but you can use an int in exactly the same way to pass the value to a makeDrag(int).
You could then use QMimeType's setData (converting your int to a QByteArray using QByteArray::number for example) to get that value to the drop target.