Results 1 to 20 of 25

Thread: Problem with a color class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

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

    Default Re: Problem with a color class

    Should be "extern QList<QColor> colors;" . Then create the variable in one of your .cpp files without the extern keyword.
    So you say that i have to write "extern QList<QColor> colors;" in my .h file out of the class (because .h file conteins a class) and "QList<QColor> colors"; in the function where i want to use this variable in the .cpp file.??

    Note that i have defined this variable in the constructor of a class and using in other functions of the class.
    Last edited by zorro68; 25th September 2007 at 23:44.

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

    Default Re: Problem with a color class

    I have a lots of troubles with the extern variable and to avoid this extern variable, i have try this:

    Qt Code:
    1. foreach(QColor color, colors){
    2. CMBcolor[i]->addItem(QString::null,color);
    3. int index = CMBcolor[i]->count()-1;
    4. pixmap.fill(color);
    5. CMBcolor[i]->setItemData(index, pixmap, Qt::DecorationRole);
    6. }
    To copy to clipboard, switch view to plain text mode 

    and fill all my combos correctly (I think this is more elegant). But when I want to know the clicked color i program (in other functions):

    Qt Code:
    1. QColor col = CMBcolor[sup]->itemData(ncolor,Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 
    (ncolor is the currentitem in the combo, sup is the combo i have picked)

    But I obtain an error. QColor is not QVariant. I have proved this:

    Qt Code:
    1. QVariant col = CMBcolor[sup]->itemData(ncolor,Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 

    and compile ok but how can i obtain the r chanel, g chanel and b chanel of the color from col.

    Note that i would also have this problem with the other way too.

  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

    Quote Originally Posted by zorro68 View Post
    So you say that i have to write "extern QList<QColor> colors;" in my .h file out of the class (because .h file conteins a class) and "QList<QColor> colors"; in the function where i want to use this variable in the .cpp file.??
    No, not in a function. Outside all functions - this will make it global to the application. And the extern statement just says "hey you compiler! There is a 'QList<QColor> colors' variable somewhere out there so you can allow functions to use it, it will be available during link time".

    Note that i have defined this variable in the constructor of a class and using in other functions of the class.
    You should define it in main.cpp (outside main()) and fill it in main().

    Quote Originally Posted by zorro68 View Post
    I have a lots of troubles with the extern variable and to avoid this extern variable, i have try this:

    Qt Code:
    1. foreach(QColor color, colors){
    2. CMBcolor[i]->addItem(QString::null,color);
    3. int index = CMBcolor[i]->count()-1;
    4. pixmap.fill(color);
    5. CMBcolor[i]->setItemData(index, pixmap, Qt::DecorationRole);
    6. }
    To copy to clipboard, switch view to plain text mode 

    and fill all my combos correctly (I think this is more elegant).
    Not really. It would be better to subclass QComboBox and do that inside its constructor. And of course then use the subclass instead of QComboBox.


    But I obtain an error. QColor is not QVariant. I have proved this:

    Qt Code:
    1. QVariant col = CMBcolor[sup]->itemData(ncolor,Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 
    Of course it's not. You have to convert it to QColor. I suggest you read QVariant docs, especially the section about qvariant_cast.

    Maybe you should just use QwwColorComboBox?

  6. The following user says thank you to wysota for this useful post:

    zorro68 (26th September 2007)

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

    Default Re: Problem with a color class

    At last, the combo works fine.

    Wysota thank you very much for your help and for your patience. Think that i am newbie in qt and c++ so i don't know a lot of "easy" ways to do something in qt.

    I'm programming an scientific aplication in fortran (this works fine) and I need a grafical interface to introduce data, but to work with windows and linux, so this is why i use qt. I have program in fortran, c, visual basic, ... and i thought that c++ would be like c, but it isn't. Now i have to plot results (data surface) in 3d, so i'm programming an opengl module (marching cubes algorithm) to do this. ¡¡¡¡¡¡¡ C++, qt, opengl, math algorithms all in one, buffff !!!!!!!! and english. In two weeks, i'll go to uk (Cardiff) with a grant to spend 10 month (I hope to learn a good english).

    I'll be here soon with other question...

    Now a personal question for you and jpn (you can answer if you like), have you programm the qt or do you work for qt? (because you are connected all time as i can see).

    Thanks for all
    Last edited by zorro68; 26th September 2007 at 15:11.

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

    We don't work for Trolltech if that is what you ask.

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.