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. this is weird...
Forum Updated to NodeBB v4.3 + New Features

this is weird...

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 10.9k Views 3 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.
  • mrjjM mrjj

    Hi
    QVector<QColor> MyColor;
    means, dynamic list of colors. (it just defines it)
    But unless do you
    MyColor.append( QColor(255,0,0) );

    Then
    Qcolor c = MyColor[index];
    will crash your program as the list is empty.

    H Offline
    H Offline
    harry
    wrote on last edited by
    #15

    @mrjj said:

    Hi
    QVector<QColor> MyColor;
    means, dynamic list of colors. (it just defines it)
    But unless do you
    MyColor.append( QColor(255,0,0) );

    i guess i should have mentioned that i have a couple of lines like this:

    MyColor[3] = QColor( 0, 255, 255);

    in my initcolors method. and i have confirmed that this method gets called when i launch the app, and it assigns the colors as intended.

    should i try the append thing instead? but then how can i be sure which color ends up at which index?

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

      hi
      if u have
      MyColor[3] = QColor( 0, 255, 255);
      then list is not empty
      maybe index just too high for what u got in list.
      use the .size() to check if index is ok

      H 1 Reply Last reply
      0
      • mrjjM mrjj

        hi
        if u have
        MyColor[3] = QColor( 0, 255, 255);
        then list is not empty
        maybe index just too high for what u got in list.
        use the .size() to check if index is ok

        H Offline
        H Offline
        harry
        wrote on last edited by
        #17

        @mrjj said:

        hi
        if u have
        MyColor[3] = QColor( 0, 255, 255);
        then list is not empty
        maybe index just too high for what u got in list.
        use the .size() to check if index is ok

        just checked: size is 10 as it should be
        index is 1, well within limits.

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

          but did u do
          MyColor[0] = QColor( 0, 255, 255);
          MyColor[1] = QColor( 0, 255, 255);
          MyColor[2] = QColor( 0, 255, 255);
          MyColor[3] = QColor( 0, 255, 255);
          MyColor[4] = QColor( 0, 255, 255);
          etc
          does it have one at 1 ?

          update:
          if I do
          QVector<QColor> colors;
          colors[0]= QColor(244,0,0); <<< crash here.
          qDebug() << colors.size();

          So not sure why it seems to work for you.
          But if u have size() 10 then it must have.
          I would use append.
          just append in the order you want.

          H 1 Reply Last reply
          0
          • mrjjM mrjj

            but did u do
            MyColor[0] = QColor( 0, 255, 255);
            MyColor[1] = QColor( 0, 255, 255);
            MyColor[2] = QColor( 0, 255, 255);
            MyColor[3] = QColor( 0, 255, 255);
            MyColor[4] = QColor( 0, 255, 255);
            etc
            does it have one at 1 ?

            update:
            if I do
            QVector<QColor> colors;
            colors[0]= QColor(244,0,0); <<< crash here.
            qDebug() << colors.size();

            So not sure why it seems to work for you.
            But if u have size() 10 then it must have.
            I would use append.
            just append in the order you want.

            H Offline
            H Offline
            harry
            wrote on last edited by
            #19

            @mrjj said:

            but did u do
            MyColor[0] = QColor( 0, 255, 255);
            MyColor[1] = QColor( 0, 255, 255);
            MyColor[2] = QColor( 0, 255, 255);
            MyColor[3] = QColor( 0, 255, 255);
            MyColor[4] = QColor( 0, 255, 255);
            etc
            does it have one at 1 ?

            yes, i fill the array with 10 different colors.

            update:
            if I do
            QVector<QColor> colors;
            colors[0]= QColor(244,0,0); <<< crash here.
            qDebug() << colors.size();

            So not sure why it seems to work for you.
            But if u have size() 10 then it must have.
            I would use append.
            just append in the order you want.

            i tried append. it seems to work, but i still have the same problem: when i try to get a qcolor from the qvector like this:

            return myColor.at(1);

            i get that little window with the segmentation fault. SIGSEGV, if that makes a difference.

            i just thought of something. gotta try it out...

            H 1 Reply Last reply
            0
            • H harry

              @mrjj said:

              but did u do
              MyColor[0] = QColor( 0, 255, 255);
              MyColor[1] = QColor( 0, 255, 255);
              MyColor[2] = QColor( 0, 255, 255);
              MyColor[3] = QColor( 0, 255, 255);
              MyColor[4] = QColor( 0, 255, 255);
              etc
              does it have one at 1 ?

              yes, i fill the array with 10 different colors.

              update:
              if I do
              QVector<QColor> colors;
              colors[0]= QColor(244,0,0); <<< crash here.
              qDebug() << colors.size();

              So not sure why it seems to work for you.
              But if u have size() 10 then it must have.
              I would use append.
              just append in the order you want.

              i tried append. it seems to work, but i still have the same problem: when i try to get a qcolor from the qvector like this:

              return myColor.at(1);

              i get that little window with the segmentation fault. SIGSEGV, if that makes a difference.

              i just thought of something. gotta try it out...

              H Offline
              H Offline
              harry
              wrote on last edited by
              #20

              @harry said:

              i just thought of something. gotta try it out...

              i have all my mycolor.append() lines in initColors(), and there they work nomally. but when i copy one of them into getColor(), i get a segmentation fault. i guess that means initColors() can access the QVector but getColor() can not. question is, why? both methods (or functions or whatever) are part of the same class, so what's going on here? weird.

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

                Ok it does sound strange.
                so
                QVector<QColor> myColors;
                is defined in class .h ?
                And only there. ?

                So when ever you use myColor, its the same one, the one and only?
                can u show all the code of the place where u use
                return myColor.at(1);

                is it just a access function like
                QColor GetColor(index ) {
                return myColor.at(index);
                }

                This should work with no issues so something strange is going on :)

                so what ever u are doing with myColors we need to know

                H 1 Reply Last reply
                0
                • mrjjM mrjj

                  Ok it does sound strange.
                  so
                  QVector<QColor> myColors;
                  is defined in class .h ?
                  And only there. ?

                  So when ever you use myColor, its the same one, the one and only?
                  can u show all the code of the place where u use
                  return myColor.at(1);

                  is it just a access function like
                  QColor GetColor(index ) {
                  return myColor.at(index);
                  }

                  This should work with no issues so something strange is going on :)

                  so what ever u are doing with myColors we need to know

                  H Offline
                  H Offline
                  harry
                  wrote on last edited by
                  #22

                  @mrjj said:

                  Ok it does sound strange.
                  so
                  QVector<QColor> myColors;
                  is defined in class .h ?

                  check. that should make it available to all methods of that class, right?

                  And only there. ?

                  check

                  So when ever you use myColor, its the same one, the one and only?

                  it should be. how can i find out?

                  can u show all the code of the place where u use
                  return myColor.at(1);

                  i'm running OSX right now which is why i can't access my code. i'll post some code when i boot into ubuntu again.

                  is it just a access function like
                  QColor GetColor(index ) {
                  return myColor.at(index);
                  }

                  check. actually i make sure the index is within acceptable limits first. but yeah, that function is there to return one of those 10 colors to any UI element that requests one.

                  This should work with no issues so something strange is going on :)

                  so what ever u are doing with myColors we need to know

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

                    Ok
                    Yes if defined in class
                    it is available to all methods of that class.
                    Its defined in the class, yes ?
                    Not outside, like global variable ?

                    It sounds it should just work. So must be something
                    unexpected .
                    Looking forward to code.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #24

                      An additional check: do you have something like QVector<QColor> myColors; in your constructor ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        An additional check: do you have something like QVector<QColor> myColors; in your constructor ?

                        H Offline
                        H Offline
                        harry
                        wrote on last edited by harry
                        #25

                        @SGaist said:

                        An additional check: do you have something like QVector<QColor> myColor; in your constructor ?

                        nope, that line is in the header file. would i be right in assuming that if that line was in the constructor, no other method would have access to the qvector? IOW, wouldn't the append lines in initcolors() also give me segmentation faults? the qvector is declared in the header file and initialized to 10 elements in the constructor (see below).

                        ok, so much for the header file. now the implementation file:

                        in the constructor i have this line:
                        myColor = QVector<QColor>(10);

                        in my initcolors() method i have 10 lines like
                        myColor.append(QColor(0,0,0));
                        and
                        qDebug() << myColor.at(1);
                        and they work normally.

                        my getcolors() method (just after initcolors(), in the same class) ends with this line:
                        return myColor.at(index);
                        and that's the one that gives me the segmentation fault.

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

                          So you have something like

                          class MainWindow : public QMainWindow {
                            Q_OBJECT
                          
                           public:
                            explicit MainWindow(QWidget* parent = 0);
                            ~MainWindow();
                           public:
                            void initcolors() {
                              myColor = QVector<QColor>(10);
                              myColor[0] = (QColor(0, 0, 0));
                              myColor[1] = (QColor(244, 0, 0));
                              myColor[2] = (QColor(255, 0, 0));
                            }
                          
                            QColor GetColor(int index) {
                              return myColor.at(index);
                            }
                            void Test() {
                              qDebug() << GetColor(2);
                            }
                           private slots:
                            void on_pushButton_released();
                          
                           private:
                            Ui::MainWindow* ui;
                            QVector<QColor> myColor;
                          
                          };
                          

                          And you call initcolors from constructor.

                          If I call Test, it works as expected.

                          So can you show how you call your
                          getcolors ?
                          Should be something like class->getcolors() if outside the class

                          H 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            So you have something like

                            class MainWindow : public QMainWindow {
                              Q_OBJECT
                            
                             public:
                              explicit MainWindow(QWidget* parent = 0);
                              ~MainWindow();
                             public:
                              void initcolors() {
                                myColor = QVector<QColor>(10);
                                myColor[0] = (QColor(0, 0, 0));
                                myColor[1] = (QColor(244, 0, 0));
                                myColor[2] = (QColor(255, 0, 0));
                              }
                            
                              QColor GetColor(int index) {
                                return myColor.at(index);
                              }
                              void Test() {
                                qDebug() << GetColor(2);
                              }
                             private slots:
                              void on_pushButton_released();
                            
                             private:
                              Ui::MainWindow* ui;
                              QVector<QColor> myColor;
                            
                            };
                            

                            And you call initcolors from constructor.

                            If I call Test, it works as expected.

                            So can you show how you call your
                            getcolors ?
                            Should be something like class->getcolors() if outside the class

                            H Offline
                            H Offline
                            harry
                            wrote on last edited by
                            #27

                            And you call initcolors from constructor.

                            correct. in the myScreen constructor i initialize the QVector with myColor = QVector<QColor>(10); then in the very next line i call initColors(); that runs ok, but when the button requests a color, i get that segmentation fault :-(

                            If I call Test, it works as expected.

                            So can you show how you call your
                            getcolors ?
                            Should be something like class->getcolors() if outside the class

                            that part is in the button.cpp file. first i make sure index is an int and in the proper range, then

                            qDebug() << "index: " << index; // shows "index: 1"
                            buttonColor = screen->getColor(index);
                            qDebug() << buttonColor;

                            i noticed that initColors() was in the private: section while getColor() was in the public: section. so i moved initColors() to the public: section but that didn't help :-(

                            i simply don't understand why initColors() has access to the QVector but getColor() does not.

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

                              @harry said:
                              no it seems odd.
                              in button.cpp
                              how do you get the screen pointer?
                              Can you show how u declare it?
                              and the code where you new it. or get reference to it.

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                harry
                                wrote on last edited by
                                #29

                                i finally found out what was wrong: the screen pointer wasn't set. but that throws up another question: why did the button manage to call getColor()? with the pointer set to some random value, the app should have crashed...

                                but now i have another problem. i added another class to my project, with a QVector<myButton>

                                in the header i have this:

                                QVector<myButton> buttons;

                                and in the constructor of the new class

                                buttons = QVector<myButton>();

                                that last line is the problem. if i comment it out, it compiles ok, but when i leave it in, i get a whole bunch of error messages, none of which tell me much. my current theory is that something is wrong with the myButton class, but i have no idea what.

                                jsulmJ 1 Reply Last reply
                                0
                                • H harry

                                  i finally found out what was wrong: the screen pointer wasn't set. but that throws up another question: why did the button manage to call getColor()? with the pointer set to some random value, the app should have crashed...

                                  but now i have another problem. i added another class to my project, with a QVector<myButton>

                                  in the header i have this:

                                  QVector<myButton> buttons;

                                  and in the constructor of the new class

                                  buttons = QVector<myButton>();

                                  that last line is the problem. if i comment it out, it compiles ok, but when i leave it in, i get a whole bunch of error messages, none of which tell me much. my current theory is that something is wrong with the myButton class, but i have no idea what.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #30

                                  @harry This line is not needed because buttons is a member variable and its default constructor will be called automatically:

                                  buttons = QVector<myButton>();
                                  

                                  What errors do you get?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #31

                                    Based on the class name, wouldn't myButton be a derived class of QPushButton ? If so you can't use it like that, QObject derived class can't be copied. More information here

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    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