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

this is weird...

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 10.0k Views
  • 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.
  • H Offline
    H Offline
    harry
    wrote on last edited by
    #1

    hi.
    something weird is going on.
    i have a couple of lines that assign colors to an array. someting like this:

    myColor[0] = new QColor( 0, 0, 0);

    the weird thing is that when i comment those lines out, the program quits normally when i click the close button of the window. but when i DON'T comment those lines out, when i click that close button, i get the following error, in red:

    The program has unexpectedly finished.

    and the qt creator informs me that the program crashed. i dont understand whats going on here. am i doing somethiong wrong?

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

      well what do you with myColor ?
      just inserting pointers to QColor in a list should not crash anything.

      how did you define myColor ?

      QColor *myColor[100];

      is it global data ?

      if you defined it in a function, then it will run out of scope and be invalid later.

      H 1 Reply Last reply
      0
      • mrjjM mrjj

        well what do you with myColor ?
        just inserting pointers to QColor in a list should not crash anything.

        how did you define myColor ?

        QColor *myColor[100];

        is it global data ?

        if you defined it in a function, then it will run out of scope and be invalid later.

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

        @mrjj said:

        well what do you with myColor ?
        just inserting pointers to QColor in a list should not crash anything.

        right now i don't do anything with it. the plan is to provide a set of colors for all UI elements to use, accessible via a function.

        how did you define myColor ?
        QColor *myColor[100];

        i use QColor *myColor[];
        should there be a number between [ and ] ?

        is it global data ?

        the QColor *myColor[]; line is in the header file, just after the Q_OBJECT line. that should make it available to all the functions of the class, right?

        if you defined it in a function, then it will run out of scope and be invalid later.

        the assignments are in a function, but the definition is in the header file

        mrjjM J 2 Replies Last reply
        0
        • Y Offline
          Y Offline
          yoavmil
          wrote on last edited by
          #4

          a tip for debuging this: run in debug mode, with the debugger turned on, see where the crash happens, and fugure it out.
          looks like something is trying to delete it layer, maybe twice.
          for a design tip, try as much as you can not to use new and delete, unless you must need it. Qt has its QObect way to make sure you don't forget to delete things. QColor is just an integer, so there is no need to make it dynamic memory.

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

            Hi,

            You are declaring a pointer to an array of QColor objects, did you allocated before using it ? If you need a list/vector of QColor why not use QVector<QColor> or std::vector<QColor> ?

            @yoavmil QColor is not just an integer, it's a full class of its own. Using QObject when needed doesn't free you from proper memory management handling.

            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
            • H harry

              @mrjj said:

              well what do you with myColor ?
              just inserting pointers to QColor in a list should not crash anything.

              right now i don't do anything with it. the plan is to provide a set of colors for all UI elements to use, accessible via a function.

              how did you define myColor ?
              QColor *myColor[100];

              i use QColor *myColor[];
              should there be a number between [ and ] ?

              is it global data ?

              the QColor *myColor[]; line is in the header file, just after the Q_OBJECT line. that should make it available to all the functions of the class, right?

              if you defined it in a function, then it will run out of scope and be invalid later.

              the assignments are in a function, but the definition is in the header file

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

              @harry
              --should there be a number between [ and ] ?
              yes if you mean it to be a list. else/now
              it means a pointer to a list.
              as @SGaist mentions, it might be easier to use a dynamic list instead of old type of c array
              so std::vector<QColor *> MyColor;
              and MyColor.push_back( new QColor( 0, 0, 0) );
              Note that the vector wont delete the the QColors object.

              --that should make it available to all the functions of the class, right?
              Yes, it is a so called member variable and is the correct way to have data.

              --the plan is to provide a set of colors for all UI elements to use
              Can I ask why stylesheets wont work for you ?
              http://doc.qt.io/qt-5.5/stylesheet-examples.html
              You can easy say for all Buttons, use this color.
              If you apply it to mainwindow, it applies to all objects on screen.

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

                To add to @mrjj, allocating QColor on the heap is rather unusual, if you take a look at Qt's classes which are using QColor, you'll see they all take const references. So you'll be dereferencing your heap allocated objects all the time if you use them with Qt classes which essentially wastes CPU cycles.

                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

                  To add to @mrjj, allocating QColor on the heap is rather unusual, if you take a look at Qt's classes which are using QColor, you'll see they all take const references. So you'll be dereferencing your heap allocated objects all the time if you use them with Qt classes which essentially wastes CPU cycles.

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

                  i finally had some time to return to this project, and now i have 2 more problems:

                  as i said above, i have an array of QColors. the problem is that when i try to access one of them with

                  return mycolor[n];

                  the compiler gives me the following error:

                  invalid conversion from QColor* to Qrgb.

                  wait, what? where does this Qrgb come from? the array contains Qcolors, and the function (or method?) is supposed to return a QColor. so what am i doing wrong here?

                  the other problem is that the paintEvent() function of that class is not called. i change the size (both by dragging the edge of the window and by calling resize()), i call update() but paintEvent does not run, the background does not turn green, and the breakpoint is not hit. what's going on here? it is a subclass of QWidget, in case that matters.

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

                    You don't have an array of QColor, you have an array of QColor pointers. Since your function returns a QColor object, a conversion must be done and the constructor that matches best is the one taking a Qrgb, but still, it's not valid.

                    As suggested before, use a vector of QColor rather than a vector of pointers to QColor.

                    As for paintEvent, can you post your code ?

                    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

                      You don't have an array of QColor, you have an array of QColor pointers. Since your function returns a QColor object, a conversion must be done and the constructor that matches best is the one taking a Qrgb, but still, it's not valid.

                      As suggested before, use a vector of QColor rather than a vector of pointers to QColor.

                      As for paintEvent, can you post your code ?

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

                      @SGaist said:

                      You don't have an array of QColor, you have an array of QColor pointers. Since your function returns a QColor object, a conversion must be done and the constructor that matches best is the one taking a Qrgb, but still, it's not valid.

                      As suggested before, use a vector of QColor rather than a vector of pointers to QColor.

                      i put in a QVector, nd now it seems to work ok. i'm gonna find out when i get to the point where i get the button to request a color from the screen.

                      As for paintEvent, can you post your code ?

                      what code are you referring to? the code in my paintEvent() method is irrelevant right now because it does not get called. yes, i have confirmed that by setting a breakpoint on the first line.

                      my main() method looks like this:

                      int main(int argc, char *argv[])
                      {
                      QApplication a(argc, argv);
                      MyScreen w;
                      w.show();
                      return a.exec();
                      }

                      did i mention that the paintEvent() problem is with MyScreen?

                      in myscreen.h i had these lines:

                      public slots:
                      void paintEvent();

                      and then i saw these lines in an example program:

                      protected:
                      void paintEvent();

                      and i tried them out but that change made no difference.

                      H 1 Reply Last reply
                      0
                      • H harry

                        @mrjj said:

                        well what do you with myColor ?
                        just inserting pointers to QColor in a list should not crash anything.

                        right now i don't do anything with it. the plan is to provide a set of colors for all UI elements to use, accessible via a function.

                        how did you define myColor ?
                        QColor *myColor[100];

                        i use QColor *myColor[];
                        should there be a number between [ and ] ?

                        is it global data ?

                        the QColor *myColor[]; line is in the header file, just after the Q_OBJECT line. that should make it available to all the functions of the class, right?

                        if you defined it in a function, then it will run out of scope and be invalid later.

                        the assignments are in a function, but the definition is in the header file

                        J Offline
                        J Offline
                        Jakob
                        wrote on last edited by
                        #11

                        @harry You should read, learn, and remember this: http://www.cplusplus.com/doc/tutorial/arrays/

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

                          Your paintEvent signature is wrong, it's missing the parameter.

                          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
                          • H harry

                            @SGaist said:

                            You don't have an array of QColor, you have an array of QColor pointers. Since your function returns a QColor object, a conversion must be done and the constructor that matches best is the one taking a Qrgb, but still, it's not valid.

                            As suggested before, use a vector of QColor rather than a vector of pointers to QColor.

                            i put in a QVector, nd now it seems to work ok. i'm gonna find out when i get to the point where i get the button to request a color from the screen.

                            As for paintEvent, can you post your code ?

                            what code are you referring to? the code in my paintEvent() method is irrelevant right now because it does not get called. yes, i have confirmed that by setting a breakpoint on the first line.

                            my main() method looks like this:

                            int main(int argc, char *argv[])
                            {
                            QApplication a(argc, argv);
                            MyScreen w;
                            w.show();
                            return a.exec();
                            }

                            did i mention that the paintEvent() problem is with MyScreen?

                            in myscreen.h i had these lines:

                            public slots:
                            void paintEvent();

                            and then i saw these lines in an example program:

                            protected:
                            void paintEvent();

                            and i tried them out but that change made no difference.

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

                            @harry said:

                            @SGaist said:

                            You don't have an array of QColor, you have an array of QColor pointers. Since your function returns a QColor object, a conversion must be done and the constructor that matches best is the one taking a Qrgb, but still, it's not valid.

                            As suggested before, use a vector of QColor rather than a vector of pointers to QColor.

                            i put in a QVector, nd now it seems to work ok. i'm gonna find out when i get to the point where i get the button to request a color from the screen.

                            ok, i'm trying to access that array now, and when i say something like

                            Qcolor c = MyColor[index];

                            i get a little window with an error message about a segmentation fault.

                            this line is in the header:

                            QVector<QColor> MyColor;

                            what am i doing wrong?

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

                              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 1 Reply Last reply
                              0
                              • 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

                                          • Login

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