Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMap Struct and finding key/value!



  • So this is my code

    // =========================================================================================================== //
    // INCLUDE
    // =========================================================================================================== //
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "adddevice.h"
    #include "QMouseEvent"
    #include "QWindow"
    #include "Structs.h"
    #include <QFile>
    #include <QFileInfo>
    
    #include <QCoreApplication>
    #include <QCommandLineParser>
    #include <QCommandLineOption>
    #include "MySocket.h"
    #include "QDebug"
    
    // =========================================================================================================== //
    // MainWindow
    // =========================================================================================================== //
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
        // Setup Device Struct
        // ======================================================================================================= //
        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"});
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    // =========================================================================================================== //
    // BUTTON BINDINGS
    // =========================================================================================================== //
    void connectTo(QString selected_board)
    {
        // Find the IP of the "Selected_board" in the struct
    
        MySocket client(QUrl(QStringLiteral("ws://192.168.10.122:80")), true); // <---------- and put the IP here
    }
    
    // BUTTON BINDINGS
    // =========================================================================================================== //
    
    void MainWindow::on_BUTTON_STUDIOLIGHT_clicked()
    {
        connectTo(QString("Studio Light"));
    }
    

    how do i do what i commented in the code under (BUTTON BINDINGS)? :/


  • Lifetime Qt Champion

    Hi,

    Make "board" a member variable.



  • @SGaist of the Mainwindow Class?


  • Lifetime Qt Champion

    @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?


  • Lifetime Qt Champion

    @Kris-Revi
    Hi
    You mean how to use the map ?

    DeviceInfo &di = board["Studio Light"];
    connectTo( di.ip ); //



  • @mrjj yes and thank you :)


  • Lifetime Qt Champion

    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 be

    auto 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) or board[key], so what is the declaration of board? OK, just seen from earlier, @mrjj is answering below....


  • Lifetime Qt Champion

    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.


Log in to reply