You cannot use pointers as value types with these structures. Try using QPointers, but I am not sure if it will work.
EDIT: actually it works with pointers too. Could you provide more code, since the problem is there.
Regards
You cannot use pointers as value types with these structures. Try using QPointers, but I am not sure if it will work.
EDIT: actually it works with pointers too. Could you provide more code, since the problem is there.
Regards
I think you forgot to include QHash in the cpp.
But in the manual there is an example concerning the use of contains() and value() and the operator[]() in which is created data like mine:
QHash is included.In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
To avoid this problem, replace hash[i] with hash.value(i) in the code above.Qt Code:
// WRONG QHash<int, QWidget *> hash; ... for (int i = 0; i < 1000; ++i) { if (hash[i] == okButton) cout << "Found button at index " << i << endl; }To copy to clipboard, switch view to plain text mode
Giuseppe CalÃ
Here the header file:
Qt Code:
#ifndef MAINDIALOG_H #define MAINDIALOG_H #include <QDialog> #include <QHash> class QComboBox; { Q_OBJECT public: private: QComboBox *comboBox_1; QComboBox *comboBox_2; QComboBox *comboBox_3; QHash<int, QComboBox*> hash; hash.insert(1, comboBox_1); hash.insert(2, comboBox_2); hash.insert(3, comboBox_3); }; #endifTo copy to clipboard, switch view to plain text mode
Giuseppe CalÃ
But you cannot do this in the header, in the class declaration.hash.insert(1, comboBox_1);
hash.insert(2, comboBox_2);
hash.insert(3, comboBox_3);
It has to be in a function.
Regards
You're right; now the program compiles but there is a segmentation fault during execution; here the cpp file:
Qt Code:
#include <QtGui> #include "maindialog.h" { hash.insert(1, comboBox_1); hash.insert(2, comboBox_2); hash.insert(3, comboBox_3); for(int i = 1; i < 4; i++) for(int j = 0; j < 4; j++) hash.value(i)->addItem("New Entry"); QVBoxLayout mainLayout; mainLayout.addWidget(comboBox_1); mainLayout.addWidget(comboBox_2); mainLayout.addWidget(comboBox_3); }To copy to clipboard, switch view to plain text mode
The problem seems the line using hash member.
Bye
Giuseppe CalÃ
You have to instantiate the combos BEFORE adding them to the hash.
Regards
Ops, you're again right.
Thanks
Giuseppe CalÃ
you also should create the layout with new instead of using an object that dies when it reaches the end of the scope of the constructor.
Bookmarks