Solved QMap Struct and finding key/value!
-
Hi,
Make "board" a member variable.
-
@SGaist of the Mainwindow Class?
-
@Kris-Revi
Yes currently board is only usable in the constructor but if you move it
to be a member, you can use it to look up the IP
in the on_BUTTON_STUDIOLIGHT_clicked -
@mrjj this is how my class looks now
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMap> #include <QString> #include "Structs.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QMap<QString, DeviceInfo> board; // <-------------- board struct QMap<QString, Pattern> pattern; // <-------------- pattern struct Hue hue; // <-------------------- Hue Struct private slots: void on_BUTTON_ADD_DEVICE_clicked(); void on_BUTTON_QUIT_clicked(); void on_BUTTON_MINIMIZE_clicked(); void on_BUTTON_MAXIMIZE_clicked(); void on_BUTTON_STUDIOLIGHT_clicked(); private: Ui::MainWindow *ui; protected: void mousePressEvent(QMouseEvent *) override; }; #endif // MAINWINDOW_H
-
This post is deleted! -
What todo now?
-
@Kris-Revi
Hi
You mean how to use the map ?DeviceInfo &di = board["Studio Light"];
connectTo( di.ip ); // -
@mrjj yes and thank you :)
-
Hi
Ok :)
There is one thing with std::map you need to be aware of.
If you try to use a key you don't have, it will create a new empty DeviceInfo that
it will then return so a more correct way would beauto key = "Studio Light"; if ( board.count(key) ) { DeviceInfo &di = board[key]; connectTo( di.ip ); // } else qDebug() << "error invalid loopup name";
-
@mrjj ty for that info :)
void MainWindow::on_BUTTON_STUDIOLIGHT_clicked() { auto key = "Studio lights"; if ( board.count(key) ) { DeviceInfo &di = board[key]; connectTo( di.ip ); } else { qDebug() << "error invalid loopup name"; } }
all im getting is
error invalid loopup name
and i have checked the name is correct! -
@Kris-Revi
I cannot spot that error on the code you have pasted! Are you sure it does not come from elsewhere, the error message will be indicating a line number?
EDIT You have just edited your message to omit the error message you claimed!Your new error:
all im getting is error invalid loopup name and i have checked the name is correct!
I take it this is a runtime error, not compile time? So, presumably, that comes from
board.count(key)
orboard[key]
, so what is the declaration ofboard
? OK, just seen from earlier, @mrjj is answering below.... -
Hi
Did you forget to remove the old board in constructor ?QMap<QString, DeviceInfo> board;board.insert("Studio Lights", DeviceInfo{"Studio lights", false, "192.168.10.138", "online", "off"}); board.insert("Neon Heaven", DeviceInfo{"Neon Heaven", true, "192.168.10.146", "online", "off"}); board.insert("Vitrineskap", DeviceInfo{"Vitrineskap", false, "vitrineskap", "online", "off"}); board.insert("Voodoo Mask", DeviceInfo{"Voodoo Mask", false, "voodoomask", "online", "off"}); board.insert("Test Device", DeviceInfo{"Test Device", false, "test123", "online", "off"});
as else you have 2 (one local and one member) and the one you made of a member will be empty.
-
@Kris-Revi
"Studio Lights" and "Studio lights"?
It is case sensitive.