0

I would like to take result from object of struct.

allitems.h

#ifndef ALLITEMS_H
#define ALLITEMS_H
#include <QString>
class allitems
{
public:
    allitems();
    struct magazalar{
        QString rev;
    }kfc;
};
#endif // ALLITEMS_H

item.cpp

#include "allitems.h"
allitems::allitems()
{
    kfc.rev="2";
}

now I would like to take result of kfc.rev from another cpp file

void MainWindow::clicked(){
    allitems aaa;
    QPushButton *xx=(QPushButton *)sender();
    //xx->objectName() returns "kfc"
    qDebug()<<aaa.(xx->objectName()).rev;
}

I would like to call kfc.rev with clicking button. when I clicked the button button objectname is kfc I can take as a result but I couldnt achieve to call struct data from button objectnames

Any idea to solve it?

5
  • where does kat come from? Commented Dec 27, 2017 at 10:26
  • You can not access a member with its name as string :aaa.(xx->objectName()).kat. Commented Dec 27, 2017 at 10:29
  • I'm sorry I just make mistake. I just edited. It is not kat, it should be rev Commented Dec 27, 2017 at 10:30
  • Is there anyway to convert string to member type? @seleciii44 Commented Dec 27, 2017 at 10:37
  • you could use a map(std::map or QMap) but that requires all of your members to be the same type (or at least same polymorphic base) Commented Dec 27, 2017 at 10:41

2 Answers 2

3

You cannot do like that:

qDebug()<<aaa.(xx->objectName()).kat;

it is not valid C++, this: (xx->objectName()) must be known at compile time, not at runtime. If you want to make it work at runtime, you would need to use a map, or if statements:

here you can use simple if-s:

if (xx->objectName() == "kfc")
 qDebug()<<aaa.kfc.kat;
//else if (xx->objectName() == "some_other_kfc")
// qDebug()<<aaa.some_other_kfc.kat;

but I dont think its best design, usually you associate with a button a click handler which knows which structure to modify - and it does not need to derive this knowledge from the button instance.

Sign up to request clarification or add additional context in comments.

3 Comments

yes I can use the if statements but I will have more than 30 buttons I dont want to write other 30 different if statements so I have to access member with using object name of button.
You may add to allitems method which will return magazalar by name, like: magazalar& getData(QString name) { if (name == "kfc") return kfc; .... }. C++ does not support (yet) reflection.
I will try your approach but I think my problem occurs because the invalid calling of member. I have to convert the QString (xx->objectName() ) to someting that I can access to struct member kfc.rev I dont know what should I need to convert to.
0

The use of sender() is bad code smell, usually, and indicates that you should be doing something else instead.

In modern C++, you can easily generate the necessary code when you connect the buttons. Let's assume that aaa is a member of MainWindow:

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
  auto const clicked = &QPushButton::clicked;
  connect(ui->kfc, clicked, [this]{ qDebug() << this->aaa.kfc.rev; });
  //more connect statements here...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.