Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Problem with a color class

  1. #1
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with a color class

    I am using Visual Studio 2005 and Qt 4.3.1.

    When I call my class "colors" like this:

    colors col;
    int r=col.getRed(3);

    the value of r is bad (always 0). ¿¿Someone knows why??


    My colors.h

    Qt Code:
    1. class colors
    2. {
    3. public:
    4. colors();
    5. ~colors();
    6.  
    7. int getRed(int num);
    8. enum{max_colors=17};
    9.  
    10. private:
    11. struct ColorData
    12. {
    13. int num;
    14. QString name;
    15. int R;
    16. int G;
    17. int B;
    18. }Propiedades[max_colors],*p;
    19. };
    To copy to clipboard, switch view to plain text mode 

    and my colors.cpp

    Qt Code:
    1. #include "colors.h"
    2.  
    3. colors::colors()
    4. {
    5. ColorData Propiedades[max_colors] =
    6. {
    7. { 0, "black", 0, 0, 0},
    8. { 1, "white",255,255,255},
    9. { 2, "darkGray",128,128,128},
    10. { 3, "gray",160,160,164},
    11. ...
    12. };
    13. p=&Propiedades[0];
    14. }
    15.  
    16. int colors::getRed(int num)
    17. {
    18. ColorData *q;
    19. q=p;
    20. for (int i=0;i<=max_colors;i++){
    21. if (num==q->num) return q->R;
    22. q++;
    23. }
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem with a color class

    May I ask why not use QColor?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    As you know (other post), I'm trying to programm a combobox with colors. But I need only some colors and when I programm QColor("name of color") :
    Qt Code:
    1. QStringList colorNames;
    2. colorNames <<"black"<<"white"<<"darkGray"....
    3. ...
    4. pixmap.fill(QColor(name));
    To copy to clipboard, switch view to plain text mode 

    some color are bad (for example darkyellow is black for me). So I programm colors with a structure:

    Qt Code:
    1. struct colortype {QString name;int r; int g; int b;};
    2. colortype colors[max_color+1];
    3. colors[0].name="black";colors[0].r=0;colors[0].g=0;colors[0].b=0;
    4. colors[1].name="white";colors[1].r=255;colors[1].g=255;colors[1].b=255;
    5. ...
    6. pixmap.fill(QColor(colors[cont].r,colors[cont].g,colors[cont].b));
    To copy to clipboard, switch view to plain text mode 

    but i need to use this in several classes. So I think to programm my own class for colors (with only the colors i need and in rgb mode).

  4. #4
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    Can anybody help me???

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    Hmm... why don't you use RGB values instead of names then? Take a look at my color combobox (btw. there is no point in reinventing the wheel, you can use QwwColorComboBox in your project provided you won't violate the licence).
    Attached Images Attached Images

  6. #6
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    I'm newbie in Qt and I don't know about your project (im looking it and its a good project). But I have two reason for programming my own class:

    First, I use the same structure for defining chemical elements, and other properties, that's why a need this structure go fine.
    Second, im learning with qt and i know that the most you programm, most you learn.

    As you can see at first of this topic, I'm using RGB color but when I call the class don't work correctly (structure variables take bad values).

    Thanks

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    The structure is weird. It is much tainted with C like code. You should consider something more object oriented. But still you are reinventing the wheel as QColor already does what you want. You could go for a simple QVector<QColor>.

  8. #8
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    I don't use QColor class because works bad. When I use:

    Qt Code:
    1. QStringList colorNames;
    2. colorNames <<"black"<<"white"<<"darkGray".... <-only some colors, not all
    3. ...
    4. pixmap.fill(QColor(name)); <- this inside a for loop, this is the cause of the QStringList
    To copy to clipboard, switch view to plain text mode 

    the green color is not (0,255,0), is a darkgreen, and darkyellow is black for me.

    And if I have the number of one color in another class (0=black, 1=White, ..., not RGB) how can i know its RGB with QColor? (I only knows its ordinal number that is the same as it position in the combo, not its name and not its R, G or B)

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    So why don't you omit those colors which you don't like or not use names at all? You can create a QColor by passing R, G and B components to QColor constructor.

    If you really wish to have a separate structure holding the components (which is completely unnecessary as that's exactly what QColor does), why don't you do it like this?
    Qt Code:
    1. struct Color{
    2. int red;
    3. int green;
    4. int blue;
    5. QString name;
    6. };
    7. QVector<Color> colors;
    8. foreach(Color col, colors){
    9. QColor c(col.red, col.green, col.blue);
    10. QPixmap px(16,16);
    11. px.fill(c);
    12. comboBox->addItem(QIcon(px), col.name);
    13. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    I think you don't understand me (excuse me for my bad english), so i'm trying to explain better:

    If i used color names like this:
    Qt Code:
    1. QStringList colorNames;
    2. colorNames <<"black"<<"white"<<"darkGray".... <-only some colors, not all
    3. ...
    4. foreach (QString name, colorName){
    5. pixmap.fill(QColor(name));
    6. CMBcolor[i]->addItem(QIcon(pixmap),"", QColor(name));
    7. }
    To copy to clipboard, switch view to plain text mode 
    the green color don't display (0,255,0), is a darkgreen, and darkyellow is black for me.
    I don't know if this is a bug or not but the combo displays bad colors for these two (green and darkyellow, the other i use are well but probably there will be other colors that fails)

    So why don't you omit those colors which you don't like ...
    I programm the QStringList to avoid those colors i don't like (I only want 16 colors - white, black, red, darkRed, green, darkGreen, blue, darkBlue, cyan, darkCyan, magenta, darkMagenta, yellow, darkYellow, gray, darkGray, lightGray)

    ... or not use names at all?
    I can avoid names of colors (I use them to complete the structure). But this is not the explanation why don't work my structure.

    Qt Code:
    1. struct ColorData
    2. {
    3. int num;
    4. QString name;
    5. int R;
    6. int G;
    7. int B;
    8. }Propiedades[max_colors],*p;
    9.  
    10. ...
    11.  
    12. ColorData Propiedades[max_colors] =
    13. {
    14. { 0, "black", 0, 0, 0},
    15. { 1, "white",255,255,255},
    16. { 2, "darkGray",128,128,128},
    17. { 3, "gray",160,160,164},
    18. ...
    19. };
    20. p=&Propiedades[0];
    To copy to clipboard, switch view to plain text mode 

    You can create a QColor by passing R, G and B components to QColor constructor.
    Yes, i can. But i have to create the new colors each time i have to use them. So this is the reason to create a class with my colors.

    If you really wish to have a separate structure holding the components (which is completely unnecessary as that's exactly what QColor does), why don't you do it like this?
    Because if i pick the combo i need to know the r g b components of the color i have picked, and with this code i have to redefined my colors again (in other class). And I don't know how to obtain the rgb components of the color when i click in the combo (the selected item of the combo).

    So if my class works I wouldn't be any problem. In the class i have clasified the colors for a single number what is the same that the position that the colors is in the combo. For example, if the combo return 1, i know that is the color white and i know elsewhere of the program the r,g,b components of this color.

    Qt Code:
    1. connect(CMBcolor,SIGNAL(currentIndexChanged(int)),this,SLOT(CMBcolor_change(int)));
    To copy to clipboard, switch view to plain text mode 

    The current index is known in CMBcolor_change by the int variable. And with this integer i obtain the rgb components. With QColor i don't know how to do this because QColor use the name or the r,g,b but not the index.

    As you can see i don't use the name of the color, as i say this is to complete the structure.

    By all this i have decided to bulid the class.

    Excuse me for my english and i hope you understand me.

  11. #11
    Join Date
    Mar 2006
    Posts
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with a color class

    Hello, I think
    colors::colors()
    {
    ColorData Propiedades[max_colors] =// this is local (constructor) variable, not class instance Propiedades.
    {
    { 0, "black", 0, 0, 0},
    { 1, "white",255,255,255},
    { 2, "darkGray",128,128,128},
    { 3, "gray",160,160,164},
    ...
    };
    p=&Propiedades[0]; // p now points to local (ctor) variable which will be diallocated
    //right now.
    }

    int colors::getRed(int num)
    {
    ColorData *q;
    q=p; // p points to deallocated memory.
    for (int i=0;i<=max_colors;i++){// bad, must be "i < max_colors" or even "i < colors::max_colors".
    if (num==q->num) return q->R;
    q++;
    }
    return 0;
    }

  12. #12
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    Yes, I think that I am pointing to a deallocated variable, because p takes extrange values in the debugger. But how to do it well? How to convert this local (constructor) variable to a class instance? (Think that Im newbie in Qt and C++. I programm in Fortran, visual basic, c...)

    Thanks

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    Quote Originally Posted by zorro68 View Post
    Yes, i can. But i have to create the new colors each time i have to use them.
    No, if you store them as QColor objects in the first place.

    Because if i pick the combo i need to know the r g b components of the color i have picked, and with this code i have to redefined my colors again (in other class). And I don't know how to obtain the rgb components of the color when i click in the combo (the selected item of the combo).
    I really don't see a problem.
    Qt Code:
    1. QMap<QString, QColor> colors; // or simply QList<QColor> if you don't want the names
    2. colors["white"] = QColor(255,255,255);
    3. colors["red"] = QColor(255,0,0);
    4. //...
    5.  
    6. for(QMap<QString,QColor>::const_iterator it = colors.begin(); it!=colors.end();++it){
    7. comboBox->addItem(...);
    8. }
    To copy to clipboard, switch view to plain text mode 
    and then when you want to identify the color:
    Qt Code:
    1. int index = comboBox->currentIndex();
    2. QColor col = colors[colors.keys().at(index)];
    To copy to clipboard, switch view to plain text mode 

    Or simply use the ability of combobox to store custom data with each item:
    Qt Code:
    1. QList<QColor> colors;
    2. //...
    3. foreach(QColor c, colors){
    4. comboBox->addItem(...);
    5. int index = comboBox->count()-1;
    6. comboBox->setItemData(index, c);
    7. }
    To copy to clipboard, switch view to plain text mode 
    and then use QComboBox::itemData to fetch the color directly from the combobox.

    So if my class works I wouldn't be any problem.
    Sure. You're just wasting time fixing something that you completely don't need, because the combobox already provides everything you might want. Oh, and about doubling data - you're doubling it with your structure as well. First you hold it in your structure and then QComboBox is creating its own items. You could of course avoid that by using a simple model (like I do with my color combobox class), but in your case I'd simply ignore the redundancy - there is no point in making things too complex.

  14. #14
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    Ok wysota, sorry for my ignorance. I'm going to program like you say. I'm proving with the first sugerence and i have a problem:

    Qt Code:
    1. for(QMap<QString,QColor>::const_iterator it = colors.begin(); it!=colors.end();++it){
    2. pixmap.fill(it->second);
    3. CMBcolor[i]->addItem(QIcon(pixmap),"",it->second);
    4. }
    To copy to clipboard, switch view to plain text mode 

    this give me an error for it->second (second not a member of QColor). I have read that it->first is the QString and it->second the QColor, but don't work. Why?

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    Quote Originally Posted by zorro68 View Post
    I'm going to program like you say.
    I don't say you should do as I said. I only said you were reinventing the wheel and wasting your precious time for things you don't really need. But the final choice is yours.

    I'm proving with the first sugerence and i have a problem:

    Qt Code:
    1. for(QMap<QString,QColor>::const_iterator it = colors.begin(); it!=colors.end();++it){
    2. pixmap.fill(it->second);
    3. CMBcolor[i]->addItem(QIcon(pixmap),"",it->second);
    4. }
    To copy to clipboard, switch view to plain text mode 

    this give me an error for it->second (second not a member of QColor). I have read that it->first is the QString and it->second the QColor, but don't work. Why?
    You should use it.key() and it.value() instead. Operator * of the iterator (which you are using behind the scenes by calling "->") returns the value of the map item (look at QMap::const_iterator docs) - in this case QColor.

  16. #16
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    You have convinced me because i don't like to waste computer memory...

    I have prove with it->value()

    Qt Code:
    1. QMap<QString, QColor> colors;
    2. colors["black"] = QColor(0,0,0);
    3. colors["white"] = QColor(255,255,255);
    4. colors["red"] = QColor(255,0,0);
    5. colors["darkGray"]=QColor(128,128,128);
    6. ...
    7.  
    8. for(QMap<QString,QColor>::const_iterator it = colors.begin(); it!=colors.end();++it){
    9. pixmap.fill(it->value());
    10. CMBcolor[i]->addItem(QIcon(pixmap),"",it->value());
    11. }
    To copy to clipboard, switch view to plain text mode 

    But all colors are diferents blue and black. Im using Qt 4.3.1

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    What is CMBcolor? An array of combobox pointers? How did you create "pixmap"?

    The following works well for me:
    Qt Code:
    1. #include <QComboBox>
    2. #include <QApplication>
    3. #include <QList>
    4. #include <QColor>
    5. #include <QPixmap>
    6.  
    7. int main(int argc, char **argv){
    8. QApplication app(argc, argv);
    9. QComboBox box;
    10. QList<QColor> colors;
    11. colors << QColor(255, 255, 255) << QColor(255, 0, 0) << QColor(128, 128, 128) << QColor(0,0,0);
    12. QPixmap px(16,16);
    13. foreach(QColor color, colors){
    14. px.fill(color);
    15. box.addItem(px, QString::null, color);
    16. }
    17. box.show();
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    What is CMBcolor? An array of combobox pointers?
    Yes

    How did you create "pixmap"?
    Qt Code:
    1. int size = CMBcolor[i]->style()->pixelMetric(QStyle::PM_SmallIconSize);
    2. QPixmap pixmap(size, size);
    To copy to clipboard, switch view to plain text mode 

    As you know, this is the same as QPixmap pixmap(16,16) <- I have proved with this too

    Now i'm going to prove this.

  19. #19
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with a color class

    Yes, this works fine if there is a simple function. But now I'm going to use the "colors" variable in another function and i have an error because this function cannot "see" this variable.
    So I put in my .h file the definition of the "colors" variable like this: QList<QColor *> colors; (¿is this ok?)
    and I'm going to initialize theirs values in .cpp file with this
    colors << QColor(0,0,0) << QColor(255,255,255) << ...
    but this not work because the << operator don't work with pointers (I think).
    How can i fill the values of colors?
    Last edited by zorro68; 25th September 2007 at 23:03.

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a color class

    Quote Originally Posted by zorro68 View Post
    Yes, this works fine if there is a simple function. But now I'm going to use the "colors" variable in another function and i have an error because this function cannot "see" this variable.
    So make it global to the parent scope (for example by making it a member variable) or global to the application or pass it as an argument to where you want to use it.

    So I put in my .h file the definition of the "colors" variable like this: QList<QColor *> colors; (¿is this ok?)
    No. Should be "extern QList<QColor> colors;" (and forget the pointer, there is no point using it and you're wasting additionally 4/8 bytes per item). Then create the variable in one of your .cpp files without the extern keyword.

    and I'm going to initialize theirs values in .cpp file with this
    colors << QColor(0,0,0) << QColor(255,255,255) << ...
    but this not work because the << operator don't work with pointers (I think).
    How can i fill the values of colors?
    Don't use pointers

Similar Threads

  1. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.