Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMap Struct and finding key/value!
Qt 6.11 is out! See what's new in the release blog

QMap Struct and finding key/value!

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kris Revi

    @SGaist of the Mainwindow Class?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @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

    K 1 Reply Last reply
    2
    • mrjjM mrjj

      @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

      K Offline
      K Offline
      Kris Revi
      wrote on last edited by
      #5

      @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
      
      
      K 1 Reply Last reply
      0
      • K Kris Revi

        @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
        
        
        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #6
        This post is deleted!
        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #7

          What todo now?

          mrjjM 1 Reply Last reply
          0
          • K Kris Revi

            What todo now?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

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

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

            K 1 Reply Last reply
            1
            • mrjjM mrjj

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

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

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #9

              @mrjj yes and thank you :)

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #10

                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";
                
                K 1 Reply Last reply
                0
                • mrjjM mrjj

                  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";
                  
                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by Kris Revi
                  #11

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

                  JonBJ B 2 Replies Last reply
                  0
                  • K Kris Revi

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

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #12

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

                    1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #13

                      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.

                      1 Reply Last reply
                      0
                      • K Kris Revi

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

                        B Offline
                        B Offline
                        Bonnie
                        wrote on last edited by
                        #14

                        @Kris-Revi
                        "Studio Lights" and "Studio lights"?
                        It is case sensitive.

                        1 Reply Last reply
                        3

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved