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. QVector append costum data type

QVector append costum data type

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 5.1k Views 1 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by pauledd
    #1

    Hi there

    This might be a more C++ related question but since it uses the QVector I will try it here.

    I've made a costum data type made of a simple struct that holds three short int's

    struct RGB
    {
       short red;
       short green;
       short blue;
    };
    

    then I created a QVector of type RGB

    QVector<RGB> vec;
    

    Now I want to append data to the QVector like this

    blue = 3882;
    red = 9293;
    green = 3828;
    vec.append(RGB(red,green,blue));
    

    I dont know how this kind of use is called but I use that very often in Qt with its own data types.
    How can I make my own RGB data type able to do this in that way?

    Of cause when I try to compile this I get an error:
    0_1514892197422_Bildschirmfoto_2018-01-02_12-22-24.png

    aha_1980A 1 Reply Last reply
    0
    • pauleddP pauledd

      Hi there

      This might be a more C++ related question but since it uses the QVector I will try it here.

      I've made a costum data type made of a simple struct that holds three short int's

      struct RGB
      {
         short red;
         short green;
         short blue;
      };
      

      then I created a QVector of type RGB

      QVector<RGB> vec;
      

      Now I want to append data to the QVector like this

      blue = 3882;
      red = 9293;
      green = 3828;
      vec.append(RGB(red,green,blue));
      

      I dont know how this kind of use is called but I use that very often in Qt with its own data types.
      How can I make my own RGB data type able to do this in that way?

      Of cause when I try to compile this I get an error:
      0_1514892197422_Bildschirmfoto_2018-01-02_12-22-24.png

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @pauledd said in QVector append costum data type:

      struct RGB
      {
      short red;
      short green;
      short blue;
      };

      Most likely you simply have to add a constructor to initialize the members to your struct:

      struct RGB
      {
        RGB(short red, short green, short blue) : red(red), green(green), blue(blue) {}
      
         short red;
         short green;
         short blue;
      };
      

      Happy New Year!

      Qt has to stay free or it will die.

      1 Reply Last reply
      5
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        You are just using the wrong brackets: vecImage.append(RGB(red,green,blue)); should be vecData.append(RGB{red,green,blue}); see http://en.cppreference.com/w/c/language/struct_initialization

        I tend to agree, however, with @aha_1980 in saying that a specific constructor is more robust or at least use the named version

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • pauleddP Offline
          pauleddP Offline
          pauledd
          wrote on last edited by pauledd
          #4

          Ok first
          @aha_1980
          I dont know why but this doesnt work. Here's my complete code:
          mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          #include <QMainWindow>
          
          struct RGB
          {
              RGB(short red, short green, short blue) : red(red), green(green), blue(blue) {}
              short red;
              short green;
              short blue;
          };
          
          namespace Ui {
          class MainWindow;
          }
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          private:
              Ui::MainWindow *ui;
              QVector<RGB> vec;
          };
          
          #endif // MAINWINDOW_H
          

          mainwindow.cpp:

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              short red = 23;
              short green = 22;
              short blue = 28;
              vec.append(RGB(red,green,blue));
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          

          I get compile error:

          /usr/include/qt5/QtCore/qvector.h:319: Fehler: no matching function for call to ‘RGB::RGB()’
                       new (from++) T();
                       ^
          

          And @VRonin
          if I use no constructor as suggested by aha_1980 and I use the curly brackets my app crashes, with constructor it does not crash, but compiles the same error as above.

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

            Hi
            it tells you
            "no matching function for call to ‘RGB::RGB()’"

            Which means, it cannot find a default constructor that takes no arguments.
            which vector want for resize and moving stuff around. ( i think )
            so you need to add that to your RGB class if it wants it.

            btw, Qt has QRgb :)

            pauleddP 1 Reply Last reply
            4
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              RGB(short red, short green, short blue) : red(red), green(green), blue(blue) {} should be RGB(short red=0, short green=0, short blue=0) : red(red), green(green), blue(blue) {}

              I use the curly brackets my app crashes

              That means you have another problem somewhere else that is unrelated to this one. Check the stack trace to see what went wrong

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • pauleddP Offline
                pauleddP Offline
                pauledd
                wrote on last edited by
                #7

                ok, maybe I should go through this first:

                Creating a Custom Type
                
                *Before we begin, we need to ensure that the custom type we are creating meets all the requirements imposed by QMetaType. In other words, it must provide:
                
                    a public default constructor,
                    a public copy constructor, and
                    a public destructor.*
                

                http://doc.qt.io/qt-5/custom-types.html

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  it tells you
                  "no matching function for call to ‘RGB::RGB()’"

                  Which means, it cannot find a default constructor that takes no arguments.
                  which vector want for resize and moving stuff around. ( i think )
                  so you need to add that to your RGB class if it wants it.

                  btw, Qt has QRgb :)

                  pauleddP Offline
                  pauleddP Offline
                  pauledd
                  wrote on last edited by
                  #8

                  @mrjj said in QVector append costum data type:

                  btw, Qt has QRgb :)

                  Yes :D but I would take this as a lesson because I am sure I would stumble over
                  the problem in the future.

                  1 Reply Last reply
                  0
                  • pauleddP Offline
                    pauleddP Offline
                    pauledd
                    wrote on last edited by
                    #9

                    Ok , sorry I found I already asked something simmilar:
                    https://forum.qt.io/topic/71761/how-to-append-struct-to-qlist/9
                    I'll try to solve by my own and report back.

                    1 Reply Last reply
                    1
                    • pauleddP Offline
                      pauleddP Offline
                      pauledd
                      wrote on last edited by pauledd
                      #10

                      I solved it.
                      My little struct exploded into a class as described in the above linked thread.
                      I added a standart/normal/copy constructor and a descructor,
                      a assignment operator and this, to me, mysterious "encapsulate members". I can just guess this is some kind of getter/setter accessing
                      the private members.

                      class RGB
                      {
                      public:
                          RGB(){}                                             // standart constructor
                          ~RGB(){}                                            // destructor
                          RGB(const RGB &other)
                              :r_(other.r_),g_(other.g_),b_(other.b_){}       // copy constructor
                          RGB(short r, short g, short b)                      // normal constructor
                              :r_(r), g_(g),b_(b){}
                          RGB& operator = (const RGB& other)                  // assignment operator
                          {
                              r_ = other.r_;
                              b_ = other.b_;
                              g_ = other.g_;
                              return *this;
                          }
                          const short& r() const {return r_;}                 // encapsulate members
                          const short& g() const {return g_;}
                          const short& b() const {return b_;}
                          void setR(const short& val){r_ = val;}
                          void setG(const short& val){g_ = val;}
                          void setB(const short& val){b_ = val;}
                      
                      private:
                          short r_;
                          short g_;
                          short b_;
                      
                      };
                      

                      @mrjj said in QVector append costum data type:

                      btw, Qt has QRgb :)

                      If I read correctly QColor Class, the whole thing was not actually not just an exercise for me because QColor seems to not support 16bit colors like 0-65536 as such but only "getRgbF" with values from 0.0 to 1.0. Shure I could use something like 0.22816 but I would like to keep the same range as used in OpenCV Datatype cv::Vec3s.

                      The long and the short of it:
                      I can now use my RGB type directly to append a new value
                      to my QVector:

                      0_1514967617001_Bildschirmfoto_2018-01-03_09-15-38.png

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

                        mysterious "encapsulate members".

                        Hi, nothing mysterious about it. :)
                        It just means hide the details.
                        This give you the freedom to later change the class internal working without
                        breaking the program/ requires modifications outside class.

                        1 Reply Last reply
                        3
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12
                          • Your default constructor leaves the values initialised with junk, that's not a great idea usually
                          • Your destructor, copy constructor and assignment operators are all doing the default. =default them or remove them altogether
                          • short is one of those rare cases where passing/returning by value is generally more efficient than by reference/pointer (16bits vs >=32bit)
                          class RGB
                          {
                          public:
                              ~RGB()=default;                                            // destructor
                              RGB(const RGB &other)=default;       // copy constructor
                              RGB(short r=0, short g=0, short b=0)                      // normal constructor
                                  :r_(r), g_(g),b_(b){}
                              RGB& operator = (const RGB& other)=default;                  // assignment operator
                              short r() const {return r_;}                 // encapsulate members
                              short g() const {return g_;}
                              short b() const {return b_;}
                              void setR(short val){r_ = val;}
                              void setG(short val){g_ = val;}
                              void setB(short val){b_ = val;}
                          
                          private:
                              short r_;
                              short g_;
                              short b_;
                          
                          }
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          5
                          • pauleddP Offline
                            pauleddP Offline
                            pauledd
                            wrote on last edited by
                            #13

                            Thanks for your information/hints! I will correct the remaining suggestions, and this should be solved.

                            1 Reply Last reply
                            0

                            • Login

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