У меня есть QMap, и я вставляю пару ключ-значение в одну функцию, но когда я пытаюсь получить доступ к этому значению (используя тот же ключ) в другой функции, я получаю нулевое значение.
Я хотел включить весь код просто для безопасности (он ниже), но на самом деле вопрос сводится к следующему: почему я получаю нулевое значение, когда я получаю доступ к карте, используя тот же ключ (я знаю, что я ' Я использую один и тот же ключ, потому что я печатаю адрес ключа, и он одинаков оба раза).
class Something
{
QMap<QPushButton*,QWidget*> map;
};
Something::exampleFunction()
{
QPushButton* myButton = new QPushButton(someParent);
QWidget* widget = new QWidget(someParent);
connect(myButton,SIGNAL(clicked()),this,SLOT(onButtonPress()));
this->map[myButton] = widget;
cout<<"Address of myButton: " << myButton << "\n";
cout<<"Address of widget: " << this->map[myButton] << "\n";
//the above line outputs the address of the widget, which means
//the qmap is working just fine?
}
Something::onButtonPress()
{
QPushButton* buttonInFunction = qobject_cast<QPushButton*>(sender());
QWidget* widgetInFunction = this->map[buttonInFunction];
cout<<"Address of button within function: " << button;
//the above line prints the same address as
//was printed for the button in exampleFunction
cout << "Address of widget within function:" << widgetInFunction;
//the above line outputs 16 zeros, i.e. null
}